Sie sind auf Seite 1von 15

Touch

Description
Structure describing the status of a finger touching the screen.
Devices can track a number of different pieces of data about a touch on a touchscreen,
including its phase (ie, whether it has just started, ended or moved), its position and
whether the touch was a single contact or several taps. Furthermore, the continuity of a
touch between frame updates can be detected by the device, so a consistent ID number can
be reported across frames and used to determine how a particular finger is moving.
The Touch struct is used by Unity to store data relating to a single touch instance and is
returned by the Input.GetTouch function. Fresh calls to GetTouch will be required on each
frame update to obtain the latest touch information from the device but the fingerID
property can be used to identify the same touch between frames.
See Also: Input.GetTouch, TouchPhase enum.
Variables
deltaPosition The position delta since last change.
deltaTime
Amount of time that has passed since the last recorded change in Touch
values.
fingerId The unique index for the touch.
phase Describes the phase of the touch.
position The position of the touch in pixel coordinates.
tapCount Number of taps.
Touch.deltaPosition
var deltaPosition: Vector2;
Description
The position delta since last change.
The absolute position of the touch is recorded periodically and available in the position
property. The deltaPosition value is a Vector2 that represents the difference between the
touch position recorded on the most recent update and that recorded on the previous update.
The deltaTime value gives the time that elapsed between the previous and current updates;
you can calculate the touch's speed of motion by dividing deltaPosition.magnitude by
deltaTime.
Touch.deltaTime
var deltaTime: float;
Description
Amount of time that has passed since the last recorded change in Touch values.
Values for the various touch properties are updated periodically. The deltaTime value is
simply the amount of time that elapsed between the previous update and the current one.
This is primarily useful for determining the movement speed of the touch position with
reference to deltaPosition.
var fingerId: int;
Description
The unique index for the touch.
All current touches are reported in the Input.touches array or by using the Input.GetTouch
function with the equivalent array index. However, the array index is not guaranteed to be
the same from one frame to the next. The fingerID value, however, consistently refers to
the same touch across frames. This ID value is very useful when analysing gestures and is
more reliable than identifying fingers by their proximity to previous position, etc.
Touch.phase
var phase: TouchPhase;
Description
Describes the phase of the touch.
The touch phase refers to the action the finger has taken on the most recent frame update.
Since a touch is tracked over its "lifetime" by the device, the start and end of a touch and
movements in between can be reported on the frames they occur. The phase property can
be used as the basis of a "switch' statement or as part of a more sophisitcated state handling
system.

var startPos: Vector2;
var direction: Vector2;
var directionChosen: boolean;
function Update () {
// Track a single touch as a direction control.
if (Input.touchCount > 0) {
var touch = Input.GetTouch(0);
// Handle finger movements based on touch phase.
switch (touch.phase) {
// Record initial touch position.
case TouchPhase.Began:
startPos = touch.position;
directionChosen = false;
break;
// Determine direction by comparing the current touch
// position with the initial one.
case TouchPhase.Moved:
direction = touch.position - startPos;
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
directionChosen = true;
break;
}
}


if (directionChosen) {
// Something that uses the chosen direction...
}
}
Touch.position
var position: Vector2;
Description
The position of the touch in pixel coordinates.
Touch.tapCount
var tapCount: int;
Description
Number of taps.
This is intended as a way to detect "double-clicks", etc, from the finger in a particular
position. In some circumstances, two fingers may be tapped alternately and this may
incorrectly register as a single finger tapping and simultaneously moving.
Input.acceleration

static var acceleration: Vector3;
Description
Last measured linear acceleration of a device in three-dimensional space. (Read Only)
// Move object using accelerometer
var speed = 10.0;







function Update () {
var dir : Vector3 = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();

// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// Move object
transform.Translate (dir * speed);
}
Input.touchCount
static var touchCount: int;
Description
Number of touches. Guaranteed not to change throughout the frame. (Read Only)

