Ejemplo n.º 1
0
    /// <summary>
    /// 返回local version config
    /// </summary>
    private async Task <VersionConfig> GetLocalVersionConfig()
    {
        VersionConfig localVersionConfig;
        string        versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");

        if (File.Exists(versionPath))
        {
            localVersionConfig = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath));
        }
        else
        {
            versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");
            UnityWebRequestAsync request = MonoBehaviourHelper.CreateTempComponent <UnityWebRequestAsync>();
            try
            {
                await request.DownloadAsync(versionPath);

                localVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
            }
            catch (System.Exception e)
            {
                Debug.Log(e.ToString());
                localVersionConfig = null;
            }
            finally
            {
                Destroy(request.gameObject);
                request = null;
            }
        }
        return(localVersionConfig);
    }
Ejemplo n.º 2
0
    public static async Task StartDownLoadResources()
    {
        if (StaticData.isUseAssetBundle)
        {
            BundleDownloaderComponent bundleDownloaderComponent = null;
            try
            {
                bundleDownloaderComponent = MonoBehaviourHelper.CreateTempComponent <BundleDownloaderComponent>();
                var t        = bundleDownloaderComponent.LoadInfo();
                var needDown = await t;
                StaticData.isNotWifiDownload = true;
                if (needDown)
                {
                    GameObject goUpdateProcess = ResourcesHelper.InstantiatePrefabFromResourceSetDefault("UISceneLoading", UIRoot.instance.GetUIRootCanvas().transform);
                    UITipABUpdateProgressComponent uiTipABUpdateProgressComponent = goUpdateProcess.GetComponent <UITipABUpdateProgressComponent>();
                    uiTipABUpdateProgressComponent.DownLoadInfo = bundleDownloaderComponent.DownloadInfo;
                    var x1 = bundleDownloaderComponent.DownloadInfo.TotalSize / 1024;
                    var x  = x1 / 1024f;
                    StaticData.isNotWifiDownloadClick = false;
                    //如果大于1m 不是wifi才弹提示
                    if (x > 1 && Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
                    {
                        GameObject goWifiConfirm = ResourcesHelper.InstantiatePrefabFromResourceSetDefault("UITipABUpdateNotWifi", UIRoot.instance.GetUIRootCanvas().transform);
                        UITipABUpdateNotWifiComponent uiTipABUpdateNotWifiComponent = goWifiConfirm.GetComponent <UITipABUpdateNotWifiComponent>();
                        //取两位小数
                        int j = (int)(x * 100);
                        x = j / 100f;
                        uiTipABUpdateNotWifiComponent.TextShow.text = $"当前不是wifi环境, 更新需要消耗{x}M流量,\n是否更新 ? (点击取消将退出游戏)";
                        await UniTask.WaitUntil(() => StaticData.isNotWifiDownloadClick == true);
                    }


                    if (!StaticData.isNotWifiDownload)
                    {
                        StaticData.QuitApplication();
                        //永远不返回
                        await UniTask.WaitUntil(() => StaticData.isNotWifiDownload == true);
                    }
                    await bundleDownloaderComponent.Down();

                    uiTipABUpdateProgressComponent.DownLoadInfo.IsEnd = true;
                    GameObject.DestroyImmediate(goUpdateProcess);
                }


                await ResourcesComponent.instance.LoadOneBundleAsync("StreamingAssets");

                ResourcesComponent.AssetBundleManifestObject = (AssetBundleManifest)ResourcesComponent.instance.GetAsset("StreamingAssets", "AssetBundleManifest");
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            finally
            {
                GameObject.Destroy(bundleDownloaderComponent.gameObject);
            }
        }
    }
Ejemplo n.º 3
0
    public static async Task <bool> IsGameVersionCodeEqual()
    {
        UnityWebRequestAsync webRequestAsync = MonoBehaviourHelper.CreateTempComponent <UnityWebRequestAsync>();

        try
        {
            GlobalProto.VersionCodeInfo NowLineCodeInfo  = null;
            GlobalProto.VersionCodeInfo UpdatingCodeInfo = null;
            string PathNowLineCodeInfo = GlobalConfigComponent.intance.GlobalProto.GetNowLineCodeInfoPath();
            //下载PathNowLineCodeInfo
            await webRequestAsync.DownloadAsync(PathNowLineCodeInfo);

            NowLineCodeInfo = GlobalConfigComponent.intance.GlobalProto.GetNowLineCodeInfo(webRequestAsync.Request.downloadHandler.text);
            string PathUpdatingCodeInfo = GlobalConfigComponent.intance.GlobalProto.GetUpdatingCodeInfoPath();
            //下载PathUpdatingCodeInfo
            await webRequestAsync.DownloadAsync(PathUpdatingCodeInfo);

            UpdatingCodeInfo = GlobalConfigComponent.intance.GlobalProto.GetUpdatingCodeInfo(webRequestAsync.Request.downloadHandler.text);
            webRequestAsync.Dispose();
            //比较VersionCode
            StaticData.DebugGreen($"local versionGameCode:{StaticData.localVersionGameCode},remote NowLineCodeInfo GameVersionCode:{NowLineCodeInfo.GameVersionCode} remote NowLineCodeInfo ResVersionCode:{NowLineCodeInfo.ResVersionCode},remote UpdatingCodeInfo GameVersionCode:{UpdatingCodeInfo.GameVersionCode} remote UpdatingCodeInfo ResVersionCode:{UpdatingCodeInfo.ResVersionCode}");
            if (StaticData.localVersionGameCode < NowLineCodeInfo.GameVersionCode)
            {
                //强更
                TipsDifferentVersion();
                StaticData.intParentResABDirectory      = NowLineCodeInfo.ResVersionCode;
                StaticData.isUsePlatformUpdatingGateWay = false;
                return(false);
            }
            else if (StaticData.localVersionGameCode == NowLineCodeInfo.GameVersionCode)
            {
                //热更本地资源
                StaticData.intParentResABDirectory      = NowLineCodeInfo.ResVersionCode;
                StaticData.isUsePlatformUpdatingGateWay = false;
                return(true);
            }
            else
            {
                //已经是最新包,只需要更新最新包资源即可
                StaticData.intParentResABDirectory      = UpdatingCodeInfo.ResVersionCode;
                StaticData.isUsePlatformUpdatingGateWay = true;
                return(true);
            }
        }
        catch (Exception e)
        {
            if (e.Message.Contains("request error"))
            {
                Debug.Log($"load VersionGameCode error:'{e.Message}'");
                return(true);
            }
        }
        finally
        {
            GameObject.Destroy(webRequestAsync.gameObject);
        }
        return(false);
    }
Ejemplo n.º 4
0
    private static async Task LoadAssetAsync(string assetBundleName, AssetBundle assetBundle, Action <float> actionProgress)
    {
        // 异步load资源到内存cache住
        UnityEngine.Object[] assets;


        AssetsLoaderAsync assetsLoaderAsync = MonoBehaviourHelper.CreateTempComponent <AssetsLoaderAsync>();

        assetsLoaderAsync.SetAB(assetBundle);
        assets = await assetsLoaderAsync.LoadAllAssetsAsync(actionProgress);

        Destroy(assetsLoaderAsync.gameObject);
    }
Ejemplo n.º 5
0
    /// <summary>
    /// 返回是否需要下载
    /// </summary>
    /// <returns></returns>
    public async Task <bool> LoadInfo()
    {
        UnityWebRequestAsync webRequestAsync = MonoBehaviourHelper.CreateTempComponent <UnityWebRequestAsync>();
        string remoteVersionText             = string.Empty;

        try
        {
            //下载remote version.txt
            string versionUrl = LoadBundlePathRoot() + "StreamingAssets/Version.txt";
            await webRequestAsync.DownloadAsync(versionUrl);

            remoteVersionText = webRequestAsync.Request.downloadHandler.text;
        }
        catch (Exception e)
        {
            if (e.Message.Contains("request error"))
            {
                webRequestAsync.Dispose();
                Debug.Log($"load VersionText error:'{e.Message}'");
                StaticData.isUseStreamingAssetRes = true;
                OnFileServerNotReach(e.Message);
                return(false);
            }
        }
        finally
        {
            Destroy(webRequestAsync.gameObject);
        }
        Debug.Log($"remoteVersionText:{remoteVersionText}");
        if (!remoteVersionText.StartsWith("{"))
        {
            Debug.Log("remote version text is not a correct json");
            this.remoteVersionConfig = null;
            return(false);
        }
        this.remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(remoteVersionText);
        var needDown = await AnalyseVersionConfig();

        if (needDown == false)
        {
            return(false);
        }
        return(true);
    }
Ejemplo n.º 6
0
    private async Task DownServerBundle()
    {
        while (true)
        {
            try
            {
                this.webRequest      = MonoBehaviourHelper.CreateTempComponent <UnityWebRequestAsync>();
                DownloadInfo.IsStart = true;
                var bundlePath = LoadBundlePathRoot() + "StreamingAssets/" + this.downloadingBundle;
                await this.webRequest.DownloadAsync(bundlePath);

                byte[] data          = this.webRequest.Request.downloadHandler.data;
                string path          = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
                string directoryName = Path.GetDirectoryName(path);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    fs.Write(data, 0, data.Length);
                    Debug.Log($"更新Bundle:{path} 完成");
                }
                var p = this.Progress;
                BundleRealProgress?.Invoke(p);
                Destroy(this.webRequest.gameObject);
                this.webRequest = null;
            }
            catch (Exception e)
            {
                Debug.LogError($"download bundle error: {this.downloadingBundle}\n{e}");
                //如果报错了,等1秒
                await UniTask.Delay(1);

                continue;
            }

            break;
        }
    }
Ejemplo n.º 7
0
    async Task LoadOneBundleAsync <T>(string assetBundleName) where T : UnityEngine.Object
    {
        //Log.Debug($"---------------load one bundle {assetBundleName}");
        ABInfo abInfo;

        if (this.bundles.TryGetValue(assetBundleName, out abInfo))
        {
            ++abInfo.RefCount;
            return;
        }


        if (this.cacheDictionary.ContainsKey(assetBundleName))
        {
            abInfo = this.cacheDictionary[assetBundleName];
            ++abInfo.RefCount;
            this.bundles[assetBundleName] = abInfo;
            this.cacheDictionary.Remove(assetBundleName);
            return;
        }


        if (!StaticData.isUseAssetBundle)
        {
#if UNITY_EDITOR
            //string[] realPath = null;
            //realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
            //foreach (string s in realPath)
            //{
            //    string assetName = Path.GetFileNameWithoutExtension(s);
            //    string path = $"{assetBundleName}/{assetName}".ToLower();
            //    T resource = AssetDatabase.LoadAssetAtPath<T>(s);
            //    this.resourceCache[path] = resource;
            //}

            this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
#endif
            return;
        }

        string      p           = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
        AssetBundle assetBundle = null;
        if (!File.Exists(p))
        {
            p = Path.Combine(PathHelper.AppResPath, assetBundleName);
        }

        AssetsBundleLoaderAsync assetsBundleLoaderAsync = MonoBehaviourHelper.CreateTempComponent <AssetsBundleLoaderAsync>();
        assetBundle = await assetsBundleLoaderAsync.LoadAsync(p);

        if (assetBundle == null)
        {
            throw new Exception($"assets bundle not found: {assetBundleName}");
        }

        //if (!assetBundle.isStreamedSceneAssetBundle)
        //{
        //    // 异步load资源到内存cache住
        //    UnityEngine.Object[] assets;
        //    using (AssetsLoaderAsync assetsLoaderAsync = ComponentFactory.Create<AssetsLoaderAsync, AssetBundle>(assetBundle))
        //    {
        //        assets = await assetsLoaderAsync.LoadAllAssetsAsync<T>();
        //    }
        //    foreach (UnityEngine.Object asset in assets)
        //    {
        //        string path = $"{assetBundleName}/{asset.name}".ToLower();
        //        this.resourceCache[path] = asset;
        //    }
        //}

        this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
    }
Ejemplo n.º 8
0
    public async Task LoadOneBundleAsync(string assetBundleName, Action <float> actionProgress = null)
    {
        //Log.Debug($"---------------load one bundle {assetBundleName}");
        ABInfo abInfo;

        if (this.bundles.TryGetValue(assetBundleName, out abInfo))
        {
            //Log.Debug($"读取到已经有的Bundle: {assetBundleName}");
            ++abInfo.RefCount;
            return;
        }


        if (this.cacheDictionary.ContainsKey(assetBundleName))
        {
            abInfo = this.cacheDictionary[assetBundleName];
            ++abInfo.RefCount;
            this.bundles[assetBundleName] = abInfo;
            this.cacheDictionary.Remove(assetBundleName);
            return;
        }


        if (!StaticData.isUseAssetBundle)
        {
#if UNITY_EDITOR
            //ZLog.Info("UNITY_EDITOR LoadOneBundleAsync");
            string[] realPath = null;
            realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
            //foreach (string s in realPath)
            //{
            //	string assetName = Path.GetFileNameWithoutExtension(s);
            //	string path = $"{assetBundleName}/{assetName}".ToLower();
            //	UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
            //	this.resourceCache[path] = resource;
            //}

            this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
#endif
            return;
        }

        string      p           = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
        AssetBundle assetBundle = null;
        if (!File.Exists(p))
        {
            p = Path.Combine(PathHelper.AppResPath, assetBundleName);
        }

        AssetsBundleLoaderAsync assetsBundleLoaderAsync = MonoBehaviourHelper.CreateTempComponent <AssetsBundleLoaderAsync>();
        DontDestroyOnLoad(assetsBundleLoaderAsync.gameObject);
        assetBundle = await assetsBundleLoaderAsync.LoadAsync(p, actionProgress);

        Destroy(assetsBundleLoaderAsync.gameObject);

        if (assetBundle == null)
        {
            throw new Exception($"assets bundle not found: {p}");
        }
        if (!assetBundle.isStreamedSceneAssetBundle && assetBundleName == "StreamingAssets")
        {
            await LoadAssetAsync(assetBundleName, assetBundle, actionProgress);
        }

        this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
    }