我正在嘗試在 unity VR 中制作一個暫停選單當我按下控制器上的按鈕時,我想要選單出現,但我不知道如何在按下按鈕時顯示選單我所擁有的圖片事件的事情,但我很可能也做錯了
uj5u.com熱心網友回復:
您目前設定的方式是,當您單擊按鈕時,它正在呼叫
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方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/369349.html
下一篇:我怎樣才能只用一根手指轉向輸入
