This commit is contained in:
CortexCore
2023-10-06 23:43:19 +08:00
parent ebf9c1f526
commit 2c4710bc5d
186 changed files with 111802 additions and 764 deletions

View File

@@ -7,8 +7,12 @@ using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
// ReSharper disable Unity.LoadSceneWrongIndex
namespace BITKit.SceneManagement
{
@@ -20,10 +24,16 @@ namespace BITKit.SceneManagement
[Serializable]
public class SceneServiceSingletonProxy:ISceneService
{
private ISceneService _sceneServiceImplementation => SceneService.Singleton;
public bool InitializeMainSceneOnLoad=>SceneService.Singleton.InitializeMainSceneOnLoad;
public UniTask LoadSceneAsync(string sceneName, CancellationToken cancellationToken,
LoadSceneMode loadSceneMode = LoadSceneMode.Additive, bool activateOnLoad = true)=>SceneService.Singleton.LoadSceneAsync(sceneName,cancellationToken,loadSceneMode,activateOnLoad);
public UniTask UnloadSceneAsync(string sceneName, CancellationToken cancellationToken)
{
return _sceneServiceImplementation.UnloadSceneAsync(sceneName, cancellationToken);
}
public event Action<string> OnLoadScene
{
add => SceneService.OnLoadScene += value;
@@ -39,6 +49,18 @@ namespace BITKit.SceneManagement
add => SceneService.OnSceneLoaded += value;
remove => SceneService.OnSceneLoaded -= value;
}
public event Action<string> OnUnloadScene
{
add => _sceneServiceImplementation.OnUnloadScene += value;
remove => _sceneServiceImplementation.OnUnloadScene -= value;
}
public event Action<string> OnSceneUnloaded
{
add => _sceneServiceImplementation.OnSceneUnloaded += value;
remove => _sceneServiceImplementation.OnSceneUnloaded -= value;
}
}
public class SceneService : MonoBehaviour,ISceneService
@@ -48,8 +70,14 @@ namespace BITKit.SceneManagement
[RuntimeInitializeOnLoadMethod]
private static void Initialize()
{
if (AllowInitialize)
OnLoadScene =null;
OnSceneLoadProgress = null;
OnSceneLoaded = null;
if (AllowInitialize && SceneManager.sceneCount is not 0)
{
SceneManager.LoadSceneAsync(0);
}
}
#endif
public static event Action<string> OnLoadScene;
@@ -60,7 +88,9 @@ namespace BITKit.SceneManagement
[SerializeField] private Optional<float> allowLoadDelay;
#endif
[SerializeField] private Optional<string> allowLoadMainScene;
private CancellationToken _cancellationToken;
private readonly List<SceneInstance> _loadedObjects = new();
private void Awake()
{
Singleton = this;
@@ -108,6 +138,7 @@ namespace BITKit.SceneManagement
if (activateOnLoad is false)
{
asyncOperation.Result.ActivateAsync().ToUniTask(cancellationToken: cancellationToken).Forget();
_loadedObjects.Add(asyncOperation.Result);
}
BIT4Log.Log<SceneService>($"场景:{sceneName}加载完成,耗时:{stopwatchWatcher.ElapsedMilliseconds}ms");
}
@@ -116,6 +147,18 @@ namespace BITKit.SceneManagement
}
}
public async UniTask UnloadSceneAsync(string sceneName, CancellationToken cancellationToken)
{
await UniTask.SwitchToMainThread();
OnUnloadScene?.Invoke(sceneName);
foreach (var x in _loadedObjects)
{
await Addressables.UnloadSceneAsync(x);
}
OnSceneUnloaded?.Invoke(sceneName);
}
event Action<string> ISceneService.OnLoadScene
{
add=>OnLoadScene+=value;
@@ -133,6 +176,9 @@ namespace BITKit.SceneManagement
add=>OnSceneLoaded+=value;
remove => OnSceneLoaded -= value;
}
public event Action<string> OnUnloadScene;
public event Action<string> OnSceneUnloaded;
}
}