Exemplo n.º 1
0
        private async ETTask LoadOneBundleAsync(string assetBundleName, bool isScene)
        {
            assetBundleName = assetBundleName.BundleNameToLower();
            ABInfo abInfo;

            if (this.bundles.TryGetValue(assetBundleName, out abInfo))
            {
                ++abInfo.RefCount;
                //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                return;
            }

            string      p           = "";
            AssetBundle assetBundle = null;

            if (!Define.IsAsync)
            {
#if UNITY_EDITOR
                if (isScene)
                {
                    p = Path.Combine(Application.dataPath, "../../AssetBundles/Windows_Scene/", assetBundleName);
                    if (File.Exists(p)) // 如果场景有预先打包
                    {
                        using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = EntityFactory.CreateWithParent <AssetsBundleLoaderAsync>(this))
                        {
                            assetBundle = await assetsBundleLoaderAsync.LoadAsync(p);
                        }

                        if (assetBundle == null)
                        {
                            // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
                            Log.Warning($"Scene bundle not found: {assetBundleName}");
                            return;
                        }

                        abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
                        this.bundles[assetBundleName] = abInfo;
                    }
                }
                else
                {
                    string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
                    foreach (string s in realPath)
                    {
                        string             assetName = Path.GetFileNameWithoutExtension(s);
                        UnityEngine.Object resource  = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(s);
                        AddResource(assetBundleName, assetName, resource);
                    }

                    if (realPath.Length > 0)
                    {
                        abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, null);
                        this.bundles[assetBundleName] = abInfo;
                        //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                    }
                    else
                    {
                        Log.Error("Bundle not exist! BundleName: " + assetBundleName);
                    }
                }
                // 编辑器模式也不能同步加载
                await TimerComponent.Instance.WaitAsync(20);
#endif
                return;
            }


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

            if (!File.Exists(p))
            {
                Log.Error("Async load bundle not exist! BundleName : " + p);
                return;
            }

            using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = EntityFactory.CreateWithParent <AssetsBundleLoaderAsync>(this))
            {
                assetBundle = await assetsBundleLoaderAsync.LoadAsync(p);
            }

            if (assetBundle == null)
            {
                // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
                Log.Warning($"assets bundle not found: {assetBundleName}");
                return;
            }

            if (!assetBundle.isStreamedSceneAssetBundle)
            {
                // 异步load资源到内存cache住
                UnityEngine.Object[] assets;
                using (AssetsLoaderAsync assetsLoaderAsync = EntityFactory.CreateWithParent <AssetsLoaderAsync, AssetBundle>(this, assetBundle))
                {
                    assets = await assetsLoaderAsync.LoadAllAssetsAsync();
                }

                foreach (UnityEngine.Object asset in assets)
                {
                    AddResource(assetBundleName, asset.name, asset);
                }
            }

            abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
            this.bundles[assetBundleName] = abInfo;

            //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
        }
Exemplo n.º 2
0
        public static Skill Create(Entity parent, int configid)
        {
            Skill skill = EntityFactory.CreateWithParent <Skill, int>(parent, configid);

            return(skill);
        }
Exemplo n.º 3
0
        private void LoadOneBundle(string assetBundleName)
        {
            assetBundleName = assetBundleName.BundleNameToLower();
            ABInfo abInfo;

            if (this.bundles.TryGetValue(assetBundleName, out abInfo))
            {
                ++abInfo.RefCount;
                //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                return;
            }

            if (!Define.IsAsync)
            {
                string[] realPath = null;
#if UNITY_EDITOR
                realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
                foreach (string s in realPath)
                {
                    string             assetName = Path.GetFileNameWithoutExtension(s);
                    UnityEngine.Object resource  = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(s);
                    AddResource(assetBundleName, assetName, resource);
                }



                if (realPath.Length > 0)
                {
                    abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, null);
                    this.bundles[assetBundleName] = abInfo;
                    //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                }
                else
                {
                    Log.Error($"assets bundle not found: {assetBundleName}");
                }
#endif
                return;
            }

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

            if (assetBundle == null)
            {
                // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
                Log.Warning($"assets bundle not found: {assetBundleName}");
                return;
            }

            if (!assetBundle.isStreamedSceneAssetBundle)
            {
                // 异步load资源到内存cache住
                UnityEngine.Object[] assets = assetBundle.LoadAllAssets();
                foreach (UnityEngine.Object asset in assets)
                {
                    AddResource(assetBundleName, asset.name, asset);
                }
            }

            abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
            this.bundles[assetBundleName] = abInfo;

            //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
        }
Exemplo n.º 4
0
        public async ETTask StartAsync(string url)
        {
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = EntityFactory.Create <UnityWebRequestAsync>(this.Domain))
                {
                    versionUrl = url + "StreamingAssets/" + "Version.txt";
                    //Log.Debug(versionUrl);
                    await webRequestAsync.DownloadAsync(versionUrl);

                    remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                    //Log.Debug(JsonHelper.ToJson(this.VersionConfig));
                }
            }
            catch (Exception e)
            {
                throw new Exception($"url: {versionUrl}", e);
            }

            // 获取streaming目录的Version.txt
            VersionConfig streamingVersionConfig;
            string        versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");

            using (UnityWebRequestAsync request = EntityFactory.Create <UnityWebRequestAsync>(this.Domain))
            {
                await request.DownloadAsync(versionPath);

                streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
            }

            // 删掉远程不存在的文件
            DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath);

            if (directoryInfo.Exists)
            {
                FileInfo[] fileInfos = directoryInfo.GetFiles();
                foreach (FileInfo fileInfo in fileInfos)
                {
                    if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name))
                    {
                        continue;
                    }

                    if (fileInfo.Name == "Version.txt")
                    {
                        continue;
                    }

                    fileInfo.Delete();
                }
            }
            else
            {
                directoryInfo.Create();
            }

            // 对比MD5
            foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values)
            {
                // 对比md5
                string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
                if (fileVersionInfo.MD5 == localFileMD5)
                {
                    continue;
                }
                this.bundles.Enqueue(fileVersionInfo.File);
                this.TotalSize += fileVersionInfo.Size;
            }
        }
