- Home /
Know when prefab has finished loading after Resources.Load
Hello everyone! I'm having an issue I'd like to solve. I don't have Unity Pro, so I cannot use AssetBundle or anything like that. I need to load multiple (heavy) prefabs into my scene. The loading process works perfectly, this is the code I'm using:
Object prefabObj = Resources.Load(path);
cellPrefab = new GameObject();
cellPrefab = (GameObject)Instantiate(prefabObj);
The object appears on the scene and I can interact with it. Problem is: some prefabs are very heavy, so Unity hangs and is not responsive for a few seconds. That's not an issue by itself, but I'd like to give the user a feedback of what's happening, something like "Loading started" and "Loading completed" on the GUI (I already have a status label). How exactly can I achieve that? Do I need to add some kind of script to the prefab?
Thank you all.
Answer by Bunny83 · May 09, 2011 at 08:21 AM
The best way would be to use a coroutine for your loading/instantiate-process to be able to give feedback after each step. Oh, by the way: you don't have to create an empty gameobject. That would just produce another empty and useless GO. Instantiate creates the whole prefab.
GameObject cellObj = null; bool loading = false;
IEnumerator LoadObj(string path) { loading = true; yield return null; // render another frame to make sure the label is drawn.
Object prefabObj = Resources.Load(path);
cellObj = (GameObject)Instantiate(prefabObj);
loading = false;
}
void Start() { StartCoroutine(LoadObj("...")); }
void OnGUI() { if (loading) { GUILayout.Label("loading..."); } }
This is exactly what I was looking for. I appreciate it, thank you very much! :)
This is a great answer, and I used it to correct an error I had.
Answer by Dreamer · May 09, 2011 at 08:05 AM
That's what loading screen in game does.
Use if(Application.isLoadingLevel) to check if a level is in loading. After loading finish you just hide the loading screen.(A full screen size 2D texture will do)
The problem is that I'm not loading a new scene, I'm loading a prefab into the current scene.
Your answer
Follow this Question
Related Questions
Load a grid of cubes in the scene view before running level? 2 Answers
Unespected rotation for Instantiate object 1 Answer
How to reduce time of instantiating a prefab that contains many objects/components in it. 1 Answer
Visualization of saved games inside a Load Menu - Problem with instantiated prefabs 0 Answers
Terrain: Use as scenario (individual) and prefab (unchanged, reusable) 0 Answers