我有一个简单的应用程序,其中使用3D模型,我想从AssetBundle
加载它们。我为每个模型类别(例如汽车,船只,树木…)创建了多个AssetBundles
我已经做完了,但是当我开始游戏并从AssetBundle
加载所有模型时,FPS计数下降。游戏被卡住,直到加载了所有模型。
我尝试用开场视频隐藏它,但即使是视频也被卡住了。
很明显,我不能使用Threads系统,因为Unity API不是线程安全的。
我的问题是:是否有更好的方式(更有效率)来加载资产?
我的代码:
创建AssetBundle
:
[MenuItem("Assets/Create assetbundle")]
static void CreateAssetBundle()
{
// this list of categories is read from a file, and I loop through it.
// this is a bit older version, but the loop is the only difference...
AssetBundleBuild carBuild = new AssetBundleBuild();
AssetBundleBuild boatBuild = new AssetBundleBuild();
string[] paths = AssetDatabase.GetAllAssetPaths();
List<string> carAssets = new List<string>();
List<string> boatAssets = new List<string>();
foreach (var item in paths)
{
if (item.EndsWith(".ma") && item.StartsWith("Car")) // find all car models
{
carAssets.Add(item);
}
else if(item.EndsWith(".ma") && item.StartsWith("Boat")) // find all boat models
{
boatAssets.Add(item);
}
}
carBuild.assetBundleName = "cars";
carBuild.assetNames = carAssets.ToArray();
boatBuild.assetBundleName = "boats";
boatBuild.assetNames = boatAssets.ToArray();
BuildPipeline.BuildAssetBundles("Assets/Test", new AssetBundleBuild[] { carBuild, boatBuild}, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}
加载模型:
private IEnumerator LoadAssetsBundle()
{
yield return null;
List<string> categories = ReadFromAFile(); // the list is being loaded from a file
yield return null;
foreach(var category in categories)
{
using(WWW loader = WWW.LoadFromCacheOrDownload(@"file://D:\Test\"+category, 1))
{
yield return loader;
AssetBundle bundle = loader.assetBundle;
var temp = bundle.LoadAllAssetsAsync();
yield return temp;
foreach (var item in temp.allAssets)
{
Instantiate(item);
yield return null;
}
bundle.Unload(false);
yield return null;
}
}
}
编辑 这里没有结果,也没有统一的答案。 我想这是最好的选择……