using System; using UnityEngine; using UnityEngine.SceneManagement; using KFADA = XGame.Framework.Asyncs; namespace XGame.Framework.Asset { public class SceneAsyncOperation : KFADA.ResultAsync { 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(); if (result != null) { Result = result; isFind = true; break; } } if (!isFind) throw new NullReferenceException($"未发现该对象:{typeof(T)},请检查场景资源文件:{_sceneName}"); Completed(); }; } }); } } }