Exemplo n.º 5
0
        public async ETTask DownloadAsync(string url)
        {
            if (this.bundles.Count == 0)
            {
                return;
            }
            try
            {
                //正在下载的文件个数
                int downloadingCount = 0;
                //下载单个文件
                async void downloadFile()
                {
                    if (this.bundles.Count == 0)
                    {
                        return;
                    }
                    downloadingCount++;
                    //取出一个进行下载
                    string downloading = this.bundles.Dequeue();

                    Log.Debug($"开始下载({downloadingCount}):{downloading}");
                    try
                    {
                        using (UnityWebRequestAsync webRequest = EntityFactory.Create <UnityWebRequestAsync>(this.Domain))
                        {
                            webRequests.Add(webRequest);
                            await webRequest.DownloadAsync(url + "StreamingAssets/" + downloading);

                            byte[] data = webRequest.Request.downloadHandler.data;
                            webRequests.Remove(webRequest);
                            string path = Path.Combine(PathHelper.AppHotfixResPath, downloading);
                            using (FileStream fs = new FileStream(path, FileMode.Create))
                            {
                                fs.Write(data, 0, data.Length);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        //下载异常跳过
                        Log.Error($"download bundle error: {downloading}\n{e}");
                    }
                    finally
                    {
                        downloadingCount--;
                    }
                    //正常下载
                    this.downloadedBundles.Add(downloading);
                    Log.Debug($"download bundle Finish: {downloading}\n");
                }

                /*
                 * //最多同时下载n个文件 下载40M(400~500)个文件测试时间(ms)对比
                 * //等待n个任务同时完成再继续 1~61616 2~44796 3~34377 4~31918 5~27184 6~25564 7~22817 8~22719
                 * //完成1个补充1个最大n个任务 1~61309 8~11871 9~10843 10~10600 15~9309 20~9146 100~9195
                 */

                //最大任务数量20 速度从61秒提升到9秒
                int maxCount = 20;
                while (true)
                {
                    await TimerComponent.Instance.WaitAsync(10);

                    //需要下载队列取完 正在下载为0表示完成更新
                    if (this.bundles.Count == 0 && downloadingCount == 0)
                    {
                        break;
                    }
                    for (int i = downloadingCount; i < maxCount; i++)
                    {
                        downloadFile();
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Exemplo n.º 6
0
        public async ETTask LoadOneBundleAsync(string assetBundleName)
        {
            ABInfo abInfo;

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

            //Log.Debug($"---------------load one bundle {assetBundleName}");
            if (!Define.IsAsync)
            {
                string[] realPath = null;
#if UNITY_EDITOR
                realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
                foreach (string s in realPath)
                {
                    string             assetName = Path.GetFileNameWithoutExtension(s);
                    UnityEngine.Object resource  = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(s);
                    AddResource(assetBundleName, assetName, resource);
                }

                abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, null);
                this.bundles[assetBundleName] = abInfo;
#endif
                return;
            }

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

            using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = EntityFactory.Create <AssetsBundleLoaderAsync>(this.Domain))
            {
                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 = EntityFactory.Create <AssetsLoaderAsync, AssetBundle>(this.Domain, assetBundle))
                {
                    assets = await assetsLoaderAsync.LoadAllAssetsAsync();
                }
                foreach (UnityEngine.Object asset in assets)
                {
                    AddResource(assetBundleName, asset.name, asset);
                }
            }

            abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
            this.bundles[assetBundleName] = abInfo;
        }
Exemplo n.º 7
0
        private async ETTask <ABInfo> LoadOneBundleAsync(string assetBundleName)
        {
            assetBundleName = assetBundleName.BundleNameToLower();
            ABInfo abInfo;

            if (this.bundles.TryGetValue(assetBundleName, out abInfo))
            {
                ++abInfo.RefCount;
                //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                return(null);
            }

            string      p           = "";
            AssetBundle assetBundle = null;

            if (!Define.IsAsync)
            {
#if UNITY_EDITOR
                string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
                foreach (string s in realPath)
                {
                    string             assetName = Path.GetFileNameWithoutExtension(s);
                    UnityEngine.Object resource  = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(s);
                    AddResource(assetBundleName, assetName, resource);
                }

                if (realPath.Length > 0)
                {
                    abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, null);
                    this.bundles[assetBundleName] = abInfo;
                    //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                }
                else
                {
                    Log.Error("Bundle not exist! BundleName: " + assetBundleName);
                }

                // 编辑器模式也不能同步加载
                await TimerComponent.Instance.WaitAsync(100);
#endif
                return(abInfo);
            }

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

            Log.Info("Async load bundle BundleName : " + p);

            // if (!File.Exists(p))
            // {
            //     Log.Error("Async load bundle not exist! BundleName : " + p);
            //     return null;
            // }

            assetBundle = await AssetBundleHelper.UnityLoadBundleAsync(p);

            if (assetBundle == null)
            {
                // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
                Log.Warning($"assets bundle not found: {assetBundleName}");
                return(null);
            }

            abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
            this.bundles[assetBundleName] = abInfo;
            return(abInfo);
            //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
        }