1
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user