123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using UnityEngine.SceneManagement;
- namespace YooAsset
- {
- public class SceneOperationHandle : OperationHandleBase
- {
- private System.Action<SceneOperationHandle> _callback;
- internal SceneOperationHandle(ProviderBase provider) : base(provider)
- {
- }
- internal override void InvokeCallback()
- {
- _callback?.Invoke(this);
- }
- /// <summary>
- /// 完成委托
- /// </summary>
- public event System.Action<SceneOperationHandle> Completed
- {
- add
- {
- if (IsValid == false)
- throw new System.Exception($"{nameof(SceneOperationHandle)} is invalid");
- if (Provider.IsDone)
- value.Invoke(this);
- else
- _callback += value;
- }
- remove
- {
- if (IsValid == false)
- throw new System.Exception($"{nameof(SceneOperationHandle)} is invalid");
- _callback -= value;
- }
- }
- /// <summary>
- /// 场景对象
- /// </summary>
- public Scene SceneObject
- {
- get
- {
- if (IsValid == false)
- return new Scene();
- return Provider.SceneObject;
- }
- }
- /// <summary>
- /// 激活场景
- /// </summary>
- public bool ActivateScene()
- {
- if (IsValid == false)
- return false;
- if (SceneObject.IsValid() && SceneObject.isLoaded)
- {
- return SceneManager.SetActiveScene(SceneObject);
- }
- else
- {
- YooLogger.Warning($"Scene is invalid or not loaded : {SceneObject.name}");
- return false;
- }
- }
- /// <summary>
- /// 是否为主场景
- /// </summary>
- public bool IsMainScene()
- {
- if (IsValid == false)
- return false;
- if (Provider is DatabaseSceneProvider)
- {
- var temp = Provider as DatabaseSceneProvider;
- return temp.SceneMode == LoadSceneMode.Single;
- }
- else if (Provider is BundledSceneProvider)
- {
- var temp = Provider as BundledSceneProvider;
- return temp.SceneMode == LoadSceneMode.Single;
- }
- else
- {
- throw new System.NotImplementedException();
- }
- }
- /// <summary>
- /// 异步卸载子场景
- /// </summary>
- public UnloadSceneOperation UnloadAsync()
- {
- // 如果句柄无效
- if (IsValid == false)
- {
- string error = $"{nameof(SceneOperationHandle)} is invalid.";
- var operation = new UnloadSceneOperation(error);
- OperationSystem.StartOperation(operation);
- return operation;
- }
- // 如果是主场景
- if (IsMainScene())
- {
- string error = $"Cannot unload main scene. Use {nameof(YooAssets.LoadSceneAsync)} method to change the main scene !";
- YooLogger.Error(error);
- var operation = new UnloadSceneOperation(error);
- OperationSystem.StartOperation(operation);
- return operation;
- }
- // 卸载子场景
- Scene sceneObject = SceneObject;
- AssetSystem.UnloadSubScene(Provider);
- {
- var operation = new UnloadSceneOperation(sceneObject);
- OperationSystem.StartOperation(operation);
- return operation;
- }
- }
- }
- }
|