AssetBundle系列——资源的加载、简易的资源管理器
每个需要进行资源管理的类都继承自IAssetManager,该类维护它所使用到的所有资源的一个资源列表。并且每个资源管理类可以重写其资源引用接口和解引用接口。
每个管理器有自己的管理策略,比如SceneManager对场景背景图可以保留最近使用的几张,使用LRU算法维护当前内存中的贴图张数等...

using UnityEngine;using System.Collections;using System.Collections.Generic;// 和资源有关的管理器都将继承自此类public class IAssetManager
{ // 管理器所管理的资源列表,实际上是引用列表
protected List<string> lstRefAsset = new List<string>(); // 增加引用的资源
public virtual void RefAsset(string name)
{} // 以一定的策略卸载资源
public virtual bool UnloadAsset()
{ return true; }
}

资源管理器类,UnloadUnusedAsset函数保证只有在真正有资源需要卸载的时候才调用Resources.UnloadUnusedAssets();
有一点需要注意的地方就是,在资源解压完成后,一定要记得要释放压缩包内存,即:
www.assetBundle.LoadAsync(GetAssetName(name), type);
www.assetBundle.Unload(false);
因为,www压缩包数据只能手动卸载,跳转场景都不会删除,所以加载完立即unload是一个好习惯。
还有一点需要注意的www.assetBundle.mainAsset是一个同步加载的操作,也就是说调用此函数时会卡。using UnityEngine;using System.Collections;

