【问题标题】:Unity3D - Gear VR Input doesn't work between scenesUnity3D - Gear VR 输入在场景之间不起作用
【发布时间】:2017-03-02 15:19:28
【问题描述】:

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

一切都很好,当我使用 Gear VR 侧面的触摸板时,我可以旋转和选择东西,但是当我改变场景并返回主菜单,然后回到我刚刚打开的场景时,该功能停止工作。

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

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.

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

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

任何帮助都会很棒!

【问题讨论】:

  • 您的错误不在 GearVRTouchpad 类中,而是在使用 GearVRTouchpad 的类中。如果你能提供那些抛出这个异常的东西会很好。
  • 我认为可能是这种情况,但我没有在任何其他脚本或文件中使用过这个脚本?

标签: c# unity3d virtual-reality gear-vr


【解决方案1】:

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

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

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

【讨论】:

  • @Tom 乐于助人!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2022-07-18
  • 1970-01-01
相关资源
最近更新 更多