Exemplo n.º 1
0
        //http文本读取成功
        private void OnHttpReadTextSuccess(object sender, IEventArgs e)
        {
            HttpReadTextSuccessEventArgs ne = (HttpReadTextSuccessEventArgs)e;

            if (ne != null)
            {
                _remoteVersion = JsonUtility.FromJson <AssetBundleVersionInfo>(ne.Content);
                if (_remoteVersion == null)
                {
                    Debug.LogError("Remote Version is null");
                    return;
                }

                //如果资源版本不一样 则更新资源
                if (!CompareVersion())
                {
                    //更新资源
                    UpdateResource();
                    //下载资源
                    DownloadResource();
                }

                //资源更新完成
                _resourceUpdateDone = true;
            }
        }
            static void GenerateAssetVersionInfo()
            {
                string platform = GameFrameworkCommon.GetPlatformName();
                string dirpath  = System.IO.Path.Combine(UnityEngine.Application.dataPath.Replace("Assets", ""), "AssetBundles/" + platform);
                AssetBundleVersionInfo abversion = new AssetBundleVersionInfo();

                abversion.IsEncrypt           = false;
                abversion.Version             = 10000;
                abversion.ManifestAssetBundle = platform;

                var infos     = new List <ResourcesInfo>();
                var resources = CalculateMd5(dirpath);

                foreach (var item in resources)
                {
                    ResourcesInfo info = new ResourcesInfo();

                    info.Name = item.Key.Substring(item.Key.IndexOf(platform) + platform.Length + 1).Replace(@"\\", "/");
                    info.MD5  = item.Value;
                    infos.Add(info);
                }
                abversion.Resources = infos;

                string assetversion = EditorJsonUtility.ToJson(abversion);

                System.IO.File.WriteAllText(System.IO.Path.Combine(ResourceManager.GetDeafultPath(PathType.ReadOnly), CheckResourceState.AssetVersionTxt), assetversion);

                ATFileOp.ShowExplorerWindow(System.IO.Path.Combine(ResourceManager.GetDeafultPath(PathType.ReadOnly), CheckResourceState.AssetVersionTxt));
            }
Exemplo n.º 3
0
        /// <summary>
        /// 下载版本信息文本成功监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnHttpReadTextSuccess(object sender, IEventArgs e)
        {
            HttpReadTextEventArgs args = (HttpReadTextEventArgs)e;

            if (args != null)
            {
                if (args.Url == Path.Combine(GameMain.ResourceMG.ResUpdatePath, _assetPlatformVersionText))
                {
                    AssetPlatformVersionInfo assetPlatform = JsonUtility.FromJson <AssetPlatformVersionInfo>(args.Additive);
                    string platformName = GameFrameworkCommon.GetPlatformName();
                    if (assetPlatform.Platforms.Contains(platformName))
                    {
                        //资源的更新路径
                        GameMain.ResourceMG.ResUpdatePath = Path.Combine(GameMain.ResourceMG.ResUpdatePath, platformName);

                        //读取远程的文本
                        string remotePath = Path.Combine(GameMain.ResourceMG.ResUpdatePath, _assetVersionTxt);
                        GameMain.WebRequestMG.ReadHttpText(remotePath);
                    }
                    else
                    {
#if UNITY_EDITOR
                        UnityEngine.Debug.Log("服务器上不包含当前平台的资源文件!");
#endif
                    }
                }
                else if (args.Url == Path.Combine(GameMain.ResourceMG.ResUpdatePath, _assetVersionTxt))
                {
                    _remoteVersion = JsonUtility.FromJson <AssetBundleVersionInfo>(args.Additive);
                    if (_remoteVersion == null)
                    {
#if UNITY_EDITOR
                        UnityEngine.Debug.LogError("Remote version is null.");
#endif
                        return;
                    }

                    if (!CompareVersion())
                    {
                        //更新资源
                        UpdateResource();
                        //下载资源
                        DownloadResource();
                    }

#if UNITY_EDITOR
                    Debug.Log(">>>>>资源已经是最新版本了!");
#endif

                    //资源更新完成
                    _resourceUpdateDone = true;
                }
            }
        }
