- Home /
Loading AssetBundle in Unity 3D drops the FPS drastically
I have a simple application that uses 3D models in it, and I want to load them from an AssetBundle. I have created multiple AssetBundles for every model category (e.g. cars, boats, trees…)
I have already done it, but when I start my game and all the models are being loaded from the AssetBundle the FPS count drops. The game gets stucked until all models are loaded.
I tried hiding it with an opening video, but even the video gets stuck.
Obviously I can't use the Threads system because Unity API is not thread safe.
My Question is: is there a better way (more efficient) to load the assets?
My Code:
Creating the 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);
}
Loading the models:
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;
}
}
}
Your answer
Follow this Question
Related Questions
Using Time efficiently regardless of current frame rate. 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers