2023-08-23 01:59:26 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
|
|
namespace BITKit.SceneManagement
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 场景服务,负责场景的加载和卸载
|
|
|
|
/// </summary>
|
|
|
|
public interface ISceneService
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 在场景加载完成后,是否初始化主场景
|
|
|
|
/// </summary>
|
|
|
|
bool InitializeMainSceneOnLoad { get; }
|
2023-10-06 23:43:19 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 加载场景
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sceneName">场景名称,通常为AddressablePath</param>
|
|
|
|
/// <param name="cancellationToken">取消令牌</param>
|
|
|
|
/// <param name="loadSceneMode">加载模式</param>
|
|
|
|
/// <param name="activateOnLoad">加载完成后激活</param>
|
|
|
|
/// <returns></returns>
|
2023-08-23 01:59:26 +08:00
|
|
|
public UniTask LoadSceneAsync(string sceneName,CancellationToken cancellationToken, LoadSceneMode loadSceneMode = LoadSceneMode.Additive, bool activateOnLoad = true);
|
2023-10-06 23:43:19 +08:00
|
|
|
public UniTask UnloadSceneAsync(string sceneName,CancellationToken cancellationToken);
|
2023-08-23 01:59:26 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 开始加载场景的回调
|
|
|
|
/// </summary>
|
|
|
|
event Action<string> OnLoadScene;
|
|
|
|
/// <summary>
|
|
|
|
/// 加载场景进度的回调
|
|
|
|
/// </summary>
|
|
|
|
event Action<string,float> OnSceneLoadProgress;
|
|
|
|
/// <summary>
|
|
|
|
/// 场景加载完成的回调
|
|
|
|
/// </summary>
|
|
|
|
event Action<string> OnSceneLoaded;
|
2023-10-06 23:43:19 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 当开始卸载场景时
|
|
|
|
/// </summary>
|
|
|
|
event Action<string> OnUnloadScene;
|
|
|
|
/// <summary>
|
|
|
|
/// 当场景卸载完成时
|
|
|
|
/// </summary>
|
|
|
|
event Action<string> OnSceneUnloaded;
|
2023-08-23 01:59:26 +08:00
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// 场景服务代理实现,主要用于快速继承
|
|
|
|
/// </summary>
|
|
|
|
public abstract class SceneServiceImplement:ISceneService
|
|
|
|
{
|
2023-10-06 23:43:19 +08:00
|
|
|
private ISceneService _sceneServiceImplementation1 => _sceneServiceImplementation;
|
2023-08-23 01:59:26 +08:00
|
|
|
protected abstract ISceneService _sceneServiceImplementation { get; }
|
|
|
|
public bool InitializeMainSceneOnLoad => _sceneServiceImplementation.InitializeMainSceneOnLoad;
|
|
|
|
|
|
|
|
public UniTask LoadSceneAsync(string sceneName,CancellationToken cancellationToken, LoadSceneMode loadSceneMode = LoadSceneMode.Additive,
|
|
|
|
bool activateOnLoad = true)
|
|
|
|
{
|
|
|
|
return _sceneServiceImplementation.LoadSceneAsync(sceneName,cancellationToken, loadSceneMode, activateOnLoad);
|
|
|
|
}
|
|
|
|
|
2023-10-06 23:43:19 +08:00
|
|
|
public UniTask UnloadSceneAsync(string sceneName, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _sceneServiceImplementation1.UnloadSceneAsync(sceneName, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2023-08-23 01:59:26 +08:00
|
|
|
public event Action<string> OnLoadScene
|
|
|
|
{
|
|
|
|
add => _sceneServiceImplementation.OnLoadScene += value;
|
|
|
|
remove => _sceneServiceImplementation.OnLoadScene -= value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public event Action<string, float> OnSceneLoadProgress
|
|
|
|
{
|
|
|
|
add => _sceneServiceImplementation.OnSceneLoadProgress += value;
|
|
|
|
remove => _sceneServiceImplementation.OnSceneLoadProgress -= value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public event Action<string> OnSceneLoaded
|
|
|
|
{
|
|
|
|
add => _sceneServiceImplementation.OnSceneLoaded += value;
|
|
|
|
remove => _sceneServiceImplementation.OnSceneLoaded -= value;
|
|
|
|
}
|
2023-10-06 23:43:19 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-08-23 01:59:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|