Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Chris_E on April 15, 2012, 07:36:44 PM

Title: Double Click
Post by: Chris_E on April 15, 2012, 07:36:44 PM
Is there a way to setup a "Double Click" as in option in the button drop-down?

Thanks!
Title: Re: Double Click
Post by: ArenMook on April 15, 2012, 07:41:08 PM
Nope, you have to manage double-clicks manually. In OnClick() record the time. When the next OnClick() arrives, check the time -- has it been long enough? If not, count it as a double-click.
Title: Re: Double Click
Post by: Chris_E on April 15, 2012, 10:09:35 PM
That sounds like a real pain in the butt.  There's no easier way to do it?  Like making a double click event and add it to the dropdown to handle the listening/waiting?
Title: Re: Double Click
Post by: ArenMook on April 15, 2012, 11:53:20 PM
Pain in the butt?
  1. public class DoubleClickListener : MonoBehaviour
  2. {
  3.         float mLastClick = 0f;
  4.  
  5.         void OnClick ()
  6.         {
  7.                 if (mLastClick + 0.2f > Time.time)
  8.                 {
  9.                         SendMessage("OnDoubleClick", gameObject, SendMessageOptions.DontRequireReceiver);
  10.                 }
  11.                 mLastClick = Time.time;
  12.         }
  13. }
Title: Re: Double Click
Post by: Chris_E on April 16, 2012, 01:52:55 AM
You left out quit a bit... next I have to make a OnDoubleClick version of button message.  Then I need to find all the places where I need double click and instead of just switching the dropdown I have to remove the existing button message script, add the new OnDoubleClick button message, and add the Double Click listener, and write in the function name to be called, again.  Yes, this is a pain in the butt.
Title: Re: Double Click
Post by: Chris_E on April 16, 2012, 03:09:14 AM
Okay, so I changed it a little bit to:

if (trigger == Trigger.DoubleClick && mLastClick + 0.2f > Time.time)

And added that to the OnClick inside the ButtonMessage script.

Then I added DoubleClick to the Trigger enum.

Now I can just select DoubleClick from the dropdown menu and it works.
Title: Re: Double Click
Post by: ArenMook on April 16, 2012, 09:20:23 AM
Not a bad idea actually. I've added that to the UIButtonMessage class on my end, and it will be available in the next update.

Btw, I suggest using Time.realTimeSinceStartup to make this script work when Time.timeScale is 0.
Title: Re: Double Click
Post by: kenshin on April 22, 2012, 01:08:01 PM
That's a good idea!

I wait for this little improvement :)
Title: Re: Double Click
Post by: ArenMook on April 22, 2012, 03:13:20 PM
It's been out for a few days, you can use the OnDoubleClick trigger on the UIButtonMessage.
Title: Re: Double Click
Post by: kenshin on April 23, 2012, 02:15:01 AM
Oops, you are right!  :-[  :D
Title: Re: Double Click
Post by: simon129 on April 26, 2012, 02:37:19 AM
A suggestion to native support click and DBclick on UIImage Button

  1. StartCoroutine(TrapDoubleClick(currentPos, 0.2F));
  2.  
  3. IEnumerator TrapDoubleClick(Vector2[] firstPos, float timer)
  4.     {
  5.         float endTime = Time.time + timer;
  6.         while (Time.time < endTime)
  7.         {
  8.             if (Input.GetMouseButtonDown(0))
  9.             {
  10.                 if (GetDistance(firstPos, GetPosition())[0] < 20)
  11.                 {
  12.                     RaiseClickEvent(firstPos, true);
  13.                     yield return new WaitForSeconds(timer + 0.01f);
  14.                     clickEnable = true;
  15.                     doubleClick = true;
  16.                 }
  17.             }
  18.             yield return 0;
  19.         }
  20.  
  21.         if (!doubleClick)
  22.         {
  23.             RaiseClickEvent(firstPos, false);
  24.         }
  25.         else
  26.         {
  27.             doubleClick = false;
  28.         }
  29.         clickEnable = true;
  30.         yield return 0;
  31.     }
  32.  
Title: Re: Double Click
Post by: ArenMook on April 26, 2012, 02:44:47 AM
You're using native input here (Input class), and it's not a good idea to mix it with NGUI. You could simply do the same thing I did in the UIButtonMessage: record the time on click, if the next click arrives within the timeframe, count it as a double-click. Quite a bit less code too.
Title: Re: Double Click
Post by: ArenMook on April 26, 2012, 02:59:39 AM
I've gone ahead and added OnDoubleClick event to the UICamera, so with the next update onward you will be able to simply add:

  1. void OnDoubleClick()
  2. {
  3.     Debug.Log("Double-clicked!");
  4. }
Title: Re: Double Click
Post by: kenshin on April 26, 2012, 07:12:01 AM
Really usefull thanks! :)