using UnityEngine;using System.Collections;using System.Collections.Generic;public class ResourceManager
{ // 已解压的Asset列表 [prefabPath, asset]
private Dictionary<string, Object> dicAsset = new Dictionary<string, Object>(); // "正在"加载的资源列表 [prefabPath, www]
private Dictionary<string, WWW> dicLoadingReq = new Dictionary<string, WWW>();
public Object GetResource(string name)
{
Object obj = null;
if (dicAsset.TryGetValue(name, out obj) == false)
{
Debug.LogWarning("<GetResource Failed> Res not exist, res.Name = " + name);
if (dicLoadingReq.ContainsKey(name))
{
Debug.LogWarning("<GetResource Failed> The res is still loading");
}
} return obj;
} // name表示prefabPath,eg:Prefab/Pet/ABC
public void LoadAsync(string name)
{
LoadAsync(name, typeof(Object));
} // name表示prefabPath,eg:Prefab/Pet/ABC
public void LoadAsync(string name, System.Type type)
{ // 如果已经下载,则返回
if (dicAsset.ContainsKey(name))
return; // 如果正在下载,则返回
if (dicLoadingReq.ContainsKey(name))
return;
// 添加引用 RefAsset(name);
// 如果没下载,则开始下载 CoroutineProvider.Instance().StartCoroutine(AsyncLoadCoroutine(name, type));
}
private IEnumerator AsyncLoadCoroutine(string name, System.Type type)
{
string assetBundleName = GlobalSetting.ConvertToAssetBundleName(name);
string url = GlobalSetting.ConverToFtpPath(assetBundleName);
int verNum = GameApp.GetVersionManager().GetVersionNum(assetBundleName);
Debug.Log("WWW AsyncLoad name =" + assetBundleName + " versionNum = " + verNum);
if (Caching.IsVersionCached(url, verNum) == false)
Debug.Log("Version Is not Cached, which will download from net!");
WWW www = WWW.LoadFromCacheOrDownload(url,verNum);
dicLoadingReq.Add(name, www);
while (www.isDone == false)
yield return null;
AssetBundleRequest req = www.assetBundle.LoadAsync(GetAssetName(name), type);
while (req.isDone == false)
yield return null;
dicAsset.Add(name, req.asset);
dicLoadingReq.Remove(name);
www.assetBundle.Unload(false);
www = null;
// Debug.Log("WWW AsyncLoad Finished " + assetBundleName + " versionNum = " + verNum); }
public bool IsResLoading(string name)
{
return dicLoadingReq.ContainsKey(name);
}
public bool IsResLoaded(string name)
{
return dicAsset.ContainsKey(name);
}
public WWW GetLoadingWWW(string name)
{
WWW www = null;
dicLoadingReq.TryGetValue(name, out www);
return www;
}
// 移除Asset资源的引用,name表示prefabPath
public void UnrefAsset(string name)
{
dicAsset.Remove(name);
}
private string GetAssetName(string ResName)
{
int index = ResName.LastIndexOf('/');
return ResName.Substring(index + 1, ResName.Length - index - 1);
}
public void UnloadUnusedAsset()
{
bool effectNeedUnload = GameApp.GetEffectManager().UnloadAsset();
bool worldNeedUnload = GameApp.GetWorldManager().UnloadAsset();
bool sceneNeedUnload = GameApp.GetSceneManager().UnloadAsset();
if (effectNeedUnload || worldNeedUnload || sceneNeedUnload)
{
Resources.UnloadUnusedAssets();
}
} // 根据资源路径添加资源引用,每个管理器管理自己的引用
private void RefAsset(string name)
{ // 模型之类的
if (name.Contains(GlobalSetting.CharacterPath))
GameApp.GetWorldManager().RefAsset(name); // 图片之类的
else if (name.Contains(GlobalSetting.TexturePath))
GameApp.GetUIManager().RefPTexture(name);// 特效之类的
else if (name.Contains(GlobalSetting.EffectPath))
GameApp.GetEffectManager().RefAsset(name);
......
else
Debug.LogWarning("<Res not ref> name = " + name);
}
}

资源管理的关键在于以下几点:
(1)资源所对应的几块内存的管理,Unity的内存一直是一个相对比较棘手的方面,所以一定要多做尝试找到规律和方法;
(2)资源加载、卸载策略,什么时候加载什么时候卸载需要根据游戏类型来进行定制;
(3)资源打包策略,也就是以什么单位进行什么类型的资源打包,原则上是让同一资源尽量只需要打包一次,比如多个场景都用到了同一棵树,那么最好是对这棵树单独打包等等。
比如Prefab1和Prefab2同时引用了Fbx1,将2个prefab单独打包时都会分别包含Fbx1,并且解压到内存时,也会有2份独立的Fbx1,这样会造成内存变大,这点一定要注意。
......
Unity内存和资源这一块虽然显得比较拖泥带水,不过只要使用的够规范,一般还是能够保证内存的干净的。
还有一个尚未解决的问题,Unity使用www方式加载资源的时候,不能进行同步加载操作,只能异步,我见到过其他人也遇到过这个问题,很是蛋疼菊紧。
AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数:
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;public class PushAndPop
{
[MenuItem("Test/BuildAssetBundle")] static void Execute()
{
string SavePath = "C:\\";
BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
| BuildAssetBundleOptions.DeterministicAssetBundle;
BuildPipeline.PushAssetDependencies();
// 共享资源Tex1.tga
Object sharedAsset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/Tex1.tga");
BuildPipeline.BuildAssetBundle(sharedAsset, null, SavePath + sharedAsset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
// Prefab1,引用了Tex1.tga BuildPipeline.PushAssetDependencies();
Object p1Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P1.prefab");
BuildPipeline.BuildAssetBundle(p1Asset, null, SavePath + p1Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
// Prefab2,引用了Tex1.tga BuildPipeline.PushAssetDependencies();
Object p2Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P2.prefab");
BuildPipeline.BuildAssetBundle(p2Asset, null, SavePath + p2Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
EditorUtility.DisplayDialog("", "Completed", "OK");
AssetDatabase.Refresh();
}
}

可以看到,Push和Pos都是成对使用,一个Push/Pop对就相当于一个Layer(层),层可以嵌套,内层可以依赖外层的资源。也就是说内层某资源在打包时,如果其引用的某个资源已经在外层加载了,那么内层的这个资源包就会包含该资源的引用而不是资源本身。Push/Pop实际上维持了一个依赖的堆栈。
那么,在加载依赖资源包时,需要注意的是:先加载依赖的资源,然后加载其他资源,需要确保这个顺序。下面的代码演示如何使用依赖资源包:

using UnityEngine;using System.Collections;public class NewBehaviourScript : MonoBehaviour
{
void OnGUI()
{ // 清空本地缓存
if (GUI.Button(new Rect(0f, 0f, 100f, 20f), Caching.spaceOccupied.ToString()))
{
Caching.CleanCache();
}
if (GUI.Button(new Rect(0f, 30f, 100f, 20f), "Load Share Res"))
{
StartCoroutine(Load(@"file://C:\Tex1.assetbundle", 1));
}
if (GUI.Button(new Rect(0f, 60f, 100f, 20f), "Load And Instantiate Prefab"))
{
StartCoroutine(LoadAndInstantiate(@"file://C:\P1.assetbundle", 1));
StartCoroutine(LoadAndInstantiate(@"file://C:\P2.assetbundle", 1));
}
} // 加载
IEnumerator Load(string url, int version)
{
WWW www = WWW.LoadFromCacheOrDownload(url, version);
yield return www;
} // 加载并实例化
IEnumerator LoadAndInstantiate(string url, int version)
{
WWW www = WWW.LoadFromCacheOrDownload(url, version);
yield return www;
if (!System.String.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
}
else
{
Object main = www.assetBundle.mainAsset;
GameObject.Instantiate(main);
}
}
}

先按第二个按钮,然后再按第三个按钮就能够正确显示两个Prefab,如果直接进行第三步操作,则实例化出来的Prefab会缺少贴图。
另外我们可以从assetbundle包的大小来看一下:
如果不打依赖包,两个prefab都打完整包,得到的包的大小为:
P1.assetbundle 56K
P2.assetbundle 38K
如果打依赖包,得到的包的大小为:
Tex1.assetbundle 10k
P1.assetbundle 47K
P2.assetbundle 29K