转自http://www.cnblogs.com/sifenkesi/p/3915477.html
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数:
BuildPipeline.PushAssetDependencies():依赖资源压栈;
BuildPipeline.PopAssetDependencies():依赖资源出栈。
直接看代码,下面为打包示例代码,Prefab1和Prefab2共享贴图资源Tex1,在打包时将Tex1单独打包,而Prefab1和Prefab2对应的assetbundle包中不实际包含Tex1资源,而是记录Tex1资源的引用:
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
@H_301_21@
public class PushAndPop
{
[MenuItem("Test/BuildAssetBundle")]
static void Execute()
{
string SavePath = "C:\\";
@H_301_21@
BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
| BuildAssetBundleOptions.DeterministicAssetBundle;
@H_301_21@
BuildPipeline.PushAssetDependencies();
// 共享资源Tex1.tga
Object sharedAsset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/Tex1.tga");
BuildPipeline.BuildAssetBundle(sharedAsset,null,SavePath + sharedAsset.name + ".assetbundle",buildOp,BuildTarget.StandaloneWindows);
@H_301_21@
// Prefab1,引用了Tex1.tga
BuildPipeline.PushAssetDependencies();
Object p1Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P1.prefab");
BuildPipeline.BuildAssetBundle(p1Asset,SavePath + p1Asset.name + ".assetbundle",BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
@H_301_21@
// Prefab2,引用了Tex1.tga
BuildPipeline.PushAssetDependencies();
Object p2Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P2.prefab");
BuildPipeline.BuildAssetBundle(p2Asset,SavePath + p2Asset.name + ".assetbundle",BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
@H_301_21@
BuildPipeline.PopAssetDependencies();
@H_301_21@
EditorUtility.DisplayDialog("","Completed","OK");
AssetDatabase.Refresh();
}
@H_301_21@
}
可以看到,Push和Pos都是成对使用,一个Push/Pop对就相当于一个Layer(层),层可以嵌套,内层可以依赖外层的资源。也就是说内层某资源在打包时,如果其引用的某个资源已经在外层加载了,那么内层的这个资源包就会包含该资源的引用而不是资源本身。Push/Pop实际上维持了一个依赖的堆栈。
那么,在加载依赖资源包时,需要注意的是:先加载依赖的资源,然后加载其他资源,需要确保这个顺序。下面的代码演示如何使用依赖资源包:
using UnityEngine;
using System.Collections;
@H_301_21@
public class NewBehavIoUrScript : MonoBehavIoUr
{
void OnGUI()
{
// 清空本地缓存
if (GUI.Button(new Rect(0f,0f,100f,20f),Caching.spaceOccupied.ToString()))
{
Caching.CleanCache();
}
@H_301_21@
if (GUI.Button(new Rect(0f,30f,"Load Share Res"))
{
StartCoroutine(Load(@"file://C:\Tex1.assetbundle",1));
}
@H_301_21@
if (GUI.Button(new Rect(0f,60f,"Load And Instantiate Prefab"))
{
StartCoroutine(LoadAndInstantiate(@"file://C:\P1.assetbundle",1));
StartCoroutine(LoadAndInstantiate(@"file://C:\P2.assetbundle",1));
}
}
@H_301_21@
// 加载
IEnumerator Load(string url,int version)
{
WWW www = WWW.LoadFromCacheOrDownload(url,version);
yield return www;
}
@H_301_21@
// 加载并实例化
IEnumerator LoadAndInstantiate(string url,version);
yield return www;
@H_301_21@
if (!System.String.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
}
else
{
Object main = www.assetBundle.mainAsset;
GameObject.Instantiate(main);
}
}
@H_301_21@
}
先按第二个按钮,然后再按第三个按钮就能够正确显示两个Prefab,如果直接进行第三步操作,则实例化出来的Prefab会缺少贴图。
另外我们可以从assetbundle包的大小来看一下:
如果不打依赖包,两个prefab都打完整包,得到的包的大小为:
P1.assetbundle 56K
P2.assetbundle 38K
如果打依赖包,得到的包的大小为:
Tex1.assetbundle 10k
P1.assetbundle 47K
P2.assetbundle 29K
规律是不是很明显。