您的位置:首页 > 其它

AsssetBunlder打包

2015-07-24 19:02 295 查看
unity3d,资源过多的话。可以压缩成一个资源包。加载出来后。可以解压。找到自己需要的资源

就想.net网站。很多图标都是放一个大图片上。而不是一个图标就是一个图片

因为是在项目编辑时候给资源打包。并不是项目运行。所以要用到Editor。untiy编辑类,脚本放到Editor文件夹中

using UnityEngine;
using System.Collections;
using UnityEditor;
public class AsssetBunlder
{
[MenuItem("Custom/SayHello")]
static void SayHello()
{
Debug.Log("hello");
}

[MenuItem("Custom/CreateBundle")]
static void CreateBoundle()
{
/*
* Selection类,在编辑状态下,获取选中的对象引用
*/
Object[] objs = Selection.GetFiltered(typeof(object), SelectionMode.DeepAssets);

string path = string.Empty;

foreach (Object ojb in objs)
{
path = Application.streamingAssetsPath + "/" + ojb.name + ".unity3d";
//创建AssetBunlder
BuildPipeline.BuildAssetBundle(ojb, null, path);

//BuildTarget.Android :不同平台打包路径不同
//BuildPipeline.BuildAssetBundle(ojb, null, path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);
}
//自动刷新
AssetDatabase.Refresh();
}

[MenuItem("Custom/CreateAllBundle")]
static void CreateAllBoundle()
{
/*
* Selection类,在编辑状态下,获取选中的对象引用
*/
Object[] objs = Selection.GetFiltered(typeof(object), SelectionMode.DeepAssets);

string path = string.Empty;
path = Application.streamingAssetsPath + "/All.assetbundle";
//创建AssetBunlder
BuildPipeline.BuildAssetBundle(null, objs, path);

//自动刷新
AssetDatabase.Refresh();
}

//场景打包
[MenuItem("Custom/StreamBundle")]
static void StreamBundleScene()
{
//要压缩的场景位置
string[] leves = { "Assets/Scene/Demo2.unity" };

//压缩后。资源保存的位置
string path = Application.streamingAssetsPath + @"/Test.assetsbundle";

/*
BuildAdditionalStreamedScenes:以流的方式打包场景
* StandaloneWindows64:在window64位电脑下打包
*/
BuildPipeline.BuildPlayer(leves, path, BuildTarget.StandaloneWindows64, BuildOptions.BuildAdditionalStreamedScenes);

}

}


单击菜单。会出现你刚写的菜单选项



单击不同的选项就可以根据不同的资源打包了

当读取资源前必须解压

using UnityEngine;
using System.Collections;

public class AssetBoundleContorller : MonoBehaviour
{

/*
* streamingAssetsPath路径。在pc和Andoid,phone中是不同的
* 所以要用预处理(#if #end if)
*/

//    public static string RUL;
//#if UNIYT_ANDROID
//    "jar:file://"+ Application.dataPath+@"/assets";
//#elif UNITY_IPHONE
//    "";
//#endif

//#if UNITY_EDITOR
//    string filepath = Application.dataPath + "/StreamingAssets" + "/my.xml";
//#elif UNITY_IPHONE
// string filepath = Application.dataPath +"/Raw"+"/my.xml";
//#elif UNITY_ANDROID
// string filepath = "jar:file://" + Application.dataPath + "!/assets/"+"/my.xml;
//#else

//#endif

//    public static string RUL =
//#if UNITY_IPHONE
//     string filepath = Application.dataPath +"/assets";
//#elif UNITY_ANDROID
//     "jar:file://" + Application.dataPath + "/assets/";
//#else
// Application.streamingAssetsPath + @"/Sphere.unity3d";
//#endif

string filepath = string.Empty;
void Awake()
{
#if UNITY_ANDROID
filepath = "jar:file://" + Application.dataPath +@"/assets";
#endif

#if UNITY_IPHONE
filepath = "Application.dataPath +@"/Raw";
#endif

#if UNITY_STANDALONE_WIN
filepath = @"file://" + Application.streamingAssetsPath + @"/assets";
#endif
}

// Use this for initialization
void Start()
{
//1:拿到我们的Assetboundle

//string path = @"file://" + Application.streamingAssetsPath + "/Sphere.unity3d";

//StartCoroutine(GetAssetboundle(path));

string path = @"file://" + Application.streamingAssetsPath + "/all.assetbundle";
StartCoroutine(GetAssetboundle(path, "Sphere"));
}

/// <summary>
/// 加载一个资源包
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
IEnumerator GetAssetboundle(string path)
{
//没有缓存
WWW www = new WWW(path);

//缓存机制,缓存在 硬盘中  5代码版本号,每次缓存都会加1
WWW www1 = WWW.LoadFromCacheOrDownload(path, 5);

yield return www;
//2:解压并实例化出来
//www.assetBundl返回的是加载AssetBundle压缩文件
//www.assetBundle.mainAsset:解压获取到物体
Instantiate(www.assetBundle.mainAsset);

//销毁assetBundle的内存
www.assetBundle.Unload(false);
}

/// <summary>
/// 很多资源打包在一起,根据名称找到相应的资源
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <returns></returns>
IEnumerator GetAssetboundle(string path, string name)
{
WWW www = new WWW(path);
yield return www;

//查看资源里面所以的子资源
//www.assetBundle.LoadAll;

//Load 根据名字加载资源
Instantiate(www.assetBundle.Load(name));
}

// Update is called once per frame
void Update()
{
//加载场景
if (Input.GetMouseButtonDown(0))
{
string path = @"file://" + Application.streamingAssetsPath + "/Test.assetsbundle";
StartCoroutine(LoadScene(path, "Demo2"));
}
}

/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <param name="name">打包场景前的scene名称</param>
/// <returns></returns>
IEnumerator LoadScene(string path, string name)
{
WWW www = new WWW(path);
yield return www;

//使用之前必须要解压assetbundle
AssetBundle bundle = www.assetBundle;

//加载场景
Application.LoadLevel(name);

www.assetBundle.Unload(false);

}
}


对于不同平台打包路径是不同的。具体看

http://docs.unity3d.com/Manual/PlatformDependentCompilation.html

http://www.taidous.com/article-417-1.html

http://unity3d.9tech.cn/jiaocheng/2013/0621/32651.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: