UGUI implements the RepeatButton and uguirepeatbutton

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.