首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在VR开发中尝试使用协同技术激活和禁用场景中的对象

在VR开发中尝试使用协同技术激活和禁用场景中的对象
EN

Stack Overflow用户
提问于 2018-08-15 23:39:23
回答 1查看 512关注 0票数 0

我正在设计一个虚拟现实游戏,我试图制作一个菜单,连接到左边的控制器。我有两个按钮,一个是用来激活场景中的几个物体,另一个是用来把玩家带到另一个场景。

我一直在看SteamVR互动示例,我试图模仿它们在示例场景中使用的按钮。除了在示例中,它们使用Coroutine在控制器上启动不同的提示,因此它们使用了Hand类。我只是试着用它来激活和停用物体,更多的是到另一个场景。

为了让事情清楚,这些是我想要实现的:

  1. 当用户点击第一个按钮时,激活通过团结中的检查选项卡指定的对象(obj1)。
  2. 当用户点击第二个按钮时,关闭对象(obj1)并移动到另一个场景。

下面是示例场景中的C#代码。它有三个按钮,它们要么激活“按钮提示”,要么激活“文本提示”:

代码语言:javascript
复制
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: Demonstrates the use of the controller hint system
//
//=============================================================================

using UnityEngine;
using System.Collections;
using Valve.VR;

namespace Valve.VR.InteractionSystem
{
    //-------------------------------------------------------------------------
    public class ControllerHintsExample : MonoBehaviour
    {
        private Coroutine buttonHintCoroutine;
        private Coroutine textHintCoroutine;

        //-------------------------------------------------
        public void ShowButtonHints( Hand hand )
        {
            if ( buttonHintCoroutine != null )
            {
                StopCoroutine( buttonHintCoroutine );
            }
            buttonHintCoroutine = StartCoroutine( TestButtonHints( hand ) );
        }


        //-------------------------------------------------
        public void ShowTextHints( Hand hand )
        {
            if ( textHintCoroutine != null )
            {
                StopCoroutine( textHintCoroutine );
            }
            textHintCoroutine = StartCoroutine( TestTextHints( hand ) );
        }


        //-------------------------------------------------
        public void DisableHints()
        {
            if ( buttonHintCoroutine != null )
            {
                StopCoroutine( buttonHintCoroutine );
                buttonHintCoroutine = null;
            }

            if ( textHintCoroutine != null )
            {
                StopCoroutine( textHintCoroutine );
                textHintCoroutine = null;
            }

            foreach ( Hand hand in Player.instance.hands )
            {
                ControllerButtonHints.HideAllButtonHints( hand );
                ControllerButtonHints.HideAllTextHints( hand );
            }
        }


        //-------------------------------------------------
        // Cycles through all the button hints on the controller
        //-------------------------------------------------
        private IEnumerator TestButtonHints( Hand hand )
        {
            ControllerButtonHints.HideAllButtonHints( hand );

            while ( true )
            {
                ControllerButtonHints.ShowButtonHint( hand, EVRButtonId.k_EButton_ApplicationMenu );
                yield return new WaitForSeconds( 1.0f );
                ControllerButtonHints.ShowButtonHint( hand, EVRButtonId.k_EButton_System );
                yield return new WaitForSeconds( 1.0f );
                ControllerButtonHints.ShowButtonHint( hand, EVRButtonId.k_EButton_Grip );
                yield return new WaitForSeconds( 1.0f );
                ControllerButtonHints.ShowButtonHint( hand, EVRButtonId.k_EButton_SteamVR_Trigger );
                yield return new WaitForSeconds( 1.0f );
                ControllerButtonHints.ShowButtonHint( hand, EVRButtonId.k_EButton_SteamVR_Touchpad );
                yield return new WaitForSeconds( 1.0f );

                ControllerButtonHints.HideAllButtonHints( hand );
                yield return new WaitForSeconds( 1.0f );
            }
        }


        //-------------------------------------------------
        // Cycles through all the text hints on the controller
        //-------------------------------------------------
        private IEnumerator TestTextHints( Hand hand )
        {
            ControllerButtonHints.HideAllTextHints( hand );

            while ( true )
            {
                ControllerButtonHints.ShowTextHint( hand, EVRButtonId.k_EButton_ApplicationMenu, "Application" );
                yield return new WaitForSeconds( 3.0f );
                ControllerButtonHints.ShowTextHint( hand, EVRButtonId.k_EButton_System, "System" );
                yield return new WaitForSeconds( 3.0f );
                ControllerButtonHints.ShowTextHint( hand, EVRButtonId.k_EButton_Grip, "Grip" );
                yield return new WaitForSeconds( 3.0f );
                ControllerButtonHints.ShowTextHint( hand, EVRButtonId.k_EButton_SteamVR_Trigger, "Trigger" );
                yield return new WaitForSeconds( 3.0f );
                ControllerButtonHints.ShowTextHint( hand, EVRButtonId.k_EButton_SteamVR_Touchpad, "Touchpad" );
                yield return new WaitForSeconds( 3.0f );

                ControllerButtonHints.HideAllTextHints( hand );
                yield return new WaitForSeconds( 3.0f );
            }
        }
    }
}