Exemplo n.º 4
0
        public override void OnEnter(params object[] parameters)
        {
            base.OnEnter(parameters);
            GameMode.Event.AddListener <HttpReadTextSuccessEventArgs>(OnHttpReadTextSuccess);
            GameMode.Event.AddListener <HttpReadTextFaileEventArgs>(OnHttpReadTextFaile);
            GameMode.Event.AddListener <DownloadSuccessEventArgs>(OnDownloadSuccess);
            GameMode.Event.AddListener <DownloadFaileEventArgs>(OnDownloadFaile);
            GameMode.Event.AddListener <DownloadProgressEventArgs>(OnDownloadProgress);

            _localVersion = LoadLocalVersion();
            LoadRemoteVersion();
        }
        public override void OnEnter(params object[] parameters)
        {
            base.OnEnter(parameters);

            string localPath = Path.Combine(GameMode.Resource.LocalPath, "AssetVersion.txt");
            AssetBundleVersionInfo versionInfo = JsonUtility.FromJson <AssetBundleVersionInfo>(File.ReadAllText(localPath));

            //设置ab包的加载方式
            GameMode.Resource.SetResourceHelper(new BundleResourceHelper());
            //加载ab包的mainfest文件
            GameMode.Resource.SetMainfestAssetBundle(versionInfo.ManifestAssetBundle, versionInfo.IsEncrypt);
        }
Exemplo n.º 6
0
        public override void OnEnter(params object[] parameters)
        {
            base.OnEnter(parameters);
            //注册监听
            GameMain.EventMG.RegisterEvent(EventType.HttpReadTextSuccess, OnHttpReadTextSuccess);
            GameMain.EventMG.RegisterEvent(EventType.HttpReadTextFailure, OnHttpReadTextFailure);
            GameMain.EventMG.RegisterEvent(EventType.HttpDownLoadSuccess, OnHttpDownLoadSuccess);
            GameMain.EventMG.RegisterEvent(EventType.HttpDownLoadFailure, OnHttpDownLoadFailure);
            GameMain.EventMG.RegisterEvent(EventType.HttpDownLoadProgress, OnHttpDownLoadProgress);

            _retryRecord  = new Dictionary <string, int>();
            _localVersion = LoadLocalVersion();
            LoadRemoteVersion();
        }
Exemplo n.º 7
0
        public override void OnEnter(params object[] parameters)
        {
#if UNITY_EDITOR
            UnityEngine.Debug.Log("---------call LoadResourceState------------");
#endif
            base.OnEnter(parameters);
            string localPath = Path.Combine(ResourceManager.GetDeafultPath(PathType.ReadOnly), CheckResourceState.AssetVersionTxt);
            AssetBundleVersionInfo versionInfo = JsonUtility.FromJson <AssetBundleVersionInfo>(File.ReadAllText(localPath));

            //设置AB包的加载方式
            GameMain.ResourceMG.SetResourceHelper(new BundleResourceHelper());
            //设置资源依赖
            GameMain.ResourceMG.SetMainfestAssetBundle(versionInfo.ManifestAssetBundle, versionInfo.IsEncrypt);

            //测试 直接切换到热更新状态里面去
            ChangeState <PreloadState>();
        }
