Author Topic: Tutorials on cooldown timers for buttons?  (Read 7702 times)

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Tutorials on cooldown timers for buttons?
« on: September 28, 2013, 01:42:15 PM »
Hey guys, I'm still relatively new to basically everything unity and coding related.  I've managed to get myself around pretty well, asking questions and stumbling onto great tutorial videos and such.  Right now I'm trying to really dig into NGUI,  I'm looking into doing buttons with cooldown timers and such, and so far I found this guy;

http://hompy.info/660

This is perfect, except of course, it only shows you the basics of how to get cooldown timers going.  Does anyone know of any other tutorials out there that cover this?

Thanks!





Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #1 on: September 29, 2013, 02:51:10 PM »
Well it depends on what you need.

What would you like to have tutorialized about cooldowns?

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #2 on: September 29, 2013, 07:34:42 PM »
Well I'm really confused how to use this system with anything really.

That tutorial I posted was great in that it showed me how to set up a simple progress bar.  But now how would I use that with a regular button?  How would I manipulate the events so that perhaps the cooldown didn't start unless I did something else after clicking the button?  I've tried combining a progress bar to a regular button and that didn't work out too well, unless of course, I did it wrong.

I've done of the more simple tutorials from this website and what not...but nothing on this functionality. 

LightSky

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 56
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #3 on: September 29, 2013, 10:48:11 PM »
It really depends on what you want to do. 
If you want an MMO style cooldown button then you would simply make a new sprite in front of your button and set it as filled.  Then have a script attached that when clicked it will set the sprite fill amount to 1 and Lerp the amount over the cooldown duration.
If you just want the text you can do the same thing except just Lerp a text field over the duration and output the amount instead.  :P

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #4 on: September 29, 2013, 11:58:06 PM »
That sounds pretty straight forward.  I am assuming that this sprite would need a collider attached to it to "block" your ability to click the button underneath?  And that this script would also need to disable the collider upon fill reaching 0?

I guess my only question then is, if I'm coding in java or "unity" script...how do I access the function of fill amount?  Is their documentation on this for people not using C# as to the names of the functions or variables we want to call?

Looking at the Sprite Script from NGUI, the fill amount seems to be a hidden variable? [HideInInspector][SerializeField].



« Last Edit: September 30, 2013, 06:41:02 AM by Rick74 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #5 on: September 30, 2013, 11:07:51 AM »
UISprite.fillAmount is a property. UISprite.mFillAmount is a private variable.

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #6 on: September 30, 2013, 11:37:25 AM »
Thanks Arenmook!  I just made that discovery this morning...lol!  Out of curiosity, I watched your demonstration on the in engine version of "NGUI" that you are working on for unity...is it going to be more Javascript friendly?

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #7 on: September 30, 2013, 12:02:52 PM »
Ok so I'm trying to get this to work with Javascript, this is what I have so far...

  1. #pragma strict
  2.  
  3. var btnCoolDown:        float = 0.0f;
  4. var button:                     GameObject;
  5.  
  6. var grabButton:         GameObject;
  7.  
  8. function Start ()
  9. {
  10.  
  11. }
  12.  
  13. function Update ()
  14. {
  15.         var hit : RaycastHit;                                                                                                                           // sets var for raycast
  16.         var ray = Camera.main.ScreenPointToRay ( Input.mousePosition );                                         // designates camera and mouse for raycasting
  17.        
  18.         button = GameObject.Find("Sprite (pigOff)");
  19.         btnCoolDown = button.GetComponent(UISprite).fillAmount;
  20.        
  21.         if (Physics.Raycast(ray, hit))                                                                                                          // checks for a raycast hit
  22.         {
  23.         if (hit.collider.gameObject.name == ("Sprite (pigOff)") && Input.GetMouseButton(0))
  24.         {
  25.                 print ("i hit you!");
  26.                 grabButton = hit.collider.gameObject;
  27.                 button.GetComponent(UISprite).fillAmount = 0;
  28.         }
  29.      }
  30. }

What I noticed was ray cast detection was not happening unless I moved the sprite outside of the UI 2d camera hierarchy.   Once that happened I started to get hit detection...is this right?  Or am I going in the wrong direction here?

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #8 on: September 30, 2013, 01:03:46 PM »
Update: 

I think I've narrowed this issue down to the layers of the object.  If something is on default layer, I can click on it with this script.  If it's in the GUI layer, I can't.

Created a simple cube with the same detection on it ( by name ) and if it's on default layer, it's detecting the raycast, when I switch it to GUI layer, it gets ignored. 

So when I try to move this Sprite you suggested I create to the default layer so that it can detect the raycast, I get this warning;

"You can't place widgets on a layer different than the UIPanel that manages them.
If you want to move widgets to a different layer, parent them to a new panel instead."

So I tried creating a new panel, as well as moved this Sprite to different heiarchy underneath the Camera in the UI Root (2d) object, but it's still not letting me change it off the GUI layer.

I'm probably a perfect case subject as to how new users are going to fumble around with this, lol.   :(



Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #9 on: September 30, 2013, 06:46:10 PM »
I'm still struggling trying to detect anything in the GUI layer with a raycast.  Does anyone have any experience working with NGUI through javascript that can help me out here?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #10 on: September 30, 2013, 06:48:36 PM »
Why are you doing your own raycasts? UICamera.hoveredObject already tells you what's under the mouse. And make sure "raycast hit triggers" is turned on in your physics properties.

Rick74

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Tutorials on cooldown timers for buttons?
« Reply #11 on: September 30, 2013, 06:57:54 PM »
Ok, well at least that's a point in the right direction!  I'll look into nguiCamera.hoveredObject and see how this functionality works.  I'm still new at all of this.