/// <summary>
        /// 轮询更新
        /// </summary>
        public override void Update()
        {
            if (_steps == ESteps.Done)
            {
                return;
            }

            if (_steps == ESteps.None)
            {
                if (MainBundleInfo.IsInvalid)
                {
                    _steps    = ESteps.Done;
                    Status    = EStatus.Failed;
                    LastError = $"The bundle info is invalid : {MainBundleInfo.BundleName}";
                    YooLogger.Error(LastError);
                    return;
                }

                if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
                {
                    _steps  = ESteps.LoadFile;
                    _webURL = MainBundleInfo.GetStreamingLoadPath();
                }
                else
                {
                    throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
                }
            }

            // 1. 从服务器或缓存中获取AssetBundle文件
            if (_steps == ESteps.LoadFile)
            {
                string hash = StringUtility.RemoveExtension(MainBundleInfo.Hash);
                _webRequest = UnityWebRequestAssetBundle.GetAssetBundle(_webURL, Hash128.Parse(hash));
                _webRequest.SendWebRequest();
                _steps = ESteps.CheckFile;
            }

            // 2. 检测获取的AssetBundle文件
            if (_steps == ESteps.CheckFile)
            {
                if (_webRequest.isDone == false)
                {
                    return;
                }

#if UNITY_2020_1_OR_NEWER
                if (_webRequest.result != UnityWebRequest.Result.Success)
#else
                if (_webRequest.isNetworkError || _webRequest.isHttpError)
#endif
                {
                    YooLogger.Warning($"Failed to get asset bundle form web : {_webURL} Error : {_webRequest.error}");
                    _steps    = ESteps.TryLoad;
                    _tryTimer = 0;
                }
                else
                {
                    CacheBundle = DownloadHandlerAssetBundle.GetContent(_webRequest);
                    if (CacheBundle == null)
                    {
                        _steps    = ESteps.Done;
                        Status    = EStatus.Failed;
                        LastError = $"AssetBundle file is invalid : {MainBundleInfo.BundleName}";
                        YooLogger.Error(LastError);
                    }
                    else
                    {
                        _steps = ESteps.Done;
                        Status = EStatus.Succeed;
                    }
                }
            }

            // 3. 如果获取失败,重新尝试
            if (_steps == ESteps.TryLoad)
            {
                _tryTimer += Time.unscaledDeltaTime;
                if (_tryTimer > 1f)
                {
                    _webRequest.Dispose();
                    _webRequest = null;
                    _steps      = ESteps.LoadFile;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 初始化资源路径映射
        /// </summary>
        public void InitAssetPathMapping(bool locationToLower)
        {
            if (_isInitAssetPathMapping)
            {
                return;
            }
            _isInitAssetPathMapping = true;

            if (EnableAddressable)
            {
                if (locationToLower)
                {
                    YooLogger.Error("Addressable not support location to lower !");
                }

                foreach (var patchAsset in AssetList)
                {
                    string location = patchAsset.Address;
                    if (AssetPathMapping.ContainsKey(location))
                    {
                        throw new Exception($"Address have existed : {location}");
                    }
                    else
                    {
                        AssetPathMapping.Add(location, patchAsset.AssetPath);
                    }
                }
            }
            else
            {
                _locationToLower = locationToLower;
                foreach (var patchAsset in AssetList)
                {
                    string location = patchAsset.AssetPath;
                    if (locationToLower)
                    {
                        location = location.ToLower();
                    }

                    // 添加原生路径的映射
                    if (AssetPathMapping.ContainsKey(location))
                    {
                        throw new Exception($"AssetPath have existed : {location}");
                    }
                    else
                    {
                        AssetPathMapping.Add(location, patchAsset.AssetPath);
                    }

                    // 添加无后缀名路径的映射
                    if (Path.HasExtension(location))
                    {
                        string locationWithoutExtension = StringUtility.RemoveExtension(location);
                        if (AssetPathMapping.ContainsKey(locationWithoutExtension))
                        {
                            YooLogger.Warning($"AssetPath have existed : {locationWithoutExtension}");
                        }
                        else
                        {
                            AssetPathMapping.Add(locationWithoutExtension, patchAsset.AssetPath);
                        }
                    }
                }
            }
        }