Exemplo n.º 8
0
        //http文本读取成功
        private void OnHttpReadTextSuccess(object sender, IEventArgs e)
        {
            HttpReadTextSuccessEventArgs ne = (HttpReadTextSuccessEventArgs)e;

            if (ne != null)
            {
                if (ne.Url == Path.Combine(GameMode.Resource.ResUpdatePath, _assetPlatformVersionText))
                {
                    PlatformVersionInfo assetPlatform = JsonUtility.FromJson <PlatformVersionInfo>(ne.Content);
                    string platformName = GetPlatformName();
                    if (assetPlatform.Platforms.Contains(platformName))
                    {
                        //更新远程资源的路径
                        GameMode.Resource.ResUpdatePath =
                            Path.Combine(GameMode.Resource.ResUpdatePath, platformName);
                        //读取远程的文本
                        string remotePath = Path.Combine(GameMode.Resource.ResUpdatePath, _assetVersionTxt);
                        GameMode.WebRequest.ReadHttpText(remotePath);
                    }
                }
                else
                {
                    _remoteVersion = JsonUtility.FromJson <AssetBundleVersionInfo>(ne.Content);
                    if (_remoteVersion == null)
                    {
                        Debug.LogError("Remote Version is null");
                        return;
                    }

                    //如果资源版本不一样 则更新资源
                    if (!CompareVersion())
                    {
                        //更新资源
                        UpdateResource();
                        //下载资源
                        DownloadResource();
                    }

                    //资源更新完成
                    _resourceUpdateDone = true;
                }
            }
        }
        public override void OnEnter(params object[] parameters)
        {
            base.OnEnter(parameters);
            GameMode.Event.AddListener <HttpReadTextSuccessEventArgs>(OnHttpReadTextSuccess);
            GameMode.Event.AddListener <HttpReadTextFaileEventArgs>(OnHttpReadTextFaile);
            GameMode.Event.AddListener <DownloadSuccessEventArgs>(OnDownloadSuccess);
            GameMode.Event.AddListener <DownloadFaileEventArgs>(OnDownloadFaile);
            GameMode.Event.AddListener <DownloadProgressEventArgs>(OnDownloadProgress);

            _localVersion = LoadLocalVersion();

            //从StreamingAsset复制到读写路径下
            if (_localVersion == null)
            {
                MoveFiles(Application.streamingAssetsPath, Application.persistentDataPath);
                _localVersion = LoadLocalVersion();
            }

            LoadRemoteVersion();
        }
        //保存资源版本信息
        private static void SaveAssetVersion(string buildPath, BuildTarget target)
        {
            string targetBundlePath = Path.Combine(buildPath, target.ToString());

            if (!File.Exists(targetBundlePath))
            {
                return;
            }

            AssetBundleVersionInfo assetVersionInfo = new  AssetBundleVersionInfo();

            assetVersionInfo.Version             = _config.Version;
            assetVersionInfo.ManifestAssetBundle = target.ToString();
            assetVersionInfo.AssetHashInfos      = new List <AssetHashInfo>();

            AssetBundle         targetBundle = AssetBundle.LoadFromFile(targetBundlePath);
            AssetBundleManifest manifest     = targetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
            List <string>       assetNames   = new List <string>();

            assetNames.Add(target.ToString());
            assetNames.AddRange(manifest.GetAllAssetBundles());
            for (int i = 0; i < assetNames.Count; i++)
            {
                AssetHashInfo assetHashInfo = new AssetHashInfo();
                assetHashInfo.Name = assetNames[i];
                //AssetBundleManifest的hashcode 获取一直为00000000000000000000000000000000 于是用版本号代替
                assetHashInfo.Hash = i == 0?_config.Version.ToString():manifest.GetAssetBundleHash(assetNames[i]).ToString();
                assetVersionInfo.AssetHashInfos.Add(assetHashInfo);
                //删除manifest文件
                string manifestPath = Path.Combine(buildPath, assetNames[i], ".manifeset");
                if (File.Exists(manifestPath))
                {
                    File.Delete(manifestPath);
                }
            }

            string json = JsonUtility.ToJson(assetVersionInfo);

            File.WriteAllText(Path.Combine(buildPath, _assetVersionTxt), json);
            targetBundle.Unload(true);
        }
