Two Unity double-click events

Source: Internet
Author: User

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

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.