using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; /// /// 暂停菜单:继续游戏 / 回到 Main 场景。 /// 按钮点击在各自 Button 的 On Click() 里接线(Inspector 配置)。 /// [DisallowMultipleComponent] public class PauseMenu : MonoBehaviour { [Tooltip("「回到Main」要加载的场景名,必须已经加进 Build Settings")] [SerializeField] private string mainSceneName = "Main"; private bool _leaving; /// 继续游戏:恢复时间,关掉自己。 public void OnContinueClicked() { Time.timeScale = 1f; Destroy(gameObject); } /// 回到 Main:加载 Main 场景,加载完成后卸载当前场景。 public void OnBackToMainClicked() { if (_leaving) return; _leaving = true; Time.timeScale = 1f; StartCoroutine(LoadMainThenUnloadCurrent()); } private IEnumerator LoadMainThenUnloadCurrent() { Scene currentScene = gameObject.scene; AsyncOperation load = SceneManager.LoadSceneAsync(mainSceneName, LoadSceneMode.Additive); if (load == null) { Debug.LogError("[PauseMenu] 场景 \"" + mainSceneName + "\" 加载失败,确认它已加入 Build Settings。", this); _leaving = false; yield break; } yield return load; // 等 Main 加载完成 Scene loaded = SceneManager.GetSceneByName(mainSceneName); if (loaded.IsValid()) SceneManager.SetActiveScene(loaded); SceneManager.UnloadSceneAsync(currentScene); // 卸载当前场景 } }