Ugui Implement button long-press effect (RepeatButton)

Source: Internet
Author: User

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)

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.