Author Topic: Add Delay to UIEventTrigger  (Read 4914 times)

Unknowns

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 3
    • View Profile
Add Delay to UIEventTrigger
« on: June 23, 2014, 11:23:10 AM »
Hi,

I have a script that controls all the buttons:

public void NameButon1 () {}
public void NameButton2 () {}
etc...

The Script work well, but when pressed directly loaded the scene, cutting the sound of the "click" and the "Tween Scale", these button are "UI2DSprite" controlled by a "UIEvent Trigger" How can add a delay to those "buttons" in "On Relase" for example?

Thanks!.  :)

spooky

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 16
    • View Profile
Re: Add Delay to UIEventTrigger
« Reply #1 on: June 23, 2014, 12:11:06 PM »
I'm not sure exactly what you mean, but if you're trying to load a scene at a button press, but want the animation and sound to finish before doing so, you could try an Update loop or a coroutine WaitForSeconds(); you can look up how to do coroutines in the Unity documentation. Although that might be a little elaborate for your needs.

The simpler solution might be the update loop. It's probably not ideal, since coroutines are probably more efficient, but it's easier to implement:
  1. Update ()
  2. {
  3.    if (Time.time > nextTime)
  4.    {
  5.       Application.LoadLevel(levelToLoad);
  6.    }
  7. }
  8.  
  9. Button ()
  10. {
  11.    nextTime = Time.time + 1.0f; //delay in seconds
  12.    levelToLoad = 0; //level you want to load
  13. }
  14.  

Just set your delay as long as you need to complete the tween and audio. :)

Unknowns

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Add Delay to UIEventTrigger
« Reply #2 on: June 23, 2014, 01:44:55 PM »
I'm not sure exactly what you mean, but if you're trying to load a scene at a button press, but want the animation and sound to finish before doing so, you could try an Update loop or a coroutine WaitForSeconds(); you can look up how to do coroutines in the Unity documentation. Although that might be a little elaborate for your needs.

The simpler solution might be the update loop. It's probably not ideal, since coroutines are probably more efficient, but it's easier to implement:
  1. Update ()
  2. {
  3.    if (Time.time > nextTime)
  4.    {
  5.       Application.LoadLevel(levelToLoad);
  6.    }
  7. }
  8.  
  9. Button ()
  10. {
  11.    nextTime = Time.time + 1.0f; //delay in seconds
  12.    levelToLoad = 0; //level you want to load
  13. }
  14.  

Just set your delay as long as you need to complete the tween and audio. :)

Thank You!.  :)

I try to explain a little more detailed, but is more or less as you say.

I have a  "ButtonControl" Script that I made to control all my buttons from a centralized location:

Example:

  1. public void buttonPlay (){
  2.  
  3. Application.LoadLevel("Play");
  4.  
  5. }
  6. public void buttonAbout (){
  7.  
  8. Application.LoadLevel("About");
  9.  
  10. }
  11. public void buttonGooglePlus (){
  12.  
  13. Application.OpenURL("");
  14.  
  15. }

A "button" is composed by:



The "UIEventTrigger" - "On Release", notify to the  "ButtonControl" Script and  "SoundEffects" Script, the first do something (Load a level, open a url, etc...), the second play a "click" sound, but when is pressed the "Tween Scale" and the Sound are cut off, I want to add a delay to prevent this.

I know how to add the delay (Invoke, coroutine WaitForSeconds(); as you say, etc...),  but not how to keep control of all buttons in a single Script with these methods. (Not even know if it's possible.)

 :)

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Add Delay to UIEventTrigger
« Reply #3 on: June 23, 2014, 02:41:00 PM »
  1. void OnClick()
  2. {
  3. Invoke("LoadNow", 0.5f); //time in seconds.
  4. }
  5.  
  6. void LoadNow()
  7. {
  8. //do loading magic.
  9. }
  10.  

spooky

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 16
    • View Profile
Re: Add Delay to UIEventTrigger
« Reply #4 on: June 23, 2014, 03:16:48 PM »
You should be able to delay the commands in that script. Invoke(Application.LoadLevel("Play"), 1.0f); should work. You wouldn't need to move anything into separate scripts.

Unknowns

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Add Delay to UIEventTrigger
« Reply #5 on: June 23, 2014, 03:44:11 PM »
Thanks to you both.  :)