Пример #1
0
        public void WriteBundle(BuildAssetBundleOptions options, BuildTarget target, string bundleDir)
        {
            string bundleStreamPath = string.Format("{0}/{1}.ab", Application.streamingAssetsPath, bundleName);
            string bundlePath       = string.Format("{0}/{1}.ab", bundleDir, bundleName);

            Debug.Log(string.Format("Building {0}", bundleShortName));

            if (node.isIndependent && !node.hasChild)
            {
                BuildPipeline.PushAssetDependencies();
            }

            uint crc = 0;

            if (!string.IsNullOrEmpty(mainAsset))
            {
                if (mainAsset.EndsWith(".unity"))
                {
                    BuildPipeline.BuildStreamedSceneAssetBundle(new string[] { mainAsset }, bundleStreamPath, target, out crc, BuildOptions.UncompressedAssetBundle);
                }
                else
                {
                    BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath(mainAsset), null, bundleStreamPath, out crc, options, target);
                }
            }
            else
            {
                List <Object> objs = new List <Object>();
                assets.ForEach(a => objs.Add(AssetDatabase.LoadMainAssetAtPath(a)));
                if (objs.Count == 0)
                {
                    Debug.LogError(string.Format("No assets found for the asset bundle: {0}", bundleShortName));
                    return;
                }
                BuildPipeline.BuildAssetBundle(null, objs.ToArray(), bundleStreamPath, out crc, options, target);
            }

            if (node.isIndependent && !node.hasChild)
            {
                BuildPipeline.PopAssetDependencies();
            }

            bundleCRC = crc.ToString();

            LZMAUtil.CompressFile(bundleStreamPath, bundlePath);
            FileInfo fi = new FileInfo(bundlePath);

            size = fi.Length;
        }
Пример #2
0
        private IEnumerator CheckUpdate()
        {
            if (!Directory.Exists(_pathResolver.bundleDir))
            {
                Directory.CreateDirectory(_pathResolver.bundleDir);
            }

            // 下载bundle.info文件至
            Debug.Log("Download bundle.info...");
            WWW www = new WWW(string.Format("{0}/{1}", _pathResolver.bundleURL, _pathResolver.dependInfoFileName));

            yield return(www);

            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.LogError(www.error);
                www.Dispose();
                //TODO:下载失败处理
                yield break;
            }

            // 写入persist目录并命名为bundle.info.new
            File.WriteAllBytes(string.Format("{0}/{1}", _pathResolver.bundleDir, _pathResolver.dependInfoFileNameNew), www.bytes);
            www.Dispose();
            yield return(null);

            // 下载完成,解析bundle.info.new文件
            LoadDependInfo(out _dependInfoReaderNew, _pathResolver.dependInfoFileNameNew);

            // 比较版本号,当前是最新版本的话跳过更新
            if (_dependInfoReader.version >= _dependInfoReaderNew.version)
            {
                File.Delete(Path.Combine(_pathResolver.bundleDir, _pathResolver.dependInfoFileNameNew));

                if (onInited != null)
                {
                    onInited();
                }
                //Util.EventDispatcher.TriggerEvent(Global.UpdateFinish);

                yield break;
            }

            // 删除最新版已经删除但是persist下还存在的ab
            foreach (var info in _dependInfoReader.infoMap)
            {
                if (!_dependInfoReaderNew.infoMap.ContainsKey(info.Key))
                {
                    File.Delete(string.Format("{0}/{1}.ab", _pathResolver.bundleDir, info.Key));
                }
            }

            // 统计需要更新的文件及大小
            long needUpdateSize = 0;
            Dictionary <string, long> needUpdateABs = new Dictionary <string, long>();

            foreach (var info in _dependInfoReaderNew.infoMap)
            {
                string          abName = info.Key;
                AssetBundleData abData = info.Value;
                if (!_dependInfoReader.infoMap.ContainsKey(abName) || _dependInfoReader.infoMap[abName].hash != abData.hash)
                {
                    needUpdateABs.Add(abName, abData.size);
                    needUpdateSize += abData.size;
                }
            }

            // 更新
            long updatedSize = 0;

            foreach (var ab in needUpdateABs)
            {
                string abName = ab.Key;
                Debug.Log(string.Format("Update {0}...", abName));

                www = new WWW(Path.Combine(_pathResolver.bundleURL, abName));
                yield return(www);

                if (!string.IsNullOrEmpty(www.error))
                {
                    Debug.LogError(www.error);
                    www.Dispose();
                    continue;
                }

                string abPath = Path.Combine(_pathResolver.bundleDir, abName);
                File.WriteAllBytes(abPath, www.bytes);
                www.Dispose();

                // 解压
                string decompressPath = string.Format("{0}.tmp", abPath);
                LZMAUtil.DecompressFile(abPath, decompressPath);
                File.Delete(abPath);
                yield return(null);

                File.Move(decompressPath, abPath);

                updatedSize += ab.Value;
            }

            // 覆盖bundle.info文件
            string dependInfoFilePath    = Path.Combine(_pathResolver.bundleDir, _pathResolver.dependInfoFileName);
            string newDependInfoFilePath = Path.Combine(_pathResolver.bundleDir, _pathResolver.dependInfoFileNameNew);

            File.Delete(dependInfoFilePath);
            File.Move(newDependInfoFilePath, dependInfoFilePath);

            _dependInfoReader    = _dependInfoReaderNew;
            _dependInfoReaderNew = null;

            // 初始化完成
            if (onInited != null)
            {
                onInited();
            }
        }