Exemplo n.º 11
0
        public static void BuildAssetBundles()
        {
            string buildPath = EditorConfigInfo.BuildPath;

            if (!Directory.Exists(buildPath))
            {
                Debug.LogError("Please set build path!");
                return;
            }

            //打包资源
            Debug.Log("开始打包!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));
            BuildAssetBundleOptions option = (BuildAssetBundleOptions)EditorConfigInfo.ZipMode;
            BuildTarget             target = (BuildTarget)EditorConfigInfo.BuildTarget;

            BuildPipeline.BuildAssetBundles(buildPath, option, target);
            Debug.Log("打包完成!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));

            //资源加密
            if ((EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES)
            {
                string keyPath = Application.dataPath + "/Resources";
                if (!Directory.Exists(keyPath))
                {
                    Directory.CreateDirectory(keyPath);
                }
                if (!File.Exists(keyPath + "/Key.asset"))
                {
                    EnciphererKey ek = ScriptableObject.CreateInstance <EnciphererKey>();
                    ek.GeneraterKey();
                    AssetDatabase.CreateAsset(ek, "Assets/Resources/Key.asset");
                }

                EnciphererKey    keyAsset = Resources.Load("Key") as EnciphererKey;
                DirectoryInfo    dir      = new DirectoryInfo(buildPath);
                FileSystemInfo[] infos    = dir.GetFileSystemInfos();
                Debug.Log("开始加密!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));
                foreach (FileSystemInfo info in infos)
                {
                    if (info is FileInfo)
                    {
                        if (info.Extension == "")
                        {
                            byte[] bs    = File.ReadAllBytes(info.FullName);
                            byte[] cipbs = Encipherer.AESEncrypt(bs, keyAsset);
                            File.WriteAllBytes(info.FullName, cipbs);
                            Debug.Log("完成资源包 " + info.Name + " 的加密!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));
                        }
                    }
                }
                Debug.Log("加密完成!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));
            }

            //写入版本号信息
            string assetVersionPath        = buildPath + "/AssetVersion.txt";
            AssetBundleVersionInfo version = new AssetBundleVersionInfo();

            version.Version   = EditorConfigInfo.AssetVersion;
            version.IsEncrypt = (EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES;
            version.Resources = new List <ResourcesInfo>();
            int index = buildPath.LastIndexOf("/", StringComparison.Ordinal);

            if (index > 0)
            {
                version.ManifestAssetBundle = buildPath.Substring(index + 1, buildPath.Length - index - 1);
            }
            DirectoryInfo dir1 = new DirectoryInfo(buildPath);

            FileSystemInfo[] infos1 = dir1.GetFileSystemInfos();
            foreach (FileSystemInfo info in infos1)
            {
                if (info is FileInfo)
                {
                    if (info.Extension == "")
                    {
                        ResourcesInfo ri = new ResourcesInfo();
                        ri.Name = info.Name;
                        ri.Hash = info.GetHashCode().ToString();
                        version.Resources.Add(ri);
                    }
                }
            }
            string content = JsonUtility.ToJson(version);

            File.WriteAllText(assetVersionPath, content);

            //版本号迭代
            EditorConfigInfo.AssetVersion += 1;
            SaveEditorConfigInfo();

            AssetDatabase.Refresh();

            //打开打包文件夹
            EditorUtility.OpenWithDefaultApp(buildPath);
        }
Exemplo n.º 12
0
        public static void BuildAssetBundles()
        {
            string buildPath = EditorConfigInfo.BuildPath;

            if (!Directory.Exists(buildPath))
            {
                Debug.LogError("Please set build path!");
                return;
            }

            //平台资源的信息
            string platformVersionPath = Path.Combine(buildPath, "AssetPlatformVersion.txt");
            AssetPlatformVersionInfo assetPlatformVersionInfo = new AssetPlatformVersionInfo();

            assetPlatformVersionInfo.Platforms = new List <string>();
            //资源加密
            EnciphererKey keyAsset = null;

            if ((EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES)
            {
                string keyPath = Application.dataPath + "/Resources";
                if (!Directory.Exists(keyPath))
                {
                    Directory.CreateDirectory(keyPath);
                }

                if (!File.Exists(keyPath + "/Key.asset"))
                {
                    EnciphererKey ek = ScriptableObject.CreateInstance <EnciphererKey>();
                    ek.GeneraterKey();
                    AssetDatabase.CreateAsset(ek, "Assets/Resources/Key.asset");
                    AssetDatabase.Refresh();
                }

                keyAsset = Resources.Load("Key") as EnciphererKey;
            }

            //根据各个平台打包
            foreach (var item in EditorConfigInfo.BuildTargets)
            {
                //打包
                BuildTarget target     = (BuildTarget)item;
                string      targetName = target.ToString().ToLower();
                //添加平台的信息
                assetPlatformVersionInfo.Platforms.Add(targetName);
                string targetBuildPath = Path.Combine(buildPath, targetName);
                if (!Directory.Exists(targetBuildPath))
                {
                    Directory.CreateDirectory(targetBuildPath);
                }

                Debug.Log("开始打包--" + targetName + System.DateTime.Now.ToString("  HH:mm:ss:fff"));
                BuildAssetBundleOptions option = (BuildAssetBundleOptions)EditorConfigInfo.ZipMode;
                BuildPipeline.BuildAssetBundles(targetBuildPath, option, target);
                Debug.Log("打包完成--" + targetName + System.DateTime.Now.ToString("  HH:mm:ss:fff"));

                //写入版本号信息
                string assetVersionPath        = targetBuildPath + "/AssetVersion.txt";
                AssetBundleVersionInfo version = new AssetBundleVersionInfo();
                version.ManifestAssetBundle = targetName;
                version.Version             = EditorConfigInfo.AssetVersion;
                version.IsEncrypt           = (EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES;
                version.Resources           = new List <ResourcesInfo>();

                List <FileInfo> allFiles = GetAllFiles(targetBuildPath);
                for (int i = 0; i < allFiles.Count; i++)
                {
                    FileInfo fileInfo = allFiles[i];
                    if (fileInfo.Extension == ".manifest")
                    {
                        File.Delete(fileInfo.FullName);
                    }
                    else if (fileInfo.Name == "AssetVersion.txt")
                    {
                        continue;
                    }
                    //加密
                    else
                    {
                        byte[] bs = File.ReadAllBytes(fileInfo.FullName);
                        if (keyAsset != null)
                        {
                            byte[] cipbs = Encipherer.AESEncrypt(bs, keyAsset);
                            File.WriteAllBytes(fileInfo.FullName, cipbs);
                            //加密后md5的值应该刷新
                            bs = cipbs;
                        }
                        ResourcesInfo resourcesInfo = new ResourcesInfo();
                        string        fullPath      = Path.GetFullPath(targetBuildPath);
                        resourcesInfo.Name = fileInfo.FullName.Replace(fullPath + "\\", "");
                        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                        //计算字节数组的哈希值
                        byte[] toData  = md5.ComputeHash(bs);
                        string fileMD5 = "";
                        for (int j = 0; j < toData.Length; j++)
                        {
                            fileMD5 += toData[j].ToString("x2");
                        }
                        resourcesInfo.MD5 = fileMD5;
                        version.Resources.Add(resourcesInfo);
                    }
                }

                //保存AssetVersion文件
                string content = JsonUtility.ToJson(version);
                File.WriteAllText(assetVersionPath, content);
            }

            //保存平台信息文件
            string platformContent = JsonUtility.ToJson(assetPlatformVersionInfo);

            File.WriteAllText(platformVersionPath, platformContent);

            //版本号迭代
            EditorConfigInfo.AssetVersion += 1;
            SaveEditorConfigInfo();

            AssetDatabase.Refresh();

            //打开打包文件夹
            EditorUtility.OpenWithDefaultApp(buildPath);
        }