这就是我所拥有的,它是示例代码的类似副本。我被困在找出对象的激活上了,还没有接触到场景的变化:

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Valve.VR;

namespace Valve.VR.InteractionSystem
{
    //................................................
    public class HandMenueActions : MonoBehaviour
    {
        public GameObject AllProducts;

        private Coroutine allProductsCoroutine;
        private Coroutine aboutUsCoroutine;

        //............................................
        public void ShowAllProducts(GameObject AllProducts)
        {
            if (allProductsCoroutine != null)
            {
                StopCoroutine(allProductsCoroutine);
            }
            allProductsCoroutine = StartCoroutine(TestAllProducts);
        }


        //............................................
        //Turns on all products in the scene
        //............................................
        private IEnumerable TestAllProducts()
        {
            AllProducts.SetActive(true);
        }
    }

我并不是很有经验,我试着为统一编写代码,文档就在它旁边打开,但我仍然在这里。

EN

回答 1

Stack Overflow用户

发布于 2018-08-16 05:58:27

我不知道你为什么要用协同效应来代替简单的方法。

老实说,我不知道具体的Valve.VR.InteractionSystem

但我只会做一些简单的方法,比如:

代码语言:javascript
复制
public GameObject obj1;

private void Update()
{
    if(Input.GetButton("Button1") // or however you access your button 1
    {
        obj1.SetActive(true);
    } else if (Input.GetButton("Button2") // or however you access your button2
    {
        obj1.SetActive(false);
    }
}

我用的是其他的,所以只有一个按钮是同时执行的。

在快速搜索中,我现在也找到了一个SteamVr控制器输入统一手册

他们有一个使用事件处理程序/回调的完整示例。这里有一个简短的,应该适合你的需要(他们也很好地解释了它是如何工作的)

代码语言:javascript
复制
using UnityEngine; 

public class ObjectEnabler : MonoBehaviour
{   
    private SteamVR_TrackedController _controller;  

    public GameObject obj1;

    private void OnEnable() 
    {       
        // Get the controller this component is attached to
        _controller = GetComponent<SteamVR_TrackedController>();    

        // Register callbacks to your buttons
        // I've left this on the ones from the example .. if you need others 
        // You have to look them up
        // In the example there is also a complete list of all button events
        RegisterCallbacks();
    }   

    private void OnDisable()    
    {       
        UnregisterCallbacks();
    }

    // Register callbacks for controler events
    private void RegisterCallbacks()
    {
        // It is always save to remove callbacks 
        // So just to be sure we only register once so methods are not executed twice
        // I personaly always first unregister callbacks:
        UnregisterCallbacks();

        // now I register the callback that means e.g.
        // Everytime the event TriggerClicked is 
        // invoked anywhere in the Scene I want to execute HandleTrickerClicked
        // hint the method can be called as you like .. could also simply be EnableObject e.g.
        _controller.TriggerClicked += HandleTriggerClicked;     
        _controller.PadClicked += HandlePadClicked; 
        // For more information how the callbacks work
        // see the link I provided
    }

    // Unregister callbacks for events
    private void UnregisterCallbacks()
    {
        _controller.TriggerClicked -= HandleTriggerClicked;     
        _controller.PadClicked -= HandlePadClicked; 
    }

    // Called on TriggerClicked
    // Enables the obj1
    private void HandleTriggerClicked() 
    {
        obj1.SetActive(true);
    }   

    // Called on PadClicked
    // Disables the obj1
    private void HandlePadClicked()
    {
        obj1.SetActive(false);
    } 
}

现在,您只需将此脚本附加到控制器( SteamVR_TrackedController组件旁边),它应该可以满足您的需要。

(我现在不确定,因为我从未使用过它,而且我正在移动,但他们的脚本最终可能会错过一些using名称空间,例如,从整个using Valve.VR;开始,但我不知道这是如何实现的)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51867817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档