路丝丝 发表于 2015-11-24 16:32

Unity3D中AssetBundle加载研究


先梳理一下思路:
要加载一个资源A,必须先去加载它的所有依赖资源。
要知道这个资源A依赖了哪些资源,必须先去加载AssetBundleManifest。
通过AssetBundleManifest对象的GetAllDependencies(A)方法,获取它依赖的所有资源。
依赖资源都加载了,就可以去真正加载资源A了。
注意点:
1.资源A加载完了后,要记得Unload(false),资源A的依赖资源要在 资源A加载完成后,才能Unload(false),否则无法正常加载资源A。
2.不Unload的话也可以,那就自己做一个字典记录所有加载过的AssetBundle,还有它们的引用计数器。那样就可以先判断是否存在,然后再确定是否要去加载。
我下面的例子,采用1的方式。就是加载完了后就Unload的方式。
using Unity Engine;
using System.Collections;
using System.Collections.Generic;
using System;
public class AssetBundleLoaderMgr : Singleton
{
public string m_assetPath = Application.streamingAssetsPath;
string assetTail = ".unity3d";
#region LoadAssetBundle
///
/// 加载目标资源
///
///
///
public void LoadAssetBundle(string name, Action callback)
{
name = name + assetTail;//eg:ui/panel.unity3d
Action> action = (depenceAssetBundles) =>
{
string realName = this.GetRuntimePlatform() + "/" + name;//eg:Windows/ui/panel.unity3d
LoadResReturnWWW(realName, (www) =>
{
int index = realName.LastIndexOf("/");
string assetName = realName.Substring(index + 1);
assetName = assetName.Replace(assetTail, "");
AssetBundle assetBundle = www.assetBundle;
UnityEngine.Object obj = assetBundle.LoadAsset(assetName);//LoadAsset(name),这个name没有后缀,eg:panel
//卸载资源内存
assetBundle.Unload(false);
for (int i = 0; i < depenceAssetBundles.Count; i++)
{
depenceAssetBundles.Unload(false);
}
//加载目标资源完成的回调
callback(obj);
});
};
LoadDependenceAssets(name, action);
}
///
/// 加载目标资源的依赖资源
///
///
///
private void LoadDependenceAssets(string targetAssetName, Action> action)
{
Debug.Log("要加载的目标资源:" + targetAssetName);//ui/panel.unity3d
Action dependenceAction = (manifest) =>
{
List depenceAssetBundles = new List();//用来存放加载出来的依赖资源的AssetBundle
string[] dependences = manifest.GetAllDependencies(targetAssetName);
Debug.Log("依赖文件个数:" + dependences.Length);
int length = dependences.Length;
int finishedCount = 0;
if (length == 0)
{
//没有依赖
action(depenceAssetBundles);
}
else
{
//有依赖,加载所有依赖资源
for (int i = 0; i < length; i++)
{
string dependenceAssetName = dependences;
dependenceAssetName = GetRuntimePlatform() + "/" + dependenceAssetName;//eg:Windows/altas/heroiconatlas.unity3d
//加载,加到assetpool
LoadResReturnWWW(dependenceAssetName, (www) =>
{
int index = dependenceAssetName.LastIndexOf("/");
string assetName = dependenceAssetName.Substring(index + 1);
assetName = assetName.Replace(assetTail, "");
AssetBundle assetBundle = www.assetBundle;
UnityEngine.Object obj = assetBundle.LoadAsset(assetName);
//assetBundle.Unload(false);
depenceAssetBundles.Add(assetBundle);
finishedCount++;
if (finishedCount == length)
{
//依赖都加载完了
action(depenceAssetBundles);
}
});
}
}
};
LoadAssetBundleManifest(dependenceAction);
}
///
/// 加载AssetBundleManifest
///
///
private void LoadAssetBundleManifest(Action action)
{
string manifestName = this.GetRuntimePlatform();
manifestName = manifestName + "/" + manifestName;//eg:Windows/Windows
LoadResReturnWWW(manifestName, (www) =>
{
AssetBundle assetBundle = www.assetBundle;
UnityEngine.Object obj = assetBundle.LoadAsset("AssetBundleManifest");
assetBundle.Unload(false);
AssetBundleManifest manif = obj as AssetBundleManifest;
action(manif);
});
}
#endregion
#region ExcuteLoader
public void LoadResReturnWWW(string name, Action callback)
{
string path = "file://" + this.m_assetPath + "/" + name;
Debug.Log("加载:" + path);
StartCoroutine(LoaderRes(path, callback));
}
IEnumerator LoaderRes(string path, Action callback)
{
WWW www = new WWW(path);
yield return www;
if (www.isDone)
{
callback(www);
}
}
#endregion
#region Util
///
/// 平台对应文件夹
///
///
private string GetRuntimePlatform()
{
string platform = "";
if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
{
platform = "Windows";
}
else if (Application.platform == RuntimePlatform.Android)
{
platform = "Android";
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
platform = "IOS";
}
else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
{
platform = "OSX";
}
return platform;
}
}
测试一下咯
42using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TestAssetBundleLoader : MonoBehaviour
{
Dictionary GameObjectPool = new Dictionary();
void Start()
{
//要加载的资源的队列
Queue needLoadQueue = new Queue();
needLoadQueue.Enqueue("ui/plane");
needLoadQueue.Enqueue("ui/cube");
needLoadQueue.Enqueue("ui/sphere");
Load(needLoadQueue);
}
void Load(Queue needLoadQueue)
{
if (needLoadQueue.Count > 0)
{
string needLoadAssetName = needLoadQueue.Dequeue();
AssetBundleLoaderMgr.Instance.LoadAssetBundle(needLoadAssetName, (obj) =>
{
GameObject go = GameObject.Instantiate(obj) as GameObject;
int index = needLoadAssetName.LastIndexOf("/");
string assetName = needLoadAssetName.Substring(index + 1);
//加载出来的GameObject放到GameObjectPool存储
GameObjectPool.Add(assetName, go);
Load(needLoadQueue);
});
}
else
{
Debug.Log("all finished");
}
}
}
可能代码写的还有很多优化的地方 ,毕竟100个人有100种写法,思路才是最重要的。其实我想说的是,代码都在了,自己尝试一下吧!本篇unity3d教程到此结束。
资源地址: http://cg.silucg.com/dongman/unity3d/7960.html

linmickey 发表于 2016-9-22 09:24


膜拜中。。。。

动漫中国 发表于 2017-2-18 21:09

顶顶多好

似冷非寒ヽ冰 发表于 2017-2-18 20:40

难得一见的好帖

shangcunbao 发表于 2017-2-18 21:03

说的非常好

动漫中国 发表于 2017-2-18 20:27

很好哦

质. 发表于 2017-2-18 20:28

不错不错

cuckoo307 发表于 2017-4-2 10:22

楼主是超人

lvxuehu 发表于 2017-4-2 10:40

好帖就是要顶

lvxuehu 发表于 2017-4-2 11:13

难得一见的好帖
页: [1] 2 3
查看完整版本: Unity3D中AssetBundle加载研究