Author Topic: How to implement OnDown/LongPress?  (Read 3301 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
How to implement OnDown/LongPress?
« on: January 18, 2014, 12:56:35 PM »
The behaviour I want is to fire an action based on x amount of time with the finger/mouse held down. I can create a script to read the length of a click when released just fine, for differentiating between clicks and presses based on time to release, but the NGUI events are only triggered on a change - there's no continuous feedback.

What's the best change (or even any, as I haven't found one solution yet!) I can make to add 'time_touch_is_down' to the event system. I can add time values to MouseOrTouch events but I can't figure out the notification system to send time notifications as long as a finger is down, nor can I figure out how to add a longPress parameterisation in NGUI, which seems heavily geared towards boolean events. Another approach I've tried is to get the current object's pressed time from my custom control script.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: How to implement OnDown/LongPress?
« Reply #1 on: January 18, 2014, 10:12:36 PM »
Use your own events, not NGUI. In OnPress(true), set some local flag. In OnPress(false), clear it. In Update(), check the flag -- if true, do whatever you need to do. Record the time in OnPress(true) if you need a delta, etc.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: How to implement OnDown/LongPress?
« Reply #2 on: January 19, 2014, 09:45:46 AM »
Of course. Thanks.

Ugur

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 60
    • View Profile
Re: How to implement OnDown/LongPress?
« Reply #3 on: January 19, 2014, 11:33:56 AM »
try this:
UIRepeatButton.cs :

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class UIRepeatButton : MonoBehaviour
  6. {
  7.         public float interval = 0.0001525F;
  8.        
  9.         bool mIsPressed = false;
  10.         float mNextClick = 0f;
  11.  
  12.         public List<EventDelegate> onPressListeners = new List<EventDelegate>();
  13.  
  14.        
  15.         void OnPress (bool isPressed) { mIsPressed = isPressed; mNextClick = Time.realtimeSinceStartup + interval; }
  16.        
  17.         void Update ()
  18.         {
  19.                 if (mIsPressed && Time.realtimeSinceStartup > mNextClick)
  20.                 {
  21.                         mNextClick = Time.realtimeSinceStartup + interval;
  22.                        
  23.                         // Do what you need to do, or simply:
  24.                         //SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
  25.                         //cleaner:
  26.                         EventDelegate.Execute(onPressListeners);
  27.                 }
  28.         }
  29. }
  30.  
  31.  

UIRepeatButtonEditor.cs (put into Editor folder in project assets, allows to more nicely choose the event handler for the button..)

  1.  
  2. using UnityEngine;
  3. using UnityEditor;
  4.  
  5. [CanEditMultipleObjects]
  6. [CustomEditor(typeof(UIRepeatButton))]
  7. public class UIRepeatButtonEditor : UIWidgetContainerEditor
  8. {
  9.         public override void OnInspectorGUI ()
  10.         {
  11.                 serializedObject.Update();
  12.  
  13.                 UIRepeatButton button = target as UIRepeatButton;
  14.  
  15.                 //this allows to more conveniently set the event handler:
  16.                 NGUIEditorTools.DrawEvents("On Repeated Press", button, button.onPressListeners);
  17.         }
  18.        
  19.         enum Highlight
  20.         {
  21.                 DoNothing,
  22.                 Press,
  23.         }
  24. }
  25.  
  26.  
  27.  
« Last Edit: January 21, 2014, 05:57:25 PM by Ugur »