【发布时间】:2018-08-15 23:39:23
【问题描述】:
我正在设计一款 VR 游戏,并尝试制作一个附加到左侧控制器的菜单。我有两个按钮,一个用于激活场景中的一些对象,另一个用于将玩家带到不同的场景。
我一直在查看 SteamVR 交互式示例,并试图模仿他们在示例场景中使用的按钮。除了在示例中,他们使用协程在控制器上启动不同的提示,因此他们使用 Hand 类。我只是想用它来激活和停用对象以及更多到不同的场景。
为了说明清楚,这些是我想要实现的目标:
- 当用户点击第一个按钮时,激活通过 Unity 中的 Inspector 选项卡指定的对象 (obj1)。
- 当用户点击第二个按钮时,停用对象 (obj1) 并移动到不同的场景。
这是示例场景中的 C# 代码。它有三个按钮,它们可以激活“按钮提示”或“文本提示”:
//======= 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 );
}
}
}
}
这就是我所拥有的示例代码的类似副本。我一直在搞清楚对象激活,还没有接触到场景变化:
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);
}
}
我没有那么有经验,我尝试编写代码以实现统一,并在旁边打开文档,但我仍然在这里。
【问题讨论】:
标签: c# unity3d virtual-reality coroutine