Button does not work correctly when changing scene

I have a problem with fading scenes. I made an animation that faded and exited, also made a script called Fader , which has a coroutine function. The animation is beautiful. There is also an empty game object called SceneManager , which has a script. In this script function, functions that open the scene are written. enter image description here

enter image description here

But the problem is that when I press any button, for example the Scene2 Button , then disappears at the beginning of the animation, on some milliseconds , when a black screen appears, if I click on this black screen, then another scene opens. It will not open scene2 Watch this video

https://drive.google.com/file/d/0B1H5fdK2PJAnbm5fWDhlN3dVVnc

link to the package https://drive.google.com/file/d/0B1H5fdK2PJAnZ2Y1UEFRMmVFbTA

Scene Manager script:

 public class Manager : MonoBehaviour { public void GoBackScene1 () { Fader.instance.Perform ("Scene1"); } public void Scene2 () { Fader.instance.Perform ("Scene2"); } public void Scene3 () { Fader.instance.Perform ("Scene3"); } public void Scene4 () { Fader.instance.Perform ("Scene4"); } } 

Scene fader script:

 public class Fader : MonoBehaviour { public static Fader instance; [SerializeField] private GameObject canvas; [SerializeField] private Animator anim; void Awake(){ makeSingleton (); } public void Perform(string levelname) { StartCoroutine(FadeInAnimation(levelname)); } void makeSingleton(){ if (instance != null) { Destroy (gameObject); } else { instance = this; DontDestroyOnLoad(gameObject); } } IEnumerator FadeInAnimation(string level){ canvas.SetActive (true); anim.Play ("FadeIn"); yield return new WaitForSeconds (1f); Application.LoadLevel (level); anim.Play ("FadeOut"); yield return new WaitForSeconds (2f); canvas.SetActive (false); } } 
+5
source share
1 answer

That's what:

The attenuation in the length of the animation is 0.417 seconds. Between the start of the animation and LoadLevel, you wait 1 second. This means that the screen remains black for 0.583 seconds.

The problem is that during all this time all the other buttons can be clicked. Therefore, if you click "Scene 2", then click on the black screen, you can accidentally click the "Scene 3" button, which initiates FadeInAnimation("Scene3") . Since you made Fader non-destructible singleton, loading a level will not stop the execution of FadeInAnimation("Scene3") .

You need some locking so as not to press other buttons while the screen is black. For example, something like this will do the job:

 private bool fading = false; IEnumerator FadeInAnimation(string level){ if (fading) yield break; fading = true; canvas.SetActive (true); anim.Play ("FadeIn"); yield return new WaitForSeconds (1f); Application.LoadLevel (level); anim.Play ("FadeOut"); yield return new WaitForSeconds (2f); canvas.SetActive (false); fading = false; } 
+1
source

Source: https://habr.com/ru/post/1239517/


All Articles