Beispiel #1
0
        public override void Update()
        {
            if (IsDone)
            {
                return;
            }

            if (Status == EStatus.None)
            {
                Status = EStatus.CheckBundle;
            }

            // 1. 检测资源包
            if (Status == EStatus.CheckBundle)
            {
                if (DependBundleGroup.IsDone() == false)
                {
                    return;
                }
                if (OwnerBundle.IsDone() == false)
                {
                    return;
                }

                if (DependBundleGroup.IsSucceed() == false)
                {
                    Status    = EStatus.Fail;
                    LastError = DependBundleGroup.GetLastError();
                    InvokeCompletion();
                    return;
                }

                if (OwnerBundle.Status != AssetBundleLoaderBase.EStatus.Succeed)
                {
                    Status    = EStatus.Fail;
                    LastError = OwnerBundle.LastError;
                    InvokeCompletion();
                    return;
                }

                Status = EStatus.Loading;
            }

            // 2. 加载场景
            if (Status == EStatus.Loading)
            {
                _asyncOp = SceneManager.LoadSceneAsync(_sceneName, SceneMode);
                if (_asyncOp != null)
                {
                    _asyncOp.allowSceneActivation = true;
                    _asyncOp.priority             = _priority;
                    Status = EStatus.Checking;
                }
                else
                {
                    Status    = EStatus.Fail;
                    LastError = $"Failed to load scene : {_sceneName}";
                    YooLogger.Error(LastError);
                    InvokeCompletion();
                }
            }

            // 3. 检测加载结果
            if (Status == EStatus.Checking)
            {
                if (_asyncOp.isDone)
                {
                    SceneObject = SceneManager.GetSceneByName(_sceneName);
                    if (SceneObject.IsValid() && _activateOnLoad)
                    {
                        SceneManager.SetActiveScene(SceneObject);
                    }

                    Status = SceneObject.IsValid() ? EStatus.Success : EStatus.Fail;
                    if (Status == EStatus.Fail)
                    {
                        LastError = $"The load scene is invalid : {MainAssetInfo.AssetPath}";
                        YooLogger.Error(LastError);
                    }
                    InvokeCompletion();
                }
            }
        }
        public override void Update()
        {
            if (IsDone)
            {
                return;
            }

            if (Status == EStatus.None)
            {
                Status = EStatus.CheckBundle;
            }

            // 1. 检测资源包
            if (Status == EStatus.CheckBundle)
            {
                if (IsWaitForAsyncComplete)
                {
                    DependBundleGroup.WaitForAsyncComplete();
                    OwnerBundle.WaitForAsyncComplete();
                }

                if (DependBundleGroup.IsDone() == false)
                {
                    return;
                }
                if (OwnerBundle.IsDone() == false)
                {
                    return;
                }

                if (DependBundleGroup.IsSucceed() == false)
                {
                    Status    = EStatus.Fail;
                    LastError = DependBundleGroup.GetLastError();
                    InvokeCompletion();
                    return;
                }

                if (OwnerBundle.Status != AssetBundleLoaderBase.EStatus.Succeed)
                {
                    Status    = EStatus.Fail;
                    LastError = OwnerBundle.LastError;
                    InvokeCompletion();
                    return;
                }

                Status = EStatus.Loading;
            }

            // 2. 加载资源对象
            if (Status == EStatus.Loading)
            {
                if (IsWaitForAsyncComplete)
                {
                    if (MainAssetInfo.AssetType == null)
                    {
                        AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath);
                    }
                    else
                    {
                        AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
                    }
                }
                else
                {
                    if (MainAssetInfo.AssetType == null)
                    {
                        _cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath);
                    }
                    else
                    {
                        _cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
                    }
                }
                Status = EStatus.Checking;
            }

            // 3. 检测加载结果
            if (Status == EStatus.Checking)
            {
                if (_cacheRequest != null)
                {
                    if (IsWaitForAsyncComplete)
                    {
                        // 强制挂起主线程(注意:该操作会很耗时)
                        YooLogger.Warning("Suspend the main thread to load unity asset.");
                        AssetObject = _cacheRequest.asset;
                    }
                    else
                    {
                        if (_cacheRequest.isDone == false)
                        {
                            return;
                        }
                        AssetObject = _cacheRequest.asset;
                    }
                }

                Status = AssetObject == null ? EStatus.Fail : EStatus.Success;
                if (Status == EStatus.Fail)
                {
                    if (MainAssetInfo.AssetType == null)
                    {
                        LastError = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.BundleName}";
                    }
                    else
                    {
                        LastError = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.BundleName}";
                    }
                    YooLogger.Error(LastError);
                }
                InvokeCompletion();
            }
        }