Categories: Unity, C #, VS2015
Date Created: 2016-04-21 I. INTRODUCTION
In iOS and Android, the operation is done through touch. The method or variable for the touch operation in the input class is as follows:
With Gettouch or touches, you can access the mobile-ready touch data, and the data is stored in the touch structure. Is the structure variable of touch:
Ii. Examples of basic usage
1. Example 1 (traverse all touch and output touch information)
void Ongui ()
{
Traverse All Touch
foreach (Touch Touch in Input.touches)
{
Output Touch Information
Guilayout.label (String. Format ("finger: {0} state: {1} location: {2}", Touch.fingerid,touch.phase.tostring (), touch.position));
}
}
2. Example 2 (demo5_2_mobileexample.unity)
This example shows how to print the number of fingers of the current touch screen.
The following is the code in the MobileExample.cs file:
usingUnityengine;usingSystem.Collections; Public classmobileexample:monobehaviour{//define the number of fingers on the touch screen Public intFingercount =0; voidUpdate () {foreach(varTouchinchinput.touches) {if(Touch.phase! = touchphase.ended && Touch.phase! =touchphase.canceled) Fingercount++; } if(Fingercount >0) Print ("users have"+ Fingercount +"finger touches the screen"); } voidOngui () {//output the number of fingers on the touchscreen to the interfaceGuilayout.label ("number of fingers:"+fingercount); }}
To run the preview effect:
3. Example 3 (demo5_3_mobilemoveexample.unity)
This example shows how to move an object based on the swipe of the finger on the screen.
The code for the MobileMoveExample.cs file is as follows:
usingUnityengine;usingSystem.Collections; Public classmobilemoveexample:monobehaviour{ Public floatSpeed =0.1f; voidUpdate () {if(Input.touchcount >0&& Input.gettouch (0). Phase = =touchphase.moved) {//get the finger moving distance in this frameVector2 touchdeltaposition = Input.gettouch (0). deltaposition; //moving objects on the XY planeTransform. Translate (-touchdeltaposition.x * speed,-TOUCHDELTAPOSITION.Y * speed,0); } }}
The run preview effect is the same as in Example 2.
"Unity" 7.5 mobile device input