This commit is contained in:
CortexCore
2023-11-15 23:55:06 +08:00
parent 5446067f91
commit 70247f0242
82 changed files with 3271 additions and 579 deletions

View File

@@ -6,8 +6,7 @@
"GUID:045a42f233e479d41adc32d02b99631e",
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
"GUID:9e24947de15b9834991c9d8411ea37cf",
"GUID:84651a3751eca9349aac36a66bba901b"
"GUID:e34a5702dd353724aa315fb8011f08c3"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -17,6 +17,8 @@ namespace BITKit.SceneManagement
/// 在场景加载完成后,是否初始化主场景
/// </summary>
bool InitializeMainSceneOnLoad { get; }
string[] GetScenes(params string[] tags);
/// <summary>
/// 加载场景
/// </summary>
@@ -56,6 +58,7 @@ namespace BITKit.SceneManagement
private ISceneService _sceneServiceImplementation1 => _sceneServiceImplementation;
protected abstract ISceneService _sceneServiceImplementation { get; }
public bool InitializeMainSceneOnLoad => _sceneServiceImplementation.InitializeMainSceneOnLoad;
public string[] GetScenes(params string[] tags)=> _sceneServiceImplementation.GetScenes(tags);
public UniTask LoadSceneAsync(string sceneName,CancellationToken cancellationToken, LoadSceneMode loadSceneMode = LoadSceneMode.Additive,
bool activateOnLoad = true)

View File

@@ -2,13 +2,14 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
using YooAsset;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
@@ -26,6 +27,11 @@ namespace BITKit.SceneManagement
{
private ISceneService _sceneServiceImplementation => SceneService.Singleton;
public bool InitializeMainSceneOnLoad=>SceneService.Singleton.InitializeMainSceneOnLoad;
public string[] GetScenes(params string[] tags)
{
return SceneService.Singleton.GetScenes(tags);
}
public UniTask LoadSceneAsync(string sceneName, CancellationToken cancellationToken,
LoadSceneMode loadSceneMode = LoadSceneMode.Additive, bool activateOnLoad = true)=>SceneService.Singleton.LoadSceneAsync(sceneName,cancellationToken,loadSceneMode,activateOnLoad);
@@ -88,9 +94,11 @@ namespace BITKit.SceneManagement
[SerializeField] private Optional<float> allowLoadDelay;
#endif
[SerializeField] private Optional<string> allowLoadMainScene;
[SerializeField] private Optional<string> allowMenuScene;
private readonly Dictionary<string, Scene> LoadedObjects = new();
private CancellationToken _cancellationToken;
private readonly List<SceneInstance> _loadedObjects = new();
private void Awake()
{
Singleton = this;
@@ -98,11 +106,26 @@ namespace BITKit.SceneManagement
}
private void Start()
{
if (allowMenuScene.Allow)
{
YooAssets
.LoadSceneAsync(allowMenuScene.Value).ToUniTask(cancellationToken: _cancellationToken)
.Forget();
}
if (allowLoadMainScene.Allow is false) return;
LoadSceneAsync(allowLoadMainScene.Value,_cancellationToken,LoadSceneMode.Single,false).Forget();
}
public bool InitializeMainSceneOnLoad => true;
public async UniTask LoadSceneAsync(string sceneName,CancellationToken cancellationToken, LoadSceneMode loadSceneMode = LoadSceneMode.Additive,
public string[] GetScenes(params string[] tags)
{
var infos = YooAssets.GetAssetInfos(tags);
return infos.Where(x=>x.Address.Split("_")[0] is "Maps").Select(x=>x.Address).ToArray();
}
public async UniTask LoadSceneAsync(string sceneName, CancellationToken cancellationToken,
LoadSceneMode loadSceneMode = LoadSceneMode.Additive,
bool activateOnLoad = true)
{
try
@@ -111,51 +134,66 @@ namespace BITKit.SceneManagement
stopwatchWatcher.Start();
BIT4Log.Log<SceneService>($"正在加载场景:{sceneName}");
OnLoadScene?.Invoke(sceneName);
#if UNITY_EDITOR
if (allowLoadDelay.Allow)
{
var progress = 0f;
while (progress < allowLoadDelay.Value)
{
OnSceneLoadProgress?.Invoke(sceneName, progress +=1/allowLoadDelay.Value * Time.deltaTime);
OnSceneLoadProgress?.Invoke(sceneName, progress += 1 / allowLoadDelay.Value * Time.deltaTime);
cancellationToken.ThrowIfCancellationRequested();
await UniTask.NextFrame(cancellationToken);
}
}
#endif
var asyncOperation = Addressables.LoadSceneAsync(sceneName, loadSceneMode, activateOnLoad);
while (asyncOperation.IsDone is false)
// var asyncOperation = Addressables.LoadSceneAsync(sceneName, loadSceneMode, activateOnLoad);
// while (asyncOperation.IsDone is false)
// {
// await UniTask.NextFrame(cancellationToken);
// var progress = asyncOperation.PercentComplete;
// OnSceneLoadProgress?.Invoke(sceneName,progress);
// }
var sceneMode = UnityEngine.SceneManagement.LoadSceneMode.Single;
var handle = YooAssets.LoadSceneAsync(sceneName, sceneMode);
while (handle.IsDone is false)
{
var progress = handle.Progress;
await UniTask.NextFrame(cancellationToken);
var progress = asyncOperation.PercentComplete;
OnSceneLoadProgress?.Invoke(sceneName,progress);
OnSceneLoadProgress?.Invoke(sceneName, progress);
}
LoadedObjects.Add(sceneName, handle.SceneObject);
OnSceneLoadProgress?.Invoke(sceneName, 1);
await Task.Delay(100, cancellationToken);
OnSceneLoaded?.Invoke(sceneName);
stopwatchWatcher.Stop();
if (activateOnLoad is false)
{
asyncOperation.Result.ActivateAsync().ToUniTask(cancellationToken: cancellationToken).Forget();
_loadedObjects.Add(asyncOperation.Result);
}
// if (activateOnLoad is false)
// {
// asyncOperation.Result.ActivateAsync().ToUniTask(cancellationToken: cancellationToken).Forget();
// _loadedObjects.Add(asyncOperation.Result);
// }
BIT4Log.Log<SceneService>($"场景:{sceneName}加载完成,耗时:{stopwatchWatcher.ElapsedMilliseconds}ms");
}
catch (OperationCanceledException)
{
}
}
public async UniTask UnloadSceneAsync(string sceneName, CancellationToken cancellationToken)
{
await UniTask.SwitchToMainThread();
if (LoadedObjects.TryRemove(sceneName) is false) return;
//await SceneManager.UnloadSceneAsync(scene);
SceneManager.LoadScene(1);
destroyCancellationToken.ThrowIfCancellationRequested();
OnUnloadScene?.Invoke(sceneName);
foreach (var x in _loadedObjects)
{
await Addressables.UnloadSceneAsync(x);
}
OnSceneUnloaded?.Invoke(sceneName);
}