TouchPhase 触摸阶段(状态)
enumeration
枚举
Description 描述
Describes phase of a finger touch.
TouchPhase is an enum type that contains the states of possible finger touches. The states represent each action the finger can take 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.
描述手指触摸的阶段。
TouchPhase是一种枚举类型,包含可能的手指触摸状态。状态代表手指在最近的帧更新时可以执行的每个动作。由于设备会在触摸的“生命周期”内跟踪触摸,因此可以在触摸发生的帧上报告触摸的开始和结束以及之间的移动。
//Attach this script to an empty GameObject //Create some UI Text by going to Create>UI>Text. //Drag this GameObject into the Text field to the Inspector window of your GameObject.
//将此脚本附加到一个空的 GameObject //通过转到 Create>UI>Text. //将此 GameObject 拖到 GameObject的“检查器”窗口的“文本”字段中.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class TouchPhaseExample : MonoBehaviour { public Vector2 startPos; public Vector2 direction;
public Text m_Text; string message;
void Update() { //根据当前 TouchPhase 和当前方向向量 Update 屏幕上的文本(Update the Text on the screen depending on current TouchPhase, and the current direction vector) m_Text.text = "Touch : " + message + "in direction" + direction;
//跟踪一次触摸作为方向控制(Track a single touch as a direction control.) if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0);
//处理基于 TouchPhase 的手指移动(Handle finger movements based on TouchPhase) switch (touch.phase) { //当首次检测到触摸时,更改消息并记录开始位置(When a touch has first been detected, change the message and record the starting position) case TouchPhase.Began: //记录初始触摸位置(Record initial touch position.) startPos = touch.position; message = "Begun "; break;
//确定触摸是否为移动触摸(Determine if the touch is a moving touch) case TouchPhase.Moved: //通过比较当前触摸位置和初始触摸位置来确定方向(Determine direction by comparing the current touch position with the initial one) direction = touch.position - startPos; message = "Moving "; break;
case TouchPhase.Ended: //返回 触摸结束时 结束 (Report that the touch has ended when it ends ) message = "Ending "; break; } } } }
Properties 属性
| 开始时 | 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 | 系统取消了对触摸的跟踪。(系统取消触控跟踪,原因如把设备放在脸上或同时超过5个触摸点) | The system cancelled tracking for the touch. |