Don';我不知道如何正确使用SetActive()

本文关键字:SetActive 何正确 我不知道 Don | 更新日期: 2024-09-23 11:46:21

所以,我编写的脚本不能完全工作。我有一个暂停按钮,当我按下它时,它会触发我的bool,并显示它工作正常,但当我"暂停"时,我的UI不会弹出,我的游戏也不会在后台停止。我希望你能理解清楚。我是个初学者!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour {
    public GameObject pauseMenu;
    public bool paused = false;
    public void start()
    {
        pauseMenu.SetActive(false);
    }
    public void update()
    {
        if(Input.GetButtonDown("escape"))
        {
            paused = !paused;
        }
        if (paused)
        {
            pauseMenu.SetActive(true);
            Time.timeScale = 0;
        }
        if (!paused)
        {
            pauseMenu.SetActive(false);
            Time.timeScale = 1;
        }
    }
    public void Resume()
    {
        paused = false;
    }
    public void pauseButton()
    {
        paused = true;
    }
}

Don';我不知道如何正确使用SetActive()

建议您将代码更改为:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour {
    public GameObject pauseMenu;
    bool paused = false;
    void Start()
    {
        pauseMenu.SetActive(false);
    }
    void Update()
    {
        if(Input.GetButtonDown("escape"))
        {
            paused = !paused;
        }
        if (paused)
        {
            PauseGame();
        }
        if (!paused)
        {
            ResumeGame();
        }
    }
    void PauseGame(){
        pauseMenu.SetActive(true);
        Time.timeScale = 0f;
    }
    void ResumeGame(){
        pauseMenu.SetActive(false);
        Time.timeScale = 1f;
    }
    public void Resume()
    {
        ResumeGame();
    }
    public void pauseButton()
    {
        PauseGame();
    }

}

我认为问题在于,当按下暂停按钮时,时间刻度为0,更新不起作用。