简体   繁体   English

我如何使用统一 vr 中的事件在统一中制作暂停菜单

[英]how do i make a pause menu in unity using events in unity vr

I'm trying to make a pause menu in unity VR I want when I press a button on the controller the menu appears but I don't know how to make the menu appear when the button is pressed a picture of what I have om the event thing but I most likely did that wrong too我正在尝试在我想要的统一 VR 中制作暂停菜单,当我按下 controller 上的按钮时,菜单会出现,但我不知道如何在按下按钮时让菜单出现一张我所拥有的图片事件的事情,但我很可能也做错了

They way you have set it up currently is that when you click the button it is calling您当前设置的方式是,当您单击它正在调用的按钮时

pauseMenu.SetActive(false);

thus it will never enable that object.因此它永远不会启用 object。

You rather need a dedicated component like eg您宁愿需要一个专用组件,例如

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);
    }
}

Attach this to your pause menu object and in the event reference the PauseMenu.TogglePause method instead.将此附加到您的暂停菜单 object 并在事件中引用PauseMenu.TogglePause方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM