Author Topic: 'Long Press' functionality in NGUI?  (Read 9594 times)

jaz1974

  • Guest
'Long Press' functionality in NGUI?
« on: September 23, 2013, 04:03:45 AM »
Hi,

Anyone know if there is a way of determining when the user has pressed on a button for more than a specific duration?
I'm trying to figure out how I can make something draggable if its been pressed on for more than 1 sec.
Any advice is greatly appreciated.

Cheers!

Jaz

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: 'Long Press' functionality in NGUI?
« Reply #1 on: September 23, 2013, 07:07:44 AM »
Record the time in OnPress(true), clear it in OnPress(false), check the duration in Update.

jaz1974

  • Guest
Re: 'Long Press' functionality in NGUI?
« Reply #2 on: September 23, 2013, 07:24:25 AM »
Hey ArenMook,

Thanks for the info.
Whilst I was waiting I'd created something using 'Time.fixedTime'.
Is this similar to your suggestion?  Perhaps the OnPress(true/false) is more efficient?
Here's the code I created :

#pragma strict

var timeCurrent : float;
var timeAtButtonDown : float ;
var timeAtButtonUp : float ;
var timeButtonHeld : float = 0 ;
var draggable : boolean = false;

function Start () {

}

function Update () {

   timeCurrent = Time.fixedTime;
   
}

function OnMouseDown()
{
   timeAtButtonDown = timeCurrent;
   Debug.Log ("Time button pressed" + timeAtButtonDown);
}

function OnMouseUp()
{
   timeAtButtonUp = timeCurrent;
   Debug.Log ("Time button released" + timeAtButtonUp);
   
   timeButtonHeld = (timeAtButtonUp - timeAtButtonDown);
   Debug.Log ("TimeButtonHeld = " + timeButtonHeld);
   
   if (timeButtonHeld > 2)
   {
   draggable = true;
   Debug.Log ("Yeah, you held the mouse for longer than 2 secs!!");
   }
}

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: 'Long Press' functionality in NGUI?
« Reply #3 on: September 23, 2013, 07:25:47 AM »
Don't use OnMouse* events. Ever. They're going away altogether, and they're Unity's events, not NGUI's. Use OnPress.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: 'Long Press' functionality in NGUI?
« Reply #4 on: September 23, 2013, 11:45:45 AM »
Also, don't use UnityScript. It sucks.

  1. private float _longClickDuration = 2f;
  2. float _lastPress = -1f;
  3.  
  4. void OnPress(bool pressed)
  5. {
  6.   if (pressed)
  7.     _lastPress = Time.realtimeSinceStartup;
  8.   else
  9.   {
  10.     if (Time.realtimeSinceStartup - _lastPress > _longClickDuration)
  11.        DoLongPress();
  12.   }
  13.  
  14. }
  15.  
« Last Edit: September 23, 2013, 02:16:46 PM by Nicki »

jaz1974

  • Guest
Re: 'Long Press' functionality in NGUI?
« Reply #5 on: September 25, 2013, 07:28:25 AM »
Great stuff!
Thanks for the help guys.

Jaz