UGUI implements the RepeatButton and uguirepeatbutton
Tag: added a latency. The repeate starts after the button is pressed for a period of time.
Using UnityEngine; using UnityEngine. events; using UnityEngine. eventSystems; using System. collections; public class Duration: MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler {public float interval = 0.1f; // callback trigger interval; public float delay = 1.0f; // delay time; 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) {// trigger click; 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 ;}}
When purchasing an item in a store, selling it in a backpack, or using an item, You need to press the button for a long time to quickly increase or decrease the number of items. A RepeatButton can be used in the Unity GUI, And the OnPressed callback can be used in the NGUI, but the Button in the UGUI does not have this function, you need to add it yourself.
Principle:
Process Unity click events
IPointerDownHandlerIPointerUpHandlerIPointerExitHandler
Control the status by pressing the mouse, releasing the mouse, and leaving the mouse.
Code:
Using UnityEngine; using UnityEngine. events; using UnityEngine. eventSystems; using System. collections; public class usage: MonoBehaviour, hour, hour, 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) {// trigger click; 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 ;}}
Usage:
Put the script on the Button (of course other controls can also), and then set the long-pressed callback function and call interval.
Long-press button, the specified OnLongPress function will be called continuously according to the set interval events.
Download example:
http://download.csdn.net/detail/cp790621656/8794181