On button click wait

by Leon Laci   Last Updated December 30, 2016 08:05 AM

I have a button I got from google material design asset pack which has a ripple animation when pressed. I'm using the button's click option in the inspector that calls a method that switches scenes. But when I tap the button it switches the scene immediately and feels unnatural since the animation hasn't finished the button ripples animation.

I tried using co-routines. Made the button call a method in which I call a IEnumerator method (since the button can't directly call a co-routine) and then switch the scene. But that doesn't work since it starts the co-routine and switches the scene, the co-routine never pauses the code and never finishes (probably because I loaded a new scene and the object that carries the script isn't transferred).

Code example:

The method that gets called when I tap a button:

 public void OnButtonTap()
 {
     StartCoroutine(Wait(0.2f));
     SceneManager.LoadScene("OnePlayerGameIntro");
 }

The wait coroutine:

 public IEnumerator Wait(float seconds)
 {
     yield return new WaitForSeconds(seconds);
 }


Answers 3


Use the Invoke method. Invoke is used to trigger another method, but it allows us to pass a float value, representing a "wait" before the method runs.

Note that Invoke takes a string for the method name. Simple type out the name of the method with no enclosing brackets inside double quotation marks, as shown, in the example. Also note that you can not call methods that take parameters, in this way.


///<summary>Opens the scene immediately.</summary>
public void OpenScene
{
    // put your logic to open the scene here
}

///<summary>Opens the scene after a 0.3 second delay.</summary>
public void OpenSceneWithWait()
{
    Invoke("OpenScene", 0.3f);
}
Gnemlock
Gnemlock
December 29, 2016 20:58 PM

If you're using Unity animations you could take advantage of Animation Events. This will allow you to trigger code at a specific point in an animation. This should allow you to be more accurate than using a co-routine or invoke method.

https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

Raeles
Raeles
December 30, 2016 07:24 AM

Here is the method with the coroutines.

 public void OnButtonTap()
 {
     StartCoroutine(LoadSceneAfterDelay("OnePlayerGameIntro", 0.2f));
 }


 public IEnumerator LoadSceneAfterDelay(string sceneName, float seconds)
 {
     yield return new WaitForSeconds(seconds);
     SceneManager.LoadScene(sceneName);
 }

Coroutines can't pause the "main thread" of Unity. A coroutine is a parallel execution of a piece of code and runs "independently" of the main thread.

Hellium
Hellium
December 30, 2016 13:27 PM

Related Questions



Dealing with multiple scenes /levels

Updated March 03, 2017 17:13 PM

Scene2d Stage Actor setup issues

Updated March 27, 2015 14:20 PM