Author Topic: Repeat Buttons NGUI  (Read 7173 times)

stety2

  • Guest
Repeat Buttons NGUI
« on: August 18, 2012, 03:04:03 AM »
How can I do Repeat Buttons in NGUI?
Thank you for your answer.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Repeat Buttons NGUI
« Reply #1 on: August 18, 2012, 03:28:37 AM »
Add a script to it that does something in Update() once it has been pressed?

stety2

  • Guest
Re: Repeat Buttons NGUI
« Reply #2 on: August 18, 2012, 03:39:21 AM »
Can you please show me an example or explain this in more detail?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Repeat Buttons NGUI
« Reply #3 on: August 18, 2012, 03:43:23 AM »
  1. using UnityEngine;
  2.  
  3. public class RepeatButton : MonoBehaviour
  4. {
  5.         public float interval = 0.25f;
  6.  
  7.         bool mIsPressed = false;
  8.         float mNextClick = 0f;
  9.  
  10.         void OnPress (bool isPressed) { mIsPressed = isPressed; mNextClick = Time.realtimeSinceStartup + interval; }
  11.        
  12.         void Update ()
  13.         {
  14.                 if (mIsPressed && Time.realtimeSinceStartup < mNextClick)
  15.                 {
  16.                         mNextClick = Time.realtimeSinceStartup + interval;
  17.  
  18.                         // Do what you need to do, or simply:
  19.                         //SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
  20.                 }
  21.         }
  22. }

stety2

  • Guest
Re: Repeat Buttons NGUI
« Reply #4 on: August 18, 2012, 04:07:18 AM »
This is not working.
Console prints only once: Touch on button

  1. using UnityEngine;
  2.  
  3. public class RepeatButtons : MonoBehaviour
  4. {
  5.     public float interval = 0.25f;
  6.  
  7.     bool mIsPressed = false;
  8.     float mNextClick = 0f;
  9.  
  10.     void OnPress (bool isPressed)
  11.         {
  12.                 mIsPressed = isPressed; mNextClick = Time.realtimeSinceStartup + interval;
  13.         }
  14.    
  15.     void Update ()
  16.     {
  17.         if (mIsPressed && Time.realtimeSinceStartup < mNextClick)
  18.         {
  19.             mNextClick = Time.realtimeSinceStartup + interval;
  20.  
  21.                         print("touch on button");
  22.         }
  23.     }
  24. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Repeat Buttons NGUI
« Reply #5 on: August 18, 2012, 04:08:32 AM »
That's probably because you have "Collapse" turned on in the console log.

stety2

  • Guest
Re: Repeat Buttons NGUI
« Reply #6 on: August 18, 2012, 04:10:19 AM »
Thank you and I'm sorry.

kruncher

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
Re: Repeat Buttons NGUI
« Reply #7 on: October 17, 2013, 06:05:00 AM »
Here is a variation which has an initial delay. Thought I would post this should it be of use to anybody :)

  1. using UnityEngine;
  2.  
  3. public class RepeatButtons : MonoBehaviour
  4. {
  5.     public float delay = 1.00f;
  6.     public float interval = 0.25f;
  7.  
  8.     bool mIsPressed = false;
  9.     float mNextClick = 0f;
  10.  
  11.     void OnPress (bool isPressed)
  12.     {
  13.         mIsPressed = isPressed;
  14.         mNextClick = Time.realtimeSinceStartup + delay;
  15.  
  16.         // Perform action as soon as button is pressed.
  17.         if (isPressed)
  18.             PerformAction();
  19.     }
  20.    
  21.     void Update ()
  22.     {
  23.         // Adjusted condition slightly...
  24.         if (mIsPressed && Time.realtimeSinceStartup >= mNextClick)
  25.         {
  26.             mNextClick = Time.realtimeSinceStartup + interval;
  27.             PerformAction();
  28.         }
  29.     }
  30.  
  31.     void PerformAction() {
  32.         print("touch on button");
  33.     }
  34. }
  35.