我如何使用unity vr中的事件统一制作暂停菜单
how do i make a pause menu in unity using events in unity vr
我正在尝试在 unity VR 中制作一个暂停菜单我想要当我按下控制器上的按钮时菜单出现但我不知道如何在按下按钮时使菜单出现a picture of what I have om the event thing but I most likely did that wrong too
你目前设置的方式是当你点击按钮时它会调用
pauseMenu.SetActive(false);
因此它永远不会启用该对象。
您更需要一个专用组件,例如
public class PauseMenu : MonoBehaviour
{
// Reference this vis the Inspector in case the PauseMenu is NOT
// the same object this component is attached to.
// Otherwise it will simply use the same object this is attached to
[SerializeField] private GameObject pauseMenu;
// Adjust this vis the Inspector
// Shall the menu initially be active or not?
[SerializeField] private bool initiallyPaused;
// Public readonly property so you can make other scripts depend on this
// e.g. do not handle User input while pause menu is open etc
public bool IsPaused => pauseMenu.activeSelf;
// Additionally provide some events yourself so other scripts
// can add callbacks and react when you enter or exit paused mode
public UnityEvent onEnterPaused;
public UnityEvent onExitPaused;
public UnityEvent<bool> onPauseStateChanged;
private void Awake ()
{
// As fallback use the same object this component is attached to
if(!pauseMenu) pauseMenu = gameObject;
SetPauseMode(initiallyPaused);
}
// This is the method you want to call vis your event instead
public void TogglePause()
{
// simply invert the active state
SetPauseMode(!IsPaused);
}
private void SetPauseMode (bool pause)
{
pauseMenu.SetActive(pause);
if(pause)
{
onEnterPaused.Invoke();
}
else
{
onExitPaused.Invoke();
}
onPauseStateChanged.Invoke(pause);
}
}
将此附加到您的暂停菜单对象,并在事件中引用 PauseMenu.TogglePause
方法。
我正在尝试在 unity VR 中制作一个暂停菜单我想要当我按下控制器上的按钮时菜单出现但我不知道如何在按下按钮时使菜单出现a picture of what I have om the event thing but I most likely did that wrong too
你目前设置的方式是当你点击按钮时它会调用
pauseMenu.SetActive(false);
因此它永远不会启用该对象。
您更需要一个专用组件,例如
public class PauseMenu : MonoBehaviour
{
// Reference this vis the Inspector in case the PauseMenu is NOT
// the same object this component is attached to.
// Otherwise it will simply use the same object this is attached to
[SerializeField] private GameObject pauseMenu;
// Adjust this vis the Inspector
// Shall the menu initially be active or not?
[SerializeField] private bool initiallyPaused;
// Public readonly property so you can make other scripts depend on this
// e.g. do not handle User input while pause menu is open etc
public bool IsPaused => pauseMenu.activeSelf;
// Additionally provide some events yourself so other scripts
// can add callbacks and react when you enter or exit paused mode
public UnityEvent onEnterPaused;
public UnityEvent onExitPaused;
public UnityEvent<bool> onPauseStateChanged;
private void Awake ()
{
// As fallback use the same object this component is attached to
if(!pauseMenu) pauseMenu = gameObject;
SetPauseMode(initiallyPaused);
}
// This is the method you want to call vis your event instead
public void TogglePause()
{
// simply invert the active state
SetPauseMode(!IsPaused);
}
private void SetPauseMode (bool pause)
{
pauseMenu.SetActive(pause);
if(pause)
{
onEnterPaused.Invoke();
}
else
{
onExitPaused.Invoke();
}
onPauseStateChanged.Invoke(pause);
}
}
将此附加到您的暂停菜单对象,并在事件中引用 PauseMenu.TogglePause
方法。