Author Topic: (solved) NGUI & long touch & gas pedal  (Read 1979 times)

winkan

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
(solved) NGUI & long touch & gas pedal
« on: July 29, 2014, 02:36:40 PM »
I was using GUITexture for gas/brake pedals for an android game but I decided to use NGUI. And it's going a little messy.
Anyway here is problem: How can I catch the long press? Below method is not working..

  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. }

------------------------------------------------------------------------
I've solved and here is solution :
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class LongTouch : MonoBehaviour
  6. {
  7.         bool mIsPressed = false;
  8.         public float interval = 0.0001525F;
  9.         float mNextClick = 0f;
  10.         public List<EventDelegate> onPressListeners = new List<EventDelegate>();
  11.         public List<EventDelegate> onNotPressListeners = new List<EventDelegate>();
  12.        
  13.        
  14.         void OnPress (bool isPressed) { mIsPressed = isPressed; mNextClick = Time.realtimeSinceStartup + interval; }
  15.        
  16.         void Update ()
  17.         {
  18.                 if (mIsPressed && Time.realtimeSinceStartup > mNextClick) {
  19.                                                 mNextClick = Time.realtimeSinceStartup + interval;
  20.                        
  21.                                                 EventDelegate.Execute (onPressListeners);
  22.                                 } else {
  23.                                                 EventDelegate.Execute (onNotPressListeners);
  24.                                 }
  25.         }
  26. }
  27.  

« Last Edit: July 29, 2014, 03:07:14 PM by winkan »