function Update () {
if (Input.touchCount > 0) {
print(Input.touchCount);
}
}
static var touches: Touch[];
Description
Returns list of objects representing status of all touches during last frame. (Read Only)
(Allocates temporary variables).
Each entry represents a status of a finger touching the screen.
// Prints number of fingers touching the screen
function Update () {
var fingerCount = 0;
for (var touch : Touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase
!= TouchPhase.Canceled)
fingerCount++;
}
if (fingerCount > 0)
print ("User has " + fingerCount + " finger(s)
touching the screen");
}
Input.GetTouch

static function GetTouch(index: int): Touch;
Description
Returns object representing status of a specific touch. (Does not allocate temporary
variables).
// Moves object according to finger movement on the screen
var speed : float = 0.1;
function Update () {
if (Input.touchCount > 0 &&
Input.GetTouch(0).phase == TouchPhase.Moved) {

// Get movement of the finger since last frame
var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
// Move object across XY plane
transform.Translate (-touchDeltaPosition.x * speed,-touchDeltaPosition.y
* speed, 0);
}
}
Another example:
// Instantiates a projectile whenever the user taps on the screen
var projectile : GameObject;
function Update () {
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
clone = Instantiate (projectile,
transform.position, transform.rotation);
}
}
}
// Shoot a ray whenever the user taps on the screen
var particle : GameObject;
function Update () {
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// Construct a ray from the current touch
coordinates
var ray = Camera.main.ScreenPointToRay
(Input.GetTouch(i).position);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (particle,
transform.position, transform.rotation);
}
}
}
}

TouchPhase
Description
Describes phase of a finger touch.
Variables
Began A finger touched the screen.
Moved A finger moved on the screen.
Stationary A finger is touching the screen but hasn't moved.
Ended A finger was lifted from the screen. This is the final phase of a touch.
Canceled The system cancelled tracking for the touch.
The OnTouch* event handlers are all declared public virtual void and passes the Touch
parameter. Meaning, each event handler could be called multiple times per Update()
depending on how many touches there are (Input.touchCount). We can treat each touch
separately by taking note of the finger id that comes with the touch.



All the while, I thought the touch phases have the following state diagram

Unfortunately, upon testing over and over again, it is POSSIBLE to start with Moved or
Ended! I havent noticed if it could start with Stationary since I dont use it for my projects
at the moment. But my point here is that the Began phase CAN BE SKIPPED! Im not
sure if this is intentional but its happening and it got me pulling my hair for the past couple
of days.
Im using an object pool in my project where the objects react to touch. When the user
touches, an object is created, lets call that Object 0. When the user touches again, another
object is created called Object 1. If Object 0 gets disabled (as part of the game mechanic)
and the user touches again, Object 0 will be re-intialized and treated as something new.
Objects dont get destroyed, rather, they become disabled. This is basically how object
pooling works.

Touch Phase

Touching into 3D


Moving 3D object

Drag and Rotate




Acelerometro


Orientacion

Input.deviceOrientation

static var deviceOrientation: DeviceOrientation;
Description
Device physical orientation as reported by OS. (Read Only)
// Play a sound if screen is facing downwards
function Update () {
if (Input.deviceOrientation == DeviceOrientation.FaceDown)
audio.Play();
}
Description
Describes physical orientation of the device as determined by the OS.
If device is physically situated between discrete positions, as when (for example) rotated
diagonally, system will report Unknown orientation.
Variables
Unknown The orientation of the device cannot be determined.
Portrait
The device is in portrait mode, with the device held upright and the
home button at the bottom.
PortraitUpsideDown
The device is in portrait mode but upside down, with the device held
upright and the home button at the top.
LandscapeLeft
The device is in landscape mode, with the device held upright and the
home button on the right side.
LandscapeRight
The device is in landscape mode, with the device held upright and the
home button on the left side.
FaceUp
The device is held parallel to the ground with the screen facing
upwards.
FaceDown
The device is held parallel to the ground
with the screen facing downwards.

Das könnte Ihnen auch gefallen