描述触摸的阶段。
触摸 phase
指的是手指在最近一帧更新中执行的操作。由于设备会跟踪触摸的整个“生命周期”,因此可以在触摸开始和结束以及之间的移动发生的那一帧中报告这些操作。 phase
属性可以用作“switch”语句的基础,或用作更复杂的状体处理系统的一部分。
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Vector2 startPos; public Vector2 direction; public bool directionChosen; void Update() { // Track a single touch as a direction control. if (Input.touchCount > 0) { Touch 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... } } }