UGUI 實現Button長按效果(RepeatButton),uguirepeatbutton

來源:互聯網
上載者:User

UGUI 實現Button長按效果(RepeatButton),uguirepeatbutton

Tag:添加了一個延遲,在按鈕按下狀態一段時間後再開始 repeate

using UnityEngine;using UnityEngine.Events;using UnityEngine.EventSystems;using System.Collections;public class RepeatPressEventTrigger : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler{    public float interval = 0.1f; //回調觸發間隔時間;    public float delay = 1.0f;//延遲時間;    public  UnityEvent onLongPress = new UnityEvent();    private bool isPointDown = false;    private float lastInvokeTime;    private float m_Delay = 0f;    // Use this for initialization    void Start()    {        m_Delay = delay;    }    // Update is called once per frame    void Update()    {        if (isPointDown)        {            if ((m_Delay -= Time.deltaTime) > 0f)            {                return;            }            if (Time.time - lastInvokeTime > interval)            {                //觸發點擊;                onLongPress.Invoke();                lastInvokeTime = Time.time;            }        }    }    public void OnPointerDown(PointerEventData eventData)    {        isPointDown = true;        m_Delay = delay;    }    public void OnPointerUp(PointerEventData eventData)    {        isPointDown = false;        m_Delay = delay;    }    public void OnPointerExit(PointerEventData eventData)    {        isPointDown = false;        m_Delay = delay;    }}



在商店中購買、在背包中出售、使用一種物品的情況下,需要對按鈕進行長按處理,來快速增加或減少 物品個數。在Unity的 GUI中有一個RepeatButton可以用,在NGUI中有OnPressed 回調可以使用,但是在 UGUI 中的 Button 並沒有這種功能,就需要自己添加。


原理:

處理 Unity 的點擊事件

IPointerDownHandlerIPointerUpHandlerIPointerExitHandler


在滑鼠 按下的狀態、鬆開、以及滑鼠離開的狀態來進行狀態控制。


代碼:

using UnityEngine;using UnityEngine.Events;using UnityEngine.EventSystems;using System.Collections;public class RepeatPressEventTrigger :MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IPointerExitHandler{public float interval=0.1f;[SerializeField]UnityEvent m_OnLongpress=new UnityEvent();private bool isPointDown=false;private float lastInvokeTime;// Use this for initializationvoid Start (){}// Update is called once per framevoid Update (){if(isPointDown){if(Time.time-lastInvokeTime>interval){//觸發點擊;m_OnLongpress.Invoke();lastInvokeTime=Time.time;}}}public void OnPointerDown (PointerEventData eventData){m_OnLongpress.Invoke();isPointDown = true;lastInvokeTime = Time.time;}public void OnPointerUp (PointerEventData eventData){isPointDown = false;}public void OnPointerExit (PointerEventData eventData){isPointDown = false;}}


使用方法:

把指令碼掛在 Button 上面 (當然其它控制項也可以) ,然後設定 長按的回呼函數 以及 調用間隔。


長按按鈕,就會按照設定的間隔事件 ,不停得調用 指定的 OnLongPress 函數。


例子下載:

http://download.csdn.net/detail/cp790621656/8794181


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.