查看: 1502|回复: 0
打印 上一主题 下一主题

[其他] uGUI长按事件的代码

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-7-30 22:24:50 |只看该作者 |倒序浏览

突然发现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.     }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-29 06:03 , Processed in 0.082063 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部