Unity开发者联盟
标题:
Unity3D中AssetBundle加载研究
[打印本页]
作者:
路丝丝
时间:
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 20:27
很好哦
作者:
质.
时间:
2017-2-18 20:28
不错不错
作者:
似冷非寒ヽ冰
时间:
2017-2-18 20:40
难得一见的好帖
作者:
shangcunbao
时间:
2017-2-18 21:03
说的非常好
作者:
动漫中国
时间:
2017-2-18 21:09
顶顶多好
作者:
yanseyundao37
时间:
2017-4-2 10:22
很好哦
作者:
cuckoo307
时间:
2017-4-2 10:22
楼主是超人
作者:
little_xiang
时间:
2017-4-2 10:37
不错不错
作者:
lvxuehu
时间:
2017-4-2 10:40
好帖就是要顶
作者:
lvxuehu
时间:
2017-4-2 11:13
难得一见的好帖
作者:
等待1gr
时间:
2017-4-4 13:05
难得一见的好帖
作者:
wangjian_3344
时间:
2017-4-4 13:12
顶顶多好
作者:
等待1gr
时间:
2017-4-4 13:23
楼主是超人
作者:
等待1gr
时间:
2017-4-4 13:25
真心顶
作者:
那些年
时间:
2017-4-4 13:27
很不错
作者:
deng0512
时间:
2017-5-31 13:38
很不错
作者:
ScaleU
时间:
2017-5-31 14:06
很好哦
作者:
cooli
时间:
2017-5-31 14:10
真心顶
作者:
deng0512
时间:
2017-5-31 14:16
楼主是超人
作者:
kiyuan
时间:
2017-5-31 14:23
难得一见的好帖
作者:
835664915
时间:
2017-6-26 11:52
6666666666666666666666
好 好 好
作者:
hqblove
时间:
2017-7-28 07:12
资源不错,太贵了点吧啊
作者:
零游科技
时间:
2017-8-20 12:27
真心顶
作者:
xirwajim
时间:
2017-8-20 12:46
不错不错
作者:
LoveSopor
时间:
2017-8-20 12:49
说的非常好
作者:
gxzlolo
时间:
2017-8-20 12:59
LZ真是人才
作者:
reid
时间:
2017-8-20 13:03
好帖就是要顶
作者:
至大中正
时间:
2017-11-18 12:11
难得一见的好帖
作者:
qq3356441
时间:
2017-11-18 12:29
LZ真是人才
作者:
RockMyWorld
时间:
2017-11-18 12:34
楼主是超人
作者:
anjianyong
时间:
2017-11-18 12:45
很不错
作者:
maumau02
时间:
2017-11-18 12:59
好帖就是要顶
作者:
kimdo
时间:
2017-11-22 22:14
好帖就是要顶
作者:
xixicat2012
时间:
2017-11-22 22:23
难得一见的好帖
作者:
kimdo
时间:
2017-11-22 22:32
真心顶
作者:
iceboy909
时间:
2017-11-22 22:34
很好哦
作者:
litao0568
时间:
2017-11-22 22:42
楼主是超人
作者:
不期而遇
时间:
2017-12-7 19:37
说的非常好
作者:
追求自由的木偶
时间:
2017-12-7 19:49
很好哦
作者:
zhanao101
时间:
2017-12-7 20:04
楼主是超人
作者:
Harrykai
时间:
2017-12-7 20:08
真心顶
作者:
追求自由的木偶
时间:
2017-12-7 20:08
顶顶多好
作者:
兔紙
时间:
2018-1-8 17:52
很不错
作者:
18702559522
时间:
2018-1-8 18:08
好帖就是要顶
作者:
orange1438
时间:
2018-1-8 18:24
很好哦
作者:
xiaoc
时间:
2018-1-8 18:29
说的非常好
作者:
舞伴
时间:
2018-1-8 18:37
楼主是超人
作者:
fist
时间:
2018-1-15 10:02
很不错
作者:
aa393844034
时间:
2018-1-15 10:06
楼主是超人
作者:
wangsy0595
时间:
2018-1-15 10:20
顶顶多好
作者:
司令
时间:
2018-1-15 10:28
真心顶
作者:
yunshangbuhe
时间:
2018-1-15 10:30
不错不错
作者:
Anedy
时间:
2018-1-21 22:01
很好哦
作者:
chsos13
时间:
2018-1-21 22:02
顶顶多好
作者:
asuka0409
时间:
2018-1-21 22:24
说的非常好
作者:
djj06101021
时间:
2018-1-21 22:34
很不错
作者:
白鬼
时间:
2018-1-21 22:37
好帖就是要顶
作者:
Cynthia
时间:
2018-4-18 20:57
说的非常好
作者:
quietcity
时间:
2018-4-18 21:03
好帖就是要顶
作者:
xiaojiejie
时间:
2018-4-18 21:17
难得一见的好帖
作者:
foggia2004
时间:
2018-4-18 21:39
顶顶多好
作者:
chrisaa
时间:
2018-4-18 21:43
楼主是超人
作者:
fiultyy
时间:
2018-4-28 10:27
好帖就是要顶
作者:
810921141
时间:
2018-4-28 10:33
LZ真是人才
作者:
dagong
时间:
2018-4-28 11:00
不错不错
作者:
shiyusan
时间:
2018-4-28 11:03
说的非常好
作者:
ydwlhhf
时间:
2018-4-28 11:12
楼主是超人
作者:
东顺
时间:
2018-5-13 19:08
说的非常好
作者:
woxiarong
时间:
2018-5-13 19:13
顶顶多好
作者:
opqabc444
时间:
2018-5-13 19:19
难得一见的好帖
作者:
月在荒城
时间:
2018-5-13 19:34
LZ真是人才
作者:
woxiarong
时间:
2018-5-13 19:50
真心顶
作者:
a1481791043
时间:
2018-5-24 11:01
很不错
作者:
brother7
时间:
2018-5-24 11:16
说的非常好
作者:
小黄人YK君
时间:
2018-5-24 11:28
难得一见的好帖
作者:
aiying
时间:
2018-5-24 11:43
好帖就是要顶
作者:
aiying
时间:
2018-5-24 11:45
LZ真是人才
作者:
lasatboy93
时间:
2018-5-26 10:25
很不错
作者:
wuchunyin
时间:
2018-5-26 10:54
不错不错
作者:
echo2300
时间:
2018-5-26 11:05
好帖就是要顶
作者:
大傻瓜
时间:
2018-5-26 11:08
很好哦
作者:
echo2300
时间:
2018-5-26 11:19
楼主是超人
欢迎光临 Unity开发者联盟 (https://www.u3dchina.com/)
Powered by Discuz! X3.5