Unity3D - Gear VR 输入在场景之间不起作用

Unity3D - Gear VR Input doesn't work between scenes

我正在使用 Gear VR 创建一个项目,您可以在其中旋转对象并根据耳机侧面的滑动和点击控件显示信息。

一切正常,当我使用 Gear VR 侧面的触摸板时,我可以旋转和 select 东西,但是当我改变场景和 return 到主菜单时,然后回到我刚才的场景,功能停止工作。

我正在使用我制作的这个脚本:

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System;

public class GearVRTouchpad : MonoBehaviour
{
    public GameObject heart;

    public float speed;

    Rigidbody heartRb;

    void Start ()
    {
        OVRTouchpad.Create();
        OVRTouchpad.TouchHandler += Touchpad;

        heartRb = heart.GetComponent<Rigidbody>();
    }  

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            SceneManager.LoadScene("Main Menu");
        }
    }


    void Touchpad(object sender, EventArgs e)
    {
        var touches = (OVRTouchpad.TouchArgs)e;

        switch (touches.TouchType)
        {
            case OVRTouchpad.TouchEvent.SingleTap:                
                // Do some stuff    
                break;      

            case OVRTouchpad.TouchEvent.Up:
                // Do some stuff
                break;
                //etc for other directions

        }
    }
}

我注意到当我开始游戏时,会创建一个 OVRTouchpadHelper。不知道跟我的问题有没有关系。

我得到的错误是:

MissingReferenceException: The object of type 'GearVRTouchpad' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

但是我没有在其他地方引用过这个脚本。

当我在播放模式下检查我的场景时,脚本仍然存在,变量赋值仍然存在。

任何帮助都会很棒!

OVRTouchpad.TouchHandler 是一个 static EventHandler(因此它将在游戏的整个生命周期中持续存在)。您的脚本在创建时订阅它,但在销毁时不会取消订阅。当您重新加载场景时,旧订阅仍在事件中,但旧 GearVRTouchpad 实例消失了。这将导致 MissingReferenceException 下次 TouchHandler 事件触发。将此添加到您的 class:

void OnDestroy() {
    OVRTouchpad.TouchHandler -= Touchpad;
}

现在,每当具有 GearVRTouchpad 行为的 GameObject 被销毁时,OVRTouchpad 中的 static 事件将不再引用它。