Input for Unity Android devices
1 based on screen position input
Sometimes it is for the whole of some style of clean, reduce the screen UI icon, so that the virtual button to abandon the usual input method. In order to replace the input mode of the virtual key, the movement of the object is controlled according to the location of the click event in the screen. For example, the entire phone screen is divided into two areas, the left area is responsible for mobile control, the right area responsible for the release of skills.
Here are the issues we need to deal with are click event Acquisition and screen information acquisition, the following two aspects of the API documentation.
Click event: Http://wiki.ceeger.com/script/unityengine/classes/touch/touch
Screen information: Http://wiki.ceeger.com/script/unityengine/classes/screen/screen
1 voidUpdate () {2 3 if(Input.touchcount >0)//determine if there is touch information4 {5 for(inti =0; i < Input.touchcount; i++)6 {7Touch touch = input.touches[i];//passing touch information to touch8 9 if(touch.phase = = Touchphase.stationary | | touch.phase = = touchphase.moved)//Touch.phase is the phase information of the touch, determined by the Touchphase enumeration to determine the current state, began: one finger begins to touch; Moved: one finger moves; stationary: one finger touches the screen but no movement occurs; Ended : A finger leaves the screen and is the final state; cnaceled: The system cancels the touch monitoring, such as when the user puts the screen on his face or more than five contacts simultaneously. Ten { OneTouchposition = touch.position;//Gets the location information for the touch, which is consistent with the screen coordinates. A if(Touchposition.x < screenwidth/4)//ScreenWidth has been assigned =>screenwidth = Screen.width in start; - { -SomeMethod1 ();//a Method 1 theBeetransform.translate (NewVector3 (-movespeed,-downspeed,0), Space.world);//Beetransform is the location information of the game object and has been assigned in start - } - Else if(touchposition.x>=screenwidth/4&& touchposition.x <screenwidth/2) - { + SomeMethod1 (); -Beetransform.translate (NewVector3 (Movespeed,-downspeed,0), Space.world); + A } at } - Else if(Touch.phase = =touchphase.ended) -SOMEMETHOD2 ();//a method - } - } - in if(Input.touchcount = =0)//when not touched - SomeMethod2 (); to}
Input for Unity Android devices