123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using KFADA = XGame.Framework.Asyncs;
- namespace XGame.Framework.Asset
- {
- public class SceneAsyncOperation<T> : KFADA.ResultAsync<T>
- {
- public object SceneAsset;
- private string _sceneName;
- private IAssetModule _assetModule;
- private AsyncOperation _async;
- private KFADA.IAsync _abAsync;
- private float _abProgress = 0.5f;
- public override float Progress
- {
- get
- {
- if (_abAsync != null || _async != null) return _abProgress;
- else
- {
- if (!IsCompleted)
- return _abProgress + _async.progress / 2;
- else
- return 1.0f;
- }
- }
- }
- public SceneAsyncOperation(string sceneName, IAssetModule assetModule)
- {
- _sceneName = sceneName;
- _assetModule = assetModule;
- Init();
- }
- private void Init()
- {
- _abAsync = _assetModule.LoadSceneBundleAsync(_sceneName);
- _abAsync.On((async) =>
- {
- if (async.IsCompleted)
- {
- _async = SceneManager.LoadSceneAsync(_sceneName, LoadSceneMode.Additive);
- _async.completed += (a) =>
- {
- bool isFind = false;
- var objects = SceneManager.GetSceneByName(_sceneName).GetRootGameObjects();
- foreach (var item in objects)
- {
- var result = item.GetComponent<T>();
- if (result != null)
- {
- Result = result;
- isFind = true;
- break;
- }
- }
- if (!isFind) throw new NullReferenceException($"未发现该对象:{typeof(T)},请检查场景资源文件:{_sceneName}");
- Completed();
- };
- }
- });
- }
- }
- }
|