feat(): 完成将Manager从Gameplay转移到Core,实现循环次数持久化,实现设置面板
This commit is contained in:
@@ -5,14 +5,16 @@ namespace Core
|
||||
{
|
||||
public class GameManager : MonoSingleton<GameManager>
|
||||
{
|
||||
public event Action OnGameStart;
|
||||
private void Start()
|
||||
{
|
||||
ScenesManager.Instance.LoadMainMenu();
|
||||
}
|
||||
|
||||
public void ReStartGame()
|
||||
public void ReStartGameplay()
|
||||
{
|
||||
ScenesManager.Instance.LoadGameplay("Level1");
|
||||
OnGameStart?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,75 +6,73 @@ namespace Core
|
||||
{
|
||||
public class ScenesManager : MonoSingleton<ScenesManager>
|
||||
{
|
||||
public static ScenesManager Instance { get; private set; }
|
||||
|
||||
private string currentGameplayScene;
|
||||
private bool isLoadGameplayUI = false;
|
||||
|
||||
private bool IsLoadGameplayUI = false;
|
||||
// 修正:直接根据当前激活场景判断
|
||||
public bool IsInMainMenu =>
|
||||
currentGameplayScene == "StartMenu";
|
||||
|
||||
void Awake()
|
||||
// 修正:只有当已有记录的 gameplay 场景且激活场景与之匹配时才返回 true
|
||||
public bool IsInGameplay =>
|
||||
currentGameplayScene == "Level1" || currentGameplayScene == "Test";
|
||||
|
||||
public bool IsActiveScene(string sceneName)
|
||||
{
|
||||
if (Instance != null)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
Instance = this;
|
||||
//DontDestroyOnLoad(gameObject);
|
||||
return SceneManager.GetActiveScene().name == sceneName;
|
||||
}
|
||||
|
||||
public bool IsSceneLoaded(string sceneName)
|
||||
{
|
||||
return SceneManager.GetSceneByName(sceneName).isLoaded;
|
||||
}
|
||||
|
||||
public void LoadMainMenu()
|
||||
{
|
||||
if (IsLoadGameplayUI)
|
||||
if (isLoadGameplayUI)
|
||||
{
|
||||
// 如果已经加载过游戏内UI场景,则卸载掉
|
||||
StartCoroutine(UnloadScene("UIScene"));
|
||||
IsLoadGameplayUI = false;
|
||||
isLoadGameplayUI = false;
|
||||
}
|
||||
// 加载主菜单场景,需要卸载掉除了Core以外的所有场景
|
||||
|
||||
StartCoroutine(SwitchGameplay("StartMenu"));
|
||||
}
|
||||
|
||||
|
||||
public void LoadGameplay(string sceneName)
|
||||
{
|
||||
StartCoroutine(SwitchGameplay(sceneName));
|
||||
if (!IsLoadGameplayUI)
|
||||
if (!isLoadGameplayUI)
|
||||
{
|
||||
// 只加载一次游戏内UI场景
|
||||
StartCoroutine(LoadSceneAdditive("UIScene"));
|
||||
IsLoadGameplayUI = true;
|
||||
isLoadGameplayUI = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换游戏场景,卸载当前的游戏场景,加载新的游戏场景
|
||||
/// 适用于在游戏内切换场景
|
||||
/// </summary>
|
||||
/// <param name="newScene"></param>
|
||||
/// <returns></returns>
|
||||
private IEnumerator SwitchGameplay(string newScene)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(currentGameplayScene))
|
||||
{
|
||||
yield return UnloadScene(currentGameplayScene);
|
||||
}
|
||||
|
||||
yield return LoadSceneAdditive(newScene);
|
||||
currentGameplayScene = newScene;
|
||||
// 可选:将新场景设置为激活场景
|
||||
SceneManager.SetActiveScene(SceneManager.GetSceneByName(newScene));
|
||||
}
|
||||
|
||||
private IEnumerator LoadSceneAdditive(string sceneName)
|
||||
{
|
||||
var async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
|
||||
var async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
|
||||
while (!async.isDone)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private IEnumerator UnloadScene(string sceneName)
|
||||
{
|
||||
var async = UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync(sceneName);
|
||||
var async = SceneManager.UnloadSceneAsync(sceneName);
|
||||
while (!async.isDone)
|
||||
{
|
||||
yield return null;
|
||||
|
||||
Reference in New Issue
Block a user