add kcp
This commit is contained in:
@@ -17,7 +17,16 @@ namespace BITKit.SceneManagement
|
||||
/// 在场景加载完成后,是否初始化主场景
|
||||
/// </summary>
|
||||
bool InitializeMainSceneOnLoad { get; }
|
||||
/// <summary>
|
||||
/// 加载场景
|
||||
/// </summary>
|
||||
/// <param name="sceneName">场景名称,通常为AddressablePath</param>
|
||||
/// <param name="cancellationToken">取消令牌</param>
|
||||
/// <param name="loadSceneMode">加载模式</param>
|
||||
/// <param name="activateOnLoad">加载完成后激活</param>
|
||||
/// <returns></returns>
|
||||
public UniTask LoadSceneAsync(string sceneName,CancellationToken cancellationToken, LoadSceneMode loadSceneMode = LoadSceneMode.Additive, bool activateOnLoad = true);
|
||||
public UniTask UnloadSceneAsync(string sceneName,CancellationToken cancellationToken);
|
||||
/// <summary>
|
||||
/// 开始加载场景的回调
|
||||
/// </summary>
|
||||
@@ -30,12 +39,21 @@ namespace BITKit.SceneManagement
|
||||
/// 场景加载完成的回调
|
||||
/// </summary>
|
||||
event Action<string> OnSceneLoaded;
|
||||
/// <summary>
|
||||
/// 当开始卸载场景时
|
||||
/// </summary>
|
||||
event Action<string> OnUnloadScene;
|
||||
/// <summary>
|
||||
/// 当场景卸载完成时
|
||||
/// </summary>
|
||||
event Action<string> OnSceneUnloaded;
|
||||
}
|
||||
/// <summary>
|
||||
/// 场景服务代理实现,主要用于快速继承
|
||||
/// </summary>
|
||||
public abstract class SceneServiceImplement:ISceneService
|
||||
{
|
||||
private ISceneService _sceneServiceImplementation1 => _sceneServiceImplementation;
|
||||
protected abstract ISceneService _sceneServiceImplementation { get; }
|
||||
public bool InitializeMainSceneOnLoad => _sceneServiceImplementation.InitializeMainSceneOnLoad;
|
||||
|
||||
@@ -45,6 +63,11 @@ namespace BITKit.SceneManagement
|
||||
return _sceneServiceImplementation.LoadSceneAsync(sceneName,cancellationToken, loadSceneMode, activateOnLoad);
|
||||
}
|
||||
|
||||
public UniTask UnloadSceneAsync(string sceneName, CancellationToken cancellationToken)
|
||||
{
|
||||
return _sceneServiceImplementation1.UnloadSceneAsync(sceneName, cancellationToken);
|
||||
}
|
||||
|
||||
public event Action<string> OnLoadScene
|
||||
{
|
||||
add => _sceneServiceImplementation.OnLoadScene += value;
|
||||
@@ -62,6 +85,18 @@ namespace BITKit.SceneManagement
|
||||
add => _sceneServiceImplementation.OnSceneLoaded += value;
|
||||
remove => _sceneServiceImplementation.OnSceneLoaded -= value;
|
||||
}
|
||||
|
||||
public event Action<string> OnUnloadScene
|
||||
{
|
||||
add => _sceneServiceImplementation1.OnUnloadScene += value;
|
||||
remove => _sceneServiceImplementation1.OnUnloadScene -= value;
|
||||
}
|
||||
|
||||
public event Action<string> OnSceneUnloaded
|
||||
{
|
||||
add => _sceneServiceImplementation1.OnSceneUnloaded += value;
|
||||
remove => _sceneServiceImplementation1.OnSceneUnloaded -= value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user