最近在參與AssetBundle劃分策略的討論,關於AssetBundle的細分方面,記得上個項目的時候主程說過,IOS有一個限制文件句柄的問題,還有5.3之前Android的SerializeFile每一個都是512K,不過這些問題在新的版本中都已經解決,但是是否bundle越大越好,或者是越細碎越好呢?簡單做了一個測試,將大概80個圖片單打成一個bundle大概1M以及打成80個不同的bundle,爲求結果的準確性,測試2000次:
測試代碼如下:
if (GUI.Button(new Rect(100, 200, 100, 50), "LoadOneByOne"))
{
string[] tempArr = bbb.Split(',');
float totalTime = 0;
Dictionary<string, AssetBundle> tempDic = new Dictionary<string, AssetBundle>();
for (int i = 0; i < 2000; i++)
{
for (int j = 0; j < tempArr.Length; j++)
{
float t = Time.realtimeSinceStartup;
AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + tempArr[j].ToLower());
totalTime += (Time.realtimeSinceStartup - t) * 1000;
assetBundle.Unload(true);
}
System.GC.Collect();
Resources.UnloadUnusedAssets();
}
Debug.LogError(string.Format("*****AssetBundle Assets Load Time {0}:{1}", "LoadAssetOneByOne", totalTime));
resultStr += string.Format("*****AssetBundle Assets Load Time {0}:{1}", "LoadAssetOneByOne", totalTime);
}
if (GUI.Button(new Rect(100, 300, 100, 50), "LoadAssetTotal"))
{
float totalTime = 0;
for (int i = 0; i < 2000; i++)
{
float t = Time.realtimeSinceStartup;
AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/commatlas1");
totalTime += (Time.realtimeSinceStartup - t) * 1000;
assetBundle.Unload(true);
assetBundle = null;
System.GC.Collect();
Resources.UnloadUnusedAssets();
}
Debug.LogError(string.Format("*****AssetBundle Assets Load Time {0}:{1}", "LoadAssetTotal", totalTime));
resultStr += "\n" + string.Format("*****AssetBundle Assets Load Time {0}:{1}", "LoadAssetTotal", totalTime);
}
}
PC測試結果:
加載一次時間:
單個bundle:80個bundle加載一次時間16.57ms,平均加載一個0.20ms
打成一個bundle:加載一次0.895ms
加載一次時間:
單個bundle:80個bundle加載一次時間61.20ms,平均加載一個0.76ms
打成一個bundle:加載一次5.0ms
由此可見bundle過大或者是過碎,必定會引起加載時間的增加,合理的劃分策略必然會提高遊戲性能