Sometimes a double-click event is required, and unity does not provide a double-click Control, which provides two double-click event methods, offensive references:
1) This method is implemented by Unityevent.
Start by creating a new image (or other control without a click event), add the following script, and then add a double-click method like the button control click event to set the double-click Interval to 0.5s
usingUnityengine;usingunityengine.events;usingUnityengine.eventsystems; Public classdblick1:monobehaviour,ipointerdownhandler{[Serializefield] unityevent DoubleClick=NewUnityevent (); Public floatInterval =0.5f; Private floatfirstclicked =0; Private floatsecondclicked =0; Public voidOnpointerdown (Pointereventdata eventData) {secondclicked=Time.realtimesincestartup; if(secondclicked-firstclicked<Interval) {Doubleclick.invoke (); } Else{firstclicked=secondclicked; } } //Use this for initialization voidStart () {}//Update is called once per frame voidUpdate () {}}
1) Trigger by button in the method of the button binding
Public floatInterval =0.5f; Private floatfirstclicked =0; Private floatsecondclicked =0; Public voidondoubleclicked () {secondclicked=Time.realtimesincestartup; if(Secondclicked-firstclicked <Interval) {Print ("clicked"); } Else{firstclicked=secondclicked; } }
Ps:
1) The first method is suitable for many double-click events to use, the second is suitable for only one click event when used, there is no big difference between the two, can be used, suitable or not just from the amount of code to consider
2) There is a problem in both methods, that is, when the mouse three times a continuous click, due to the logic of the problem, the event will trigger two times, you can add a variable to control, this is just to provide the idea of two double-click events
Two Unity double-click events