"Unity" 7.5 mobile device input

Source: Internet
Author: User

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

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.