When you buy in a store, sell in a backpack, and use an item, you need to press and hold the button to quickly increase or decrease the number of items. There is a RepeatButton in Unity's GUI that can be used in the Ngui, onpressed callbacks are available, but the Button in the Ugui does not have this function, it needs to be added by itself.
Principle:
Handle Unity's Click events
Ipointerdownhandleripointeruphandleripointerexithandler
State control in the state of the mouse press, release, and the state of the mouse left.
Code:
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 ();p rivate 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;}}
How to use:
Hang the script on top of the Button (and of course other controls), and set the long-pressed callback function and the call interval.
When you press and hold the button, the specified onlongpress function is not kept in accordance with the set interval event.
Example Download:
http://download.csdn.net/detail/cp790621656/8794181
Ugui Implement button long-press effect (RepeatButton)