我有一个GameSession脚本,其代码片段如下所示:
private void Awake()
{
int numGameSessions = FindObjectsOfType<GameSession>().Length;
exitDoor = GameObject.FindGameObjectWithTag("ExitDoor");
if (numGameSessions > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
pauseMenuCanvas = GameObject.FindGameObjectWithTag("PauseMenu");
pauseMenuCanvas.SetActive(false);
}
private void Update()
{
if ((Input.GetKeyDown(KeyCode.P) || Input.GetKeyDown(KeyCode.Escape)) && !pauseDisabled && currentLevelId > 0)
{
if (gamePaused)
unPauseGame();
else
PauseGame();
}
}
void PauseGame()
{
if (pauseMenuCanvas)
{
Time.timeScale = 0f;
pauseDisabled = true;
pauseMenuCanvas.SetActive(true);
if (pauseMenuPanel == null)
pauseMenuPanel = GameObject.FindGameObjectWithTag("PauseMenuPanel");
if (pauseMenuPanel)
{
pauseMenuPanel.GetComponent<CanvasGroup>().interactable = true;
if (pauseMenuPanelAnimator == null)
pauseMenuPanelAnimator = pauseMenuPanel.GetComponent<Animator>();
RuntimeAnimatorController ac = pauseMenuPanelAnimator.runtimeAnimatorController;
pauseMenuPanelAnimator.SetTrigger("Open");
}
}
gamePaused = true;
pauseDisabled = false;
}
public void unPauseGame()
{
StartCoroutine(UnPauseCoRoutine());
}
IEnumerator UnPauseCoRoutine()
{
pauseDisabled = true;
Time.timeScale = 1;
if (pauseMenuPanelAnimator)
{
pauseMenuPanelAnimator.SetTrigger("Close");
if (audioPanelAnimator == null)
{
if (audioPanel == null)
audioPanel = GameObject.FindGameObjectWithTag("AudioPanel");
if (audioPanel)
{
audioPanel.GetComponent<CanvasGroup>().interactable = false;
audioPanelAnimator = audioPanel.GetComponent<Animator>();
}
}
//if(audioPanelAnimator)
audioPanelAnimator.SetTrigger("Close");
if (helpPanelAnimator == null)
{
if (helpPanel == null)
helpPanel = GameObject.FindGameObjectWithTag("HelpPanel");
//if (helpPanel)
{
helpPanelAnimator = helpPanel.GetComponent<Animator>();
helpPanel.GetComponent<CanvasGroup>().interactable = false;
}
}
//if(helpPanelAnimator)
helpPanelAnimator.SetTrigger("Close");
//if(pauseMenuPanelAnimator)
RuntimeAnimatorController ac = pauseMenuPanelAnimator.runtimeAnimatorController;
yield return new WaitForSeconds(ac.animationClips[0].length); // use any animation from the array, as all have same length
audioPanelAnimator.SetTrigger("Idle");
helpPanelAnimator.SetTrigger("Idle");
pauseMenuPanelAnimator.SetTrigger("Idle");
if (pauseMenuCanvas)
pauseMenuCanvas.SetActive(false);
}
gamePaused = false;
pauseDisabled = false;
}
此脚本已附加到我场景中的GameSession
GameObject上。每个场景都有一个GameSession对象,但是默认情况下仅在第一个场景(主菜单场景)中启用它。在其余场景中,默认情况下将其禁用。我这样做是因为DontDestroyOnLoad
引起了一些问题。玩家只能通过从主菜单开始播放第一级,因此他们在每个场景中始终会有一个GameSession
对象。但是,出于开发目的,我通过在播放器的Start()
函数上附加以下代码段来避免这种情况。
gameSession = GameObject.FindGameObjectWithTag("GameController");
if (gameSession == null)
{
gameSession = GameObject.Find("/GameSession");
GameObject[] objs = SceneManager.GetActiveScene().GetRootGameObjects();
for (int i=0; i < objs.Length; i++){
if (objs[i].tag == "GameController")
gameSession = objs[i];
}
if (gameSession)
gameSession.SetActive(true);
}
现在的问题是,当我玩Scene1时,游戏暂停/取消暂停正在按预期方式工作。但是,如果我播放主菜单场景并按“开始”按钮,则暂停功能会中断。
这是附加到我的pausemenupanel上的动画器,该动画器在用户暂停游戏时打开。
随附的是用于说明方案的gif。当我打开暂停菜单,然后单击“帮助”按钮然后取消暂停游戏时,如果将GameSession
对象从上一级移植过来,一切都会中断。
https://imgur.com/a/AyTVe7j
如果在打开帮助面板时取消暂停游戏,则会中断后续的暂停。看起来,pauseMenupanel动画是自动从“打开”状态触发为“关闭”的,尽管在游戏暂停时没有这样的代码可以在暂停菜单面板上触发“关闭”。我还通过将日志置于暂停和取消暂停功能来确认了这一点。很久以来,我一直想把头撞在墙上:(