- Home /
Display loading/progress indicator from Start()?
I'm making a simulator which involves dispersing objects randomly over the NavMesh. I have code for getting random point on the mesh, and I am caching 200 - 400 such positions in Start().
However this, along with other heavy lifting done in other Start methods, like Instantiates into object pools etc., cause a long delay at the start of the program (20-40 seconds).
What is the best practice way to provide a loading/progress indicator for work done in Start()? I already implemented the loading bar from here: http://answers.unity3d.com/questions/457594/how-to-make-a-progress-bar-for-loading-next-scene.html , but this jumps straight to 90% and then freezes while the Start() work is done (I understand this is because there's is not much in the way of assets to load, but lots happening in the Start() of scripts.
Is there a way even I can change the text in the load bar from a Start() method after calling loadLevelAsync? The scene loading script:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneSelector : MonoBehaviour {
public Texture2D white;
public Texture2D stripes;
public Texture2D window;
private AsyncOperation async;
private Object loadBarPrefab;
private GameObject loadbarObject;
private GUIStyle loadingBarStyle;
private int windowWidth = 400;
private int windowHeight = 200;
private float barWidth = 300;
private float barHeight = 50;
// Use this for initialization
void Start () {
loadBarPrefab = Resources.Load("LoadingBar");
loadbarObject = (GameObject)Instantiate(loadBarPrefab);
loadbarObject.SetActive(false);
}
public void RestartCurrentScene()
{
async = SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
}
void OnGUI()
{
if (async != null)
{
GUI.DrawTexture(new Rect((Screen.width / 2) - (windowWidth / 2), (Screen.height / 2) - (windowHeight / 2), windowWidth, windowHeight), window);
GUI.DrawTexture(new Rect((Screen.width / 2) - (barWidth / 2), (Screen.height / 2) - (barHeight / 2), barWidth, barHeight), stripes);
GUI.DrawTexture(new Rect((Screen.width / 2) - (barWidth / 2), (Screen.height / 2) - (barHeight / 2), barWidth/*maybe100*/ * async.progress, barHeight), white);
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUI.contentColor = Color.black;
GUI.Label(new Rect((Screen.width / 2) - (barWidth / 2), (Screen.height / 2) - (barHeight / 2), barWidth, barHeight), "***Loading: " + string.Format("{0:N0}%", async.progress * 100f) + "***");
}
}
}
Is it possible to pass a message from a script's Start() to a load bar/message in this way?
Thanks!
Your answer
Follow this Question
Related Questions
Loading Muiltiple Scenes Async 0 Answers
Holding SceneManager.LoadSceneAsync until a button has been pressed? 1 Answer
How to make Awake and Start get called in order with additive scenes? 1 Answer
How can I get the last frame before a new level was loaded ? 1 Answer
Load and run scene in background 1 Answer