纳金网

标题: uGUI长按事件的代码 [打印本页]

作者: 王者再临    时间: 2015-7-30 22:24
标题: uGUI长按事件的代码

突然发现uGUI没有给出可以直接用的长按事件。。。上个没有注释的代码,代码说明一切。
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Events;
  4. using UnityEngine.EventSystems;

  5. public class UILongPressEvent : MonoBehaviour,IPointerDownHandler,IPointerExitHandler,IPointerUpHandler
  6. {
  7.     [SerializeField]
  8.     UnityEvent m_onLongPress = new UnityEvent();

  9.     float interval = 0.1f;
  10.     float longPressDelay = 0.5f;

  11.     private bool isTouchDown = false;
  12.     private bool isLongpress = false;
  13.     private float touchBegin = 0;
  14.     private float lastInvokeTime = 0;
  15.            
  16.         // Update is called once per frame
  17.         void Update ()
  18.     {
  19.         if (isTouchDown)
  20.         {
  21.             if (isLongpress)
  22.             {
  23.                 if (Time.time - lastInvokeTime > interval)
  24.                 {
  25.                     m_onLongPress.Invoke();
  26.                     lastInvokeTime = Time.time;
  27.                 }
  28.             }
  29.             else
  30.             {
  31.                 if (Time.time - touchBegin > longPressDelay)
  32.                 {
  33.                     isLongpress = true;
  34.                 }
  35.             }
  36.         }
  37.         }
  38.    
  39.     public void OnPointerDown(PointerEventData eventData)
  40.     {
  41.         touchBegin = Time.time;
  42.         isTouchDown = true;
  43.     }

  44.     public void OnPointerExit(PointerEventData eventData)
  45.     {
  46.         isTouchDown = false;
  47.         isLongpress = false;
  48.     }

  49.     public void OnPointerUp(PointerEventData eventData)
  50.     {
  51.         isTouchDown = false;
  52.         isLongpress = false;
  53.     }
  54. }
复制代码
反正都写了,顺便给下NGUI的长按吧。
NGUI的可以监听onPress事件;
  1. bool pressMinus = false;
  2.     bool pressPlus = false;

  3.     void LongPressMinus(GameObject sender, bool state)
  4.     {
  5.         pressMinus = state;
  6.         if (state)
  7.         {
  8.             StartCoroutine(ExcuteLongMinus());
  9.         }
  10.         else
  11.         {
  12.             CancelInvoke();
  13.         }
  14.     }

  15.     void LongPressPlus(GameObject sender, bool state)
  16.     {
  17.         pressPlus = state;
  18.         if (state)
  19.         {
  20.             StartCoroutine(ExcuteLongPlus());
  21.         }
  22.         else
  23.         {
  24.             CancelInvoke();
  25.         }
  26.     }

  27.     IEnumerator ExcuteLongMinus()    {
  28.         yield return new WaitForSeconds(0.8f);
  29.         if (pressMinus)
  30.         {
  31.             InvokeRepeating("MinusTimes", 0, 0.1f);
  32.         }
  33.     }

  34.     IEnumerator ExcuteLongPlus()    {
  35.         yield return new WaitForSeconds(0.8f);
  36.         if (pressPlus)
  37.         {
  38.             InvokeRepeating("PlusTimes", 0, 0.1f);
  39.         }
  40.     }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5