返回介绍

Touch.phase 相位

发布于 2019-12-18 15:38:40 字数 2888 浏览 972 评论 0 收藏 0

JavaScript => public var phase: TouchPhase;
C# => public TouchPhase phase;

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.

触摸相位指的是手指最近一帧更新的动作。由于触摸是有设备跟踪它的生命周期,触摸的开始和结束以及移动在它们发生的当前帧报告。相位属性能用来判断状态或更多状态处理系统的一部分。

JavaScript:

#pragma strict
public var startPos: Vector2;
public var direction: Vector2;
public var directionChosen: boolean;
function Update() {
	// Track a single touch as a direction control.
	if (Input.touchCount > 0) {
		var touch: var = 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...
	}
}

C#:

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) {
			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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文