c# - 无法让 TouchPhase 在 Unity3D 中工作

标签 c# android ios unity-game-engine

这是我的代码

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/

相关文章:

c# - 如何使用 C# .NET 跨域设置/更改 Active Directory 用户密码?

c# - WebAPI - 在 Controller 上未找到任何操作

c# - 带或不带 .ToString() 的字符串连接?

android - 他们是如何制作这样的布局的——他们使用了哪些元素?

android - 如何在 searchView 中限制特殊字符和数字?

ios - 如何通过 API 调用使用 ChimpKit for iOS 添加到 MailChimp 列表而不验证电子邮件?

c# - Paypal 快速结帐 Asp.Net

android - 相机预览 onPictureTaken 在 android 中返回错误数据

ios - 是否可以使用 Swift 在 iOS 8 中启动另一个应用程序?

ios - 使动画播放/暂停按钮像顶空应用一样