【问题标题】:Unity, C#: Delay on function not working? Waitforseconds?Unity,C#:功能延迟不起作用?等几秒?
【发布时间】:2016-05-01 08:17:35
【问题描述】:

我正在 Unity 中使用 Google Cardboard 创建一个 VR 应用,并且我已经成功地使用此功能从 VR 切换到普通视图:

public GameObject[] cardboardObjects;
    public GameObject[] monoObjects;
    public bool switched = false;

    // Turn on or off VR mode
    void ActivateVRMode(bool goToVR) {
        foreach (GameObject cardboardThing in cardboardObjects) {
            cardboardThing.SetActive(goToVR);
        }
        foreach (GameObject monoThing in monoObjects) {
            monoThing.SetActive(!goToVR);
        }
        Cardboard.SDK.VRModeEnabled = goToVR;

        // Tell the game over screen to redisplay itself if necessary
        //gameObject.GetComponent<GameController>().RefreshGameOver();
    }

然后我在按钮上调用此函数以切换到单视图:

public void Switch() {
        ActivateVRMode(false);
        switched = true;
        Debug.Log (switched+"No VR!");
    }

然后我想在计时器上显示单声道视图,因为一旦用户切换到非 VR 模式,它只会在切换回 VR 之前保持设定的时间(3 秒左右)。

我尝试通过翻转布尔值并使用WaitForSeconds 来做到这一点,但是视图仍然是单声道视图并且没有任何反应:

IEnumerator Switchback() 
    {
        yield return new WaitForSeconds(3); 
            ActivateVRMode(true);
            switched = false;
            Debug.Log (switched+"VR!");
    }

    void Update () {

        if (switched == true) {
            Switchback ();
        }

        //Debug.Log ("updating");

    }

我在这里做错了什么?

【问题讨论】:

  • 老兄不要忘记接受下面的答案。这就是你遇到的问题。 gjttt1 为您提供解决方案。

标签: c# unity3d


【解决方案1】:

您忘记将Switchback 函数作为协程调用

您需要在Update() 函数中调用StartCoroutine(Switchback()),而不是调用Switchback()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多