unity5.4.3p2里面的AssetBundle打包流程,相比之前unity4.x的打包简单了许多,Unity4.X中打包的时候需要自己去管理依赖关系,各种BuildPipeline.PushAssetDependencies()和BuildPipeline.PopAssetDependencies(),一不小心手一抖,依赖关系就惨不忍睹。下面简单说说一下unity5.4的步骤。
1.在工程里新建两个prefab,分别为ui1.prefab和ui2.prefab,如下图:
然后选中ui1,在右下角的AssetBundle Setting中,设置ui1的AssetBundleName为ui1,一样的步骤将ui2的AssetBundleName设置为ui2。注意:当把AssetBundleName设置为同一个的时候,将会打进同一个包,这里分开打包。
2.在ui1中引用ui2,具体引用操作很简单,在ui1上挂个脚本,脚本里弄个publish GameObject prefab就好,如下图:
然后将AssetBundle目录下的资源进行打包,打包过程其实很简单(BuildAssetBundleOptions选择不同的压缩格式,压缩比不一样,在Profiler中SerializedFile大小也不一样,加载后加压的速度也不一样,可酌情考虑使用),过程如下:
- using UnityEngine;
- using System.Collections;
- using UnityEditor;
- public class CreateAssetBundles : Editor
- {
- [MenuItem("Tools/Build AssetBundles")]
- static void BuildAllAssetBundles()
- {
- BuildPipeline.BuildAssetBundles(Application.dataPath + "/AssetBundles", BuildAssetBundleOptions.UncompressedAssetBundle,BuildTarget.Android);
- AssetDatabase.SaveAssets();
- AssetDatabase.Refresh();
- Debug.LogWarning("打包成功");
- }
- }
打包后的结果如下:
打包的时候,在输出的bundle所在的文件夹内还会生成一个总的manifest文件,叫做[文件夹名].manifest,我们看看都生成了什么文件,
前面三个文件和后面三个文件是新生成的,其实我们用到的也就是这几个文件。在AssetBundles.manifest中我们看到
这里记录了我们打包的所有的assetBundle的信息,其中info_0中记录了ui1中引用了ui2。再看看ui1.manifest文件,
这里也记录了ui1引用了哪些assetBundle。
3.加载assetBundle
前面提到ui1中引用到了ui2的资源,所以这里分两种方式加载,一种是先加载ui1再加载ui2,另一种是先加载ui2再加载ui1,看看这两种方式有什么区别。
(1)先加载ui1后加载ui2
- void Load()
- {
-
- AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/AssetBundles");
- if (assetBundleManifest != null)
- {
- AssetBundleManifest manifest = (AssetBundleManifest)assetBundleManifest.LoadAsset("AssetBundleManifest");
-
-
- AssetBundle bundle = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/ui1");
- GameObject obj = bundle.LoadAsset("ui1") as GameObject;
- if (obj != null)
- {
- Instantiate(obj);
- }
-
- string[] depends = manifest.GetAllDependencies("ui1");
- AssetBundle[] dependsAssetbundle = new AssetBundle[depends.Length];
- for (int index = 0; index < depends.Length; index++)
- {
- dependsAssetbundle[index] = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/" + depends[index]);
- }
- }
- }
结果如下图:
所以这里先加载ui1后加载ui1的依赖文件ui2,Relation中的prefab居然没有missing。我可记得在4.X的版本中必须先要加载依赖文件才行的啊
(2)在加载依赖文件ui2再加载ui1,
- void Load()
- {
-
- AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/AssetBundles");
- if (assetBundleManifest != null)
- {
- AssetBundleManifest manifest = (AssetBundleManifest)assetBundleManifest.LoadAsset("AssetBundleManifest");
-
- string[] depends = manifest.GetAllDependencies("ui1");
- AssetBundle[] dependsAssetbundle = new AssetBundle[depends.Length];
- for (int index = 0; index < depends.Length; index++)
- {
- dependsAssetbundle[index] = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/" + depends[index]);
- }
-
- AssetBundle bundle = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/ui1");
- GameObject obj = bundle.LoadAsset("ui1") as GameObject;
- if (obj != null)
- {
- Instantiate(obj);
- }
-
- }
- }
这种情况ui2出来可以正常理解了。
(3)只加载ui1,不加载依赖文件ui2
- void Load()
- {
-
- AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/AssetBundles");
- if (assetBundleManifest != null)
- {
- AssetBundleManifest manifest = (AssetBundleManifest)assetBundleManifest.LoadAsset("AssetBundleManifest");
-
-
-
-
-
-
-
-
- AssetBundle bundle = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/ui1");
- GameObject obj = bundle.LoadAsset("ui1") as GameObject;
- if (obj != null)
- {
- Instantiate(obj);
- }
-
- }
- }
这种情况必须missing啊。
so,问题出来了,本测试中使用的是unity5.4.3p2,为何步骤(1)中ui1依赖的文件ui2后加载也能出来?,暂时不知为何,看官如果知道可否指点一二。