- Home /
Will using Resources.LoadAsync() to load Prefabs reduce loading times in comparison to just referencing Prefabs in the Inspector?
Unity unadvises using Resources.Load()
to load Prefabs as opposed to just referencing them:
In Unity you usually don't use path names to access assets, instead you expose a reference to an asset by declaring a member-variable, and then assign it in the inspector. When using this technique Unity can automatically calculate which assets are used when building a player. This radically minimizes the size of your players to the assets that you actually use in the built game. When you place assets in "Resources" folders this can not be done, thus all assets in the "Resources" folders will be included in a build.
http://docs.unity3d.com/ScriptReference/Resources.html
I need to load several things, but I don't require downloading things from my server like AssetBundles offers. I do, however, have several prefabs that not all users will use. If I use the LoadASync()
method instead of just using a reference, will it take less time for my level to load?
Alternative solutions to the problem also welcome :^)
Answer by valentin-simian · Aug 19, 2015 at 06:11 AM
I found the fastest way to load multiple prefabs, of which only several may be used, is to incrementally load them by AssetBundles.
Even though the Unity Manual documentation implies their use by downloading AssetBundles from a server, it is possible to load them from drive using the file protocol via the WWW class.
Code for iOS, mostly cannibalized from @Dreamora:
using UnityEngine;
using System.Collections;
public class AssetLoader : MonoBehaviour {
void Awake ()
{
StartCoroutine(LoadBundle());
}
IEnumerator LoadBundle () {
WWW test = new WWW("file://"+Application.dataPath + "/Raw" + "/YourAssetBundleName.unity3d");
yield return test;
Debug.Log(test + " WWW Test");
// Load and retrieve the AssetBundle
AssetBundle bundle = test.assetBundle;
Debug.Log(bundle + " BUNDLE");
// Load the object asynchronously
AssetBundleRequest request = bundle.LoadAssetAsync ("your_game_object", typeof(GameObject));
// Wait for completion
yield return request;
// Get the reference to the loaded object
GameObject obj = request.asset as GameObject;
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
test.Dispose ();
}
}
Answer by YoungDeveloper · Aug 18, 2015 at 07:08 AM
Resources load is a much heavier operation, as data is read from drive. Accessing something by serialized variable reference is much faster.
Implying that the referenced prefabs are loaded directly into RA$$anonymous$$? Arguably faster, as having less RA$$anonymous$$ will make further operations take more time to perform.
I'm reading elsewhere that I can load AssetBundles from drive via the file protocol using the WWW class. This way I can load and unload assets on request ins$$anonymous$$d of on load.
Will keep posted on final code.
Your answer
Follow this Question
Related Questions
Resources.Load() What has to be in Resources Folder 1 Answer
Eliminating prefabs from resources folder? 1 Answer
About to gut my use of the Resources folder. What are the best practices going forward? 1 Answer
Saving a level with the help of prefabs 3 Answers
What does Unity actually do when Loading Resources that already exist? 0 Answers