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

99 lines
2.5 KiB
C#
Raw Normal View History

2024-01-26 19:15:07 +08:00
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
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
private static readonly string[] Worlds = { "WorldSceneMode" };
2024-01-26 19:15:07 +08:00
//当前模式
private GBattleMode _current = GBattleMode.Not;
//初始化管理器
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();
App.Sync.OnReset();
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)}");
await SceneManager.LoadSceneAsync(GetWorldName(mode), LoadSceneMode.Additive);
}
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] 关闭场景");
foreach (var world in Worlds)
{
try
{
await SceneManager.UnloadSceneAsync(world);
}
catch
{
// ignored
}
}
2024-01-26 19:15:07 +08:00
}
}
}