JisolGame/JNFrame/Assets/Game/Script/battle/GBattleModeManager.cs

111 lines
2.8 KiB
C#
Raw Normal View History

2024-01-26 19:15:07 +08:00
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
2024-02-01 19:06:51 +08:00
using Game.Plugins.App;
2024-01-29 19:07:52 +08:00
using Plugins.JNGame.Sync.Frame;
using Plugins.JNGame.Util;
2024-01-26 19:15:07 +08:00
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Script.battle
{
//各种模式场景
public enum GBattleMode
{
Not = -1,
Default = 0
}
//初始化参数类
public class GBattleModeInfo
{
public List<GameObject> modes;
public GameObject root;
}
//全局战斗模式管理器
public class GBattleModeManager : SingletonScene<GBattleModeManager>
{
2024-01-29 19:07:52 +08:00
2024-02-20 18:39:12 +08:00
private static readonly string[] Worlds = { "RVODemoMode", };
2024-01-26 19:15:07 +08:00
//当前模式
private GBattleMode _current = GBattleMode.Not;
2024-02-04 16:17:39 +08:00
//当前模式实体
public Object root;
//获取当前模式
public T GetMode<T>() where T : class
{
return root as T;
}
2024-01-26 19:15:07 +08:00
//初始化管理器
public void Init(GBattleModeInfo info)
{
2024-01-29 19:07:52 +08:00
// App.Event.AddListener(JNSyncFrameEvent.CREATE,LoadScene);
// App.Event.AddListener(JNSyncFrameEvent.CLEAR,UnloadScene);
2024-01-26 19:15:07 +08:00
}
//打开指定模式
public async UniTask Open(GBattleMode mode)
{
//销毁之前模式
_current = mode;
2024-01-29 19:07:52 +08:00
await OnReset();
2024-01-29 02:28:42 +08:00
//开始同步
2024-01-29 19:07:52 +08:00
App.Sync.OnStart();
2024-01-26 19:15:07 +08:00
}
2024-01-29 19:07:52 +08:00
//重置当前模式
public async UniTask OnReset()
2024-01-26 19:15:07 +08:00
{
2024-01-29 19:07:52 +08:00
await this.UnloadScene();
2024-01-30 19:22:27 +08:00
await UniTask.NextFrame();
2024-01-29 19:07:52 +08:00
App.Sync.OnReset();
2024-02-05 18:56:55 +08:00
await UniTask.NextFrame();
2024-01-29 19:07:52 +08:00
await this.LoadScene();
2024-01-26 19:15:07 +08:00
_current = GBattleMode.Not;
}
//获取场景名称
public string GetWorldName(GBattleMode mode)
{
return Worlds[(int)mode];
}
//加载场景
2024-01-29 19:07:52 +08:00
private async UniTask LoadScene()
2024-01-26 19:15:07 +08:00
{
2024-01-29 19:07:52 +08:00
GBattleMode mode = this._current;
2024-01-26 19:15:07 +08:00
if (mode == GBattleMode.Not) return;
Debug.Log($"[GBattleModeManager] 打开场景{GetWorldName(mode)}");
2024-01-30 19:22:27 +08:00
await SceneManager.LoadSceneAsync(GetWorldName(mode));
2024-01-26 19:15:07 +08:00
}
2024-01-29 19:07:52 +08:00
//销毁所有场景
private async UniTask UnloadScene()
2024-01-26 19:15:07 +08:00
{
2024-01-29 19:07:52 +08:00
Debug.Log($"[GBattleModeManager] 关闭场景");
2024-01-30 19:22:27 +08:00
await UniTask.NextFrame();
//
// for (int i = SceneManager.sceneCount - 1; i >= 0; i--)
// {
// try
// {
// await SceneManager.UnloadSceneAsync(SceneManager.GetSceneAt(i));
// }
// catch
// {
// // ignored
// }
// }
2024-01-26 19:15:07 +08:00
}
}
}