- Home /
Execute script when scene is visible to player
Hi all
When using scenemanager I am having differences between editor and device. I have a singleton game manager which sets up some logic and then loads next scene which is the menu scene.
On that menu scene I have a countdown script which starts at 10 down to 1. This is hooked up to a Unity UI text control and updates the correct count in the editor as expected.
However, when I check on a device the menu scene loads but there is a bigger delay and by the time I can see the new scene the countdown is already down to 4.
How can I consistently manage the transition so that I only start scripts / animations when visible to cater for the different loading speeds and behaviours between hardware?
I have tried scenemanager.loadsceneasync as well as AsyncOperation such as allowSceneActivation to false as well as checking isDone and none have the desired effect. Also OnLevelWasLoaded has an unpredictable execution order.
What would you suggest for a true "is visible to user" and "is fully loaded" check to kick off these scripts?
Have you tried to put GameObject with a script attached on it? So it's runs whenever the scene is loaded. Just my guess.
I have indeed and that is what is executing the countdown but start, update or any other such method doesn't mean (or guarantee) its available to view to the player.
I have also tried renderer.isVisible and also OnBecameVisible member on the object and still get the inconsistencies across devices.
Any suggestions?
Answer by RicoLoco · Sep 02, 2016 at 01:13 PM
I believe the counter shouldn't start until allowSceneActivation is set to true unless the counter logic is on the Singleton rather than an object of the newly loaded scene...
Do you do any sort of visual transition? At the end of a fade or something could be a good time to activate the counter.
Thanks for the comment @RicoLoco
No visual transition between scene at this stage. I tried the allowSceneActivation with code similar to this:
private void StartNow () {
AsyncOperation async = Application.LoadLevelAsync (1);
// Set this false to wait changing the scene
async.allowSceneActivation = false;
StartCoroutine (LoadLevelProgress (async));
}
IEnumerator LoadLevelProgress (AsyncOperation async) {
while (!async.isDone) {
Debug.Log ("Loading " + async.progress);
yield return null;
}
Debug.Log ("Loading complete");
// This is where I'm actually changing the scene
async.allowSceneActivation = true;
}
but put the value of async.allowSceneActivation to a variable on the singleton which I could access from my countdown object.
In that object within the update method I am checking that allowSceneActivation is true to instigate the countdown. All this works fine within the editor but skip to the device and the countdown which starts at 10 is only visible from 4 or 5.
Is this approach correct?
Application.LoadLevel has been upgraded. Here's a good explanation of the new way where you can take advantage of additive scene loading and I'm no expert but I would bet that that would solve your problem. Best advice I can offer. Good Luck.
http://www.alanzucconi.com/2016/03/23/scene-management-unity-5/
Here's something I just stumbled on that sounds like it may help, the RuntimeInitializeOnLoad$$anonymous$$ethodAttribute. It's brand new to me but I thought of your problem when I found it so thought I'd share... http://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoad$$anonymous$$ethodAttribute.html
Your answer
Follow this Question
Related Questions
Scene changing with virtual button 1 Answer
How to save changes to a scene when moving to another scene 1 Answer
Loading Scene in between two scene for web player. 0 Answers
LoadSceneOnClicks.LoadAsynchronously(int)': not all code paths return a value 1 Answer
Room change error in multiplayer 0 Answers