Today, my little friend asked me how to write a piece of code to detect double-click and drag-and-drop. So I wrote this code O (∩_∩) o~
The code is as follows:
usingUnityengine;usingSystem.Collections; Public classTest2:monobehaviour { Public floatOneTime;//Detecting the effective time of a double-click Private intDowncount;//To determine whether to click or double Private BOOLCurrentcheck;//currently being detected Private BOOLIstuodong;//whether to drag Private BOOLStarttuodong;//Start dragging voidUpdate () {if(Input.getkeydown (keycode.a)) {Downcount++; Istuodong=true; if(Currentcheck = =false) {startcoroutine (Checkdown ()); } } if(Input.getkeyup (keycode.a)) {Istuodong=false; Starttuodong=false; } if(Starttuodong) {Debug.Log ("is dragging"); }} IEnumerator Checkdown () {Currentcheck=true; yield return Newwaitforseconds (oneTime); if(Istuodong) {Debug.Log ("Drag"); Starttuodong=true; }Else if(Downcount >=2)//description is double-click{Debug.Log ("Double click"); } Else if(Downcount = =1)//Click{Debug.Log ("Click"); } Downcount=0; Currentcheck=false; }}
Unity code Detection Click, double click, drag and Drop