这是我的代码
using UnityEngine;
using System.Collections;
public class cube : MonoBehaviour {
bool start;
// Use this for initialization
void Start () {
start = false;
}
// Update is called once per frame
void Update () {
if (Input.GetTouch(0).phase == TouchPhase.Began) {
start = true;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended) {
start = false;
}
if (start) {
transform.RotateAround (Vector3.zero, Vector3.left, 1);
}
else
transform.RotateAround (Vector3.zero, Vector3.right, 1);
}
}
当触摸屏幕时,start 变量应设置为 true,立方体应开始顺时针旋转,当您松开屏幕时,它应该开始逆时针旋转,但只有在开始触摸屏幕并停止时才顺时针旋转放手时旋转。
还有一件事,当我通过触摸屏幕将变量设置为 true 时,立方体应该继续旋转,因为 start 变量现在为 true,但一旦我停止触摸屏幕它就会停止。它适用于键盘按钮,但不适用于触摸屏。
if (Input.GetTouch(0).phase == TouchPhase.Began) {
start = true;
if (start) {
transform.RotateAround (Vector3.zero, Vector3.left, 1);
}
我在这上面花了几个小时,但仍然无法理解问题出在哪里。
最佳答案
您的代码在这里:
if (Input.GetTouch(0).phase == TouchPhase.Began) {
start = true;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended) {
start = false;
}
如果屏幕上没有触摸会怎样?是的,您会收到错误消息,因为游戏正在尝试访问不存在的内容。如果您在 Unity 上模拟它,它将打印在控制台上,但由于您使用的是您自己的设备,因此您不会收到发生错误的通知。
将 touchCount
添加到您的代码中:
if (Input.touchCount > 0) {
if (Input.GetTouch (0).phase == TouchPhase.Began) {
start = true;
}
if (Input.GetTouch (0).phase == TouchPhase.Ended) {
start = false;
}
}
关于c# - 无法让 TouchPhase 在 Unity3D 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129434/