如何能提升unity asset bundle中加载的assetbundle 的速度

加载assetbundle太慢了_unity3d吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:66,153贴子:
加载assetbundle太慢了收藏
我做了一个循环,循环读取很多assetbundle文件,但是只有第一个文件加载很快,后面就在读取数据那一步卡住了,然后很久了,之后的数据就都加载出来了
火星时代作为专业unity3d研发培训机构,依靠多年游戏教学实力,让您快速成为u3d工程师.毕业月薪&12000起&.详情请点击&&&
为啥加载完第一个,会卡很久,大概有7秒左右,之后的数据就很快的按顺序加载出来了
还没解决啊,不知道什么原因啊
是我描述不够清楚吗
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或1626人阅读
Unity(80)
Unity之Editor(63)
Unity5 AssetBundle打包以及加载
关于AssetBundle 是什么,干什么用,在网上到处是,在此就不过多赘述了。下面直接上代码,如何打包 AssetBundle,打包的AB如何加载。
using UnityE
using System.C
using System.Collections.G
using UnityE
using System.IO;
public class BuildAssetBundle : Editor {
private static string assetPath = "AllAssets";
private static string AssetBundleOutPsth = "Assets/StreamingAssets";
private static List&string& assetPathList = new List&string&();
private static Dictionary&string, string& asExtensionDic = new Dictionary&string, string& ();
[MenuItem("Assets/BuildAssetBundle")]
private static void BuildAssetBundleSource()
assetPathList.Clear ();
SetASExtensionDic();
string outPsth = bine (AssetBundleOutPsth, Plathform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));
GetDirs(Application.dataPath + "/" + assetPath);
BuildAsset (outPsth);
private static void SetASExtensionDic ()
asExtensionDic.Clear ();
asExtensionDic.Add (".prefab", ".unity3d");
asExtensionDic.Add (".mat", ".unity3d");
asExtensionDic.Add (".png", ".unity3d");
private static void GetDirs(string dirPath)
foreach (string path in Directory.GetFiles(dirPath))
if (asExtensionDic.ContainsKey(System.IO.Path.GetExtension(path)))
string pathReplace = "";
if (Application.platform == RuntimePlatform.WindowsEditor)
pathReplace = path.Replace('\\', '/');
assetPathList.Add(pathReplace);
if (Directory.GetDirectories(dirPath).Length & 0)
foreach (string path in Directory.GetDirectories(dirPath))
GetDirs(path);
private static void ClearAssetBundlesName()
int length = AssetDatabase.GetAllAssetBundleNames ().L
Debug.Log (length);
string[] oldAssetBundleNames = new string[length];
for (int i = 0; i & i++)
oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
for (int j = 0; j & oldAssetBundleNames.L j++)
AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true);
private static void BuildAsset(string outPath)
for (int i = 0; i & assetPathList.C i ++)
string asPath = assetPathList[i];
string path = "";
if (Application.platform == RuntimePlatform.WindowsEditor)
path = asPath.Substring(asPath.IndexOf("Assets/"));
AssetImporter assetImporter = AssetImporter.GetAtPath(path);
if (assetImporter == null)
Debug.LogWarning("null
" + path);
string assetName = asPath.Substring(asPath.IndexOf(assetPath));
assetName = assetName.Replace(Path.GetExtension(assetName), ".unity3d");
assetImporter.assetBundleName = assetN
if (!Directory.Exists (outPath)) {
Directory.CreateDirectory(outPath);
BuildPipeline.BuildAssetBundles (outPath, 0, EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh ();
public class Plathform
public static string GetPlatformFolder(BuildTarget target)
switch (target)
case BuildTarget.Android:
return "Android";
case BuildTarget.iOS:
return "IOS";
case BuildTarget.WebPlayer:
return "WebPlayer";
case BuildTarget.StandaloneWindows:
case BuildTarget.StandaloneWindows64:
return "Windows";
case BuildTarget.StandaloneOSXIntel:
case BuildTarget.StandaloneOSXIntel64:
case BuildTarget.StandaloneOSXUniversal:
return "OSX";
return null;
操作如下,以项目为例
在Asset下打开面板,选择 BuildAssetBundle
在 StreamingAssets文件夹下生成AB文件
打比完毕,每个资源自动打包出两个文件
打开 1.unity3d 文件如下
上面是一些哈希表对应的数值,这个不需要知道
主要看下面 Assets: 显示该资源的路径以及资源类型
Dependencies:显示该资源依赖的资源路径,可以看到该预设依赖的资源为一个材质资源
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
下面是加载,封装了一下加载代码如下
using UnityE
using System.C
using System.Collections.G
using System.IO;
public delegate void CallBack(UnityEngine.Object obj);
public class LoadAssetBundle{
public static readonly LoadAssetBundle Instance = new LoadAssetBundle();
private string path = Application.streamingAssetsP
private Dictionary&string, AssetBundle& assetBundleRefDic = new Dictionary&string, AssetBundle&();
private string GetThePathWithPlathform
if (Application.isEditor)
return "file://" +
public IEnumerator Load(string assetPath, string assetName, Type type, CallBack callBack, bool isUnload)
string loadpath = GetThePathWithP
string url = bine(loadpath, assetPath);
AssetBundle ab = GetASFromUrl (url);
if (ab != null) {
GetObject (ab, assetName, type, callBack, isUnload);
yield return null;
WWW www = WWW.LoadFromCacheOrDownload (url, 0);
yield return
if (!string.IsNullOrEmpty (www.error)) {
Debug.LogError (www.error);
yield return null;
ab = www.assetB
GetObject ( ab, assetName, type, callBack, isUnload);
if (!isUnload)
if (!assetBundleRefDic.ContainsKey(url))
Debug.Log("add");
assetBundleRefDic.Add(url, ab);
private void GetObject(AssetBundle ab, string assetName, Type type, CallBack callBack, bool isUnload)
UnityEngine.Object obj = ab.LoadAsset (assetName, type);
if (callBack != null)
callBack(obj);
if (isUnload) {
ab.Unload (true);
private AssetBundle GetASFromUrl(string url)
AssetBundle ab = null;
if (assetBundleRefDic.TryGetValue (url, out ab)) {
return null;
using UnityE
using System.C
public class Test : MonoBehaviour {
void Update () {
if (Input.GetKeyDown (KeyCode.A))
string loadPath = "Android/allassets/gameobject.unity3d";
string asName = "gameobject";
StartCoroutine(LoadAssetBundle.Instance.Load(loadPath, asName, typeof(GameObject), CallBack, true));
if (Input.GetKeyDown (KeyCode.D))
string loadPath = "Android/allassets/gameobject.unity3d";
string asName = "gameobject";
StartCoroutine(LoadAssetBundle.Instance.Load(loadPath, asName, typeof(GameObject), CallBack, false));
private void CallBack(UnityEngine.Object obj)
Debug.Log (obj.name);
GameObject.Instantiate ((GameObject)obj);
运行项目点击键盘 A 键,将资源加载出来
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:196045次
积分:4057
积分:4057
排名:第6357名
原创:216篇
评论:39条
(1)(3)(5)(13)(4)(1)(3)(7)(13)(1)(9)(11)(11)(7)(130)}

我要回帖

更多关于 unity asset bundle 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信