190 lines
6.6 KiB
C#
190 lines
6.6 KiB
C#
|
/********************************************************************
|
|||
|
文件: SceneManager.cs
|
|||
|
作者:梦语
|
|||
|
邮箱:1982614048@qq.com
|
|||
|
日期:2024/02/19 14:26:48
|
|||
|
功能:场景管理
|
|||
|
*********************************************************************/
|
|||
|
|
|||
|
using DG.Tweening;
|
|||
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.EventSystems;
|
|||
|
using UnityEngine.SceneManagement;
|
|||
|
using UnityTimer;
|
|||
|
|
|||
|
namespace Ether
|
|||
|
{
|
|||
|
public class SceneSystemManager : SingletonAutoMono<SceneSystemManager>
|
|||
|
{
|
|||
|
private Dictionary<string, SceneSoundData> allSceneAudio;
|
|||
|
|
|||
|
private void PlaySceneAudio(string sceneName)
|
|||
|
{
|
|||
|
if (allSceneAudio == null)
|
|||
|
{
|
|||
|
allSceneAudio = new Dictionary<string, SceneSoundData>();
|
|||
|
if (FileTools.ReadFileForJson(PathTools.SceneAudioCfg, out List<SceneSoundData> list))
|
|||
|
{
|
|||
|
foreach (var data in list)
|
|||
|
{
|
|||
|
if (!allSceneAudio.ContainsKey(data.sceneName))
|
|||
|
{
|
|||
|
allSceneAudio.Add(data.sceneName, data);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (AudioManager.Inst.IsPlayingBgAudio())
|
|||
|
{
|
|||
|
DOTween.To(() => AudioManager.Inst.BackGroundVolume, (x) =>
|
|||
|
{
|
|||
|
AudioManager.Inst.BackGroundVolume = x;
|
|||
|
}, 0, 1).OnComplete(() =>
|
|||
|
{
|
|||
|
AudioManager.Inst.StopBgAudio();
|
|||
|
if (allSceneAudio.TryGetValue(sceneName, out SceneSoundData soundData))
|
|||
|
{
|
|||
|
if (soundData.soundDataList.Count > 0)
|
|||
|
{
|
|||
|
int playIndex = UnityEngine.Random.Range(0, soundData.soundDataList.Count - 1);
|
|||
|
SoundData playData = soundData.soundDataList[playIndex];
|
|||
|
AudioManager.Inst.BackGroundVolume = playData.volum;
|
|||
|
AudioManager.Inst.PlayBgAudio(playData.name);
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (allSceneAudio.TryGetValue(sceneName, out SceneSoundData soundData))
|
|||
|
{
|
|||
|
if (soundData.soundDataList.Count > 0)
|
|||
|
{
|
|||
|
int playIndex = UnityEngine.Random.Range(0, soundData.soundDataList.Count - 1);
|
|||
|
SoundData playData = soundData.soundDataList[playIndex];
|
|||
|
AudioManager.Inst.BackGroundVolume = playData.volum;
|
|||
|
AudioManager.Inst.PlayBgAudio(playData.name);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
#region 同步加载场景
|
|||
|
public void LoadScene(string sceneName, LoadSceneMode mode = LoadSceneMode.Single)
|
|||
|
{
|
|||
|
if (SceneManager.GetActiveScene().name == sceneName)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
PlaySceneAudio(sceneName);
|
|||
|
SceneManager.LoadScene(sceneName, mode);
|
|||
|
}
|
|||
|
|
|||
|
public void LoadScene(int sceneBuildIndex, LoadSceneMode mode = LoadSceneMode.Single)
|
|||
|
{
|
|||
|
if (SceneManager.GetActiveScene().buildIndex == sceneBuildIndex)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
SceneManager.LoadScene(sceneBuildIndex, mode);
|
|||
|
}
|
|||
|
|
|||
|
public Scene LoadScene(string sceneName, LoadSceneParameters loadSceneParameters)
|
|||
|
{
|
|||
|
if (SceneManager.GetActiveScene().name == sceneName)
|
|||
|
{
|
|||
|
return default;
|
|||
|
}
|
|||
|
PlaySceneAudio(sceneName);
|
|||
|
return SceneManager.LoadScene(sceneName, loadSceneParameters);
|
|||
|
}
|
|||
|
|
|||
|
public Scene LoadScene(int sceneBuildIndex, LoadSceneParameters loadSceneParameters)
|
|||
|
{
|
|||
|
if (SceneManager.GetActiveScene().buildIndex == sceneBuildIndex)
|
|||
|
{
|
|||
|
return default;
|
|||
|
}
|
|||
|
return SceneManager.LoadScene(sceneBuildIndex, loadSceneParameters);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 异步加载场景
|
|||
|
|
|||
|
public void LoadSceneAsync(string sceneName, Action<float> callBack = null, LoadSceneMode mode = LoadSceneMode.Single)
|
|||
|
{
|
|||
|
if (SceneManager.GetActiveScene().name == sceneName)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
MonoManager.Inst.StartCoroutine(DoLoadSceneAsync(sceneName, callBack, mode));
|
|||
|
}
|
|||
|
|
|||
|
public void LoadSceneAsync(int sceneIndex, Action<float> callBack = null, LoadSceneMode mode = LoadSceneMode.Single)
|
|||
|
{
|
|||
|
if (SceneManager.GetActiveScene().buildIndex == sceneIndex)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
MonoManager.Inst.StartCoroutine(DoLoadSceneAsync(sceneIndex, callBack, mode));
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator DoLoadSceneAsync(int sceneIndex, Action<float> loadingCallBack = null, LoadSceneMode mode = LoadSceneMode.Single)
|
|||
|
{
|
|||
|
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneIndex, mode);
|
|||
|
|
|||
|
// 等待直到场景加载完成
|
|||
|
while (!asyncLoad.isDone)
|
|||
|
{
|
|||
|
// 可以在这里添加加载进度条的更新逻辑
|
|||
|
float progress = asyncLoad.progress;
|
|||
|
// 更新UI进度条
|
|||
|
loadingCallBack?.Invoke(progress);
|
|||
|
|
|||
|
// 等待下一帧
|
|||
|
yield return null;
|
|||
|
}
|
|||
|
loadingCallBack?.Invoke(1);
|
|||
|
|
|||
|
// 场景加载完成,自动跳转到新场景
|
|||
|
SceneManager.SetActiveScene(SceneManager.GetSceneAt(sceneIndex));
|
|||
|
|
|||
|
EventCenter.BroadCast("LoadSceneSuccess");
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator DoLoadSceneAsync(string sceneName, Action<float> loadingCallBack = null, LoadSceneMode mode = LoadSceneMode.Single)
|
|||
|
{
|
|||
|
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName, mode);
|
|||
|
|
|||
|
// 等待直到场景加载完成
|
|||
|
while (!asyncLoad.isDone)
|
|||
|
{
|
|||
|
// 可以在这里添加加载进度条的更新逻辑
|
|||
|
float progress = asyncLoad.progress;
|
|||
|
// 更新UI进度条
|
|||
|
loadingCallBack?.Invoke(progress);
|
|||
|
|
|||
|
// 等待下一帧
|
|||
|
yield return null;
|
|||
|
}
|
|||
|
loadingCallBack?.Invoke(1);
|
|||
|
|
|||
|
// 场景加载完成,自动跳转到新场景
|
|||
|
SceneManager.SetActiveScene(SceneManager.GetSceneByName(sceneName));
|
|||
|
|
|||
|
PlaySceneAudio(sceneName);
|
|||
|
EventCenter.BroadCast("LoadSceneSuccess");
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|