示例#1
0
        public void Export()
        {
            List <AssetBundleBuild> list = new List <AssetBundleBuild>();
            //标记所有 asset bundle name
            var all = AssetBundleUtil.GetAll();

            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.needSelfExport)
                {
                    AssetBundleBuild build = new AssetBundleBuild();
                    build.assetBundleName = target.bundleName;
                    build.assetNames      = new string[] { target.assetPath };
                    list.Add(build);
                }
            }
            string bundleSavePath = AssetBundleUtil.GetBundleDir();

            //开始打包
            BuildPipeline.BuildAssetBundles(bundleSavePath, list.ToArray(), BuildAssetBundleOptions.UncompressedAssetBundle, EditorUserBuildSettings.activeBuildTarget);
            AssetBundle         ab       = AssetBundle.LoadFromFile(bundleSavePath + "/AssetBundles");
            AssetBundleManifest manifest = ab.LoadAsset("AssetBundleManifest") as AssetBundleManifest;

            //hash
            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.needSelfExport)
                {
                    Hash128 hash = manifest.GetAssetBundleHash(target.bundleName);
                    if (target.bundleCrc != hash.ToString())
                    {
                        if (AssetBundleBuildPanel.ABUpdateOpen)
                        {
                            string savePath     = Path.Combine(Path.GetTempPath(), target.bundleName);
                            string downloadPath = Path.Combine(Path.Combine(AssetBundleBuildPanel.TargetFolder, "AssetBundles"), target.bundleName);
                            File.Copy(savePath, downloadPath, true);
                            XMetaResPackage pack = new XMetaResPackage();
                            pack.buildinpath = target.bundleShortName;
                            pack.download    = string.Format("AssetBundles/{0}", target.bundleName);
                            pack.Size        = (uint)(new FileInfo(downloadPath)).Length;
                            if (pack.Size != (new FileInfo(downloadPath)).Length)
                            {
                                EditorUtility.DisplayDialog("Bundle ", target.bundleShortName + " is lager than UINTMAX!!!", "OK");
                            }
                            AssetBundleBuildPanel.assetBundleUpdate.Add(pack);
                            File.Copy(savePath, bundleSavePath, true);
                        }
                    }
                    target.bundleCrc = hash.ToString();
                }
            }
            SaveDepAll(all);
            ab.Unload(true);
            RemoveUnused(all);

            AssetDatabase.RemoveUnusedAssetBundleNames();
            AssetDatabase.Refresh();
        }
示例#2
0
        /// <summary>
        /// 删除未使用的AB,可能是上次打包出来的,而这一次没生成的
        /// </summary>
        /// <param name="all"></param>
        protected void RemoveUnused(List <AssetTarget> all)
        {
            HashSet <string> usedSet = new HashSet <string>();

            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.needSelfExport)
                {
                    usedSet.Add(target.bundleName);
                }
            }

            DirectoryInfo di = new DirectoryInfo(pathResolver.BundleSavePath);

            FileInfo[] abFiles = di.GetFiles("*.ab");
            for (int i = 0; i < abFiles.Length; i++)
            {
                FileInfo fi = abFiles[i];
                if (usedSet.Add(fi.Name))
                {
                    Debug.Log("Remove unused AB : " + fi.Name);

                    fi.Delete();
                    //for U5X
                    File.Delete(fi.FullName + ".manifest");
                }
            }
        }
示例#3
0
        public virtual void Save(Stream stream, AssetTarget[] targets)
        {
            StreamWriter sw = new StreamWriter(stream);

            //写入文件头判断文件类型用,ABDT 意思即 Asset-Bundle-Data-Text
            sw.WriteLine("ABDT");

            for (int i = 0; i < targets.Length; i++)
            {
                AssetTarget           target = targets[i];
                HashSet <AssetTarget> deps   = new HashSet <AssetTarget>();
                target.GetDependencies(deps);

                //debug name
                sw.WriteLine(target.assetPath);
                //bundle name
                sw.WriteLine(target.bundleName);
                //File Name
                sw.WriteLine(target.bundleShortName);
                //hash
                sw.WriteLine(target.bundleCrc);
                //type
                sw.WriteLine((int)target.compositeType);
                //写入依赖信息
                sw.WriteLine(deps.Count);

                foreach (AssetTarget item in deps)
                {
                    sw.WriteLine(item.bundleName);
                }
                sw.WriteLine("<------------->");
            }
            sw.Close();
        }
示例#4
0
        public void AddRootTargets(FileInfo file)
        {
            AssetTarget target = AssetBundleUtil.Load(file);

            if (target == null)
            {
                XDebug.LogError(file);
            }
            target.exportType = AssetBundleExportType.Root;
        }
示例#5
0
        public override void Save(Stream stream, AssetTarget[] targets)
        {
            BinaryWriter sw = new BinaryWriter(stream);

            //写入文件头判断文件类型用,ABDB 意思即 Asset-Bundle-Data-Binary
            sw.Write(new char[] { 'A', 'B', 'D', 'B' });

            List <string> bundleNames = new List <string>();

            for (int i = 0; i < targets.Length; i++)
            {
                AssetTarget target = targets[i];
                bundleNames.Add(target.bundleName);
            }

            //写入文件名池
            sw.Write(bundleNames.Count);
            for (int i = 0; i < bundleNames.Count; i++)
            {
                sw.Write(bundleNames[i]);
            }

            //写入详细信息
            for (int i = 0; i < targets.Length; i++)
            {
                AssetTarget           target = targets[i];
                HashSet <AssetTarget> deps   = new HashSet <AssetTarget>();
                target.GetDependencies(deps);

                //debug name
                sw.Write(target.assetPath);
                //bundle name
                sw.Write(bundleNames.IndexOf(target.bundleName));
                //File Name
                sw.Write(target.bundleShortName);
                //hash
                sw.Write(target.bundleCrc);
                //type
                sw.Write((int)target.compositeType);
                //写入依赖信息
                sw.Write(deps.Count);

                foreach (AssetTarget item in deps)
                {
                    sw.Write(bundleNames.IndexOf(item.bundleName));
                }
            }
            sw.Close();
        }
示例#6
0
        public static AssetTarget Load(FileInfo file, System.Type t)
        {
            AssetTarget target   = null;
            string      fullPath = file.FullName;
            int         index    = fullPath.IndexOf("Assets");

            if (index != -1)
            {
                string assetPath = fullPath.Substring(index);
                if (_assetPath2target.ContainsKey(assetPath))
                {
                    target = _assetPath2target[assetPath];
                }
                else
                {
                    Object o = null;
                    if (t == null)
                    {
                        o = AssetDatabase.LoadMainAssetAtPath(assetPath);
                    }
                    else
                    {
                        o = AssetDatabase.LoadAssetAtPath(assetPath, t);
                    }

                    if (o != null)
                    {
                        int instanceId = o.GetInstanceID();

                        if (_object2target.ContainsKey(instanceId))
                        {
                            target = _object2target[instanceId];
                        }
                        else
                        {
                            target = new AssetTarget(o, file, assetPath);
                            string key = string.Format("{0}/{1}", assetPath, instanceId);
                            _assetPath2target[key]     = target;
                            _object2target[instanceId] = target;
                        }
                    }
                }
            }

            return(target);
        }
示例#7
0
        public static AssetTarget Load(Object o)
        {
            AssetTarget target = null;

            if (o != null)
            {
                int instanceId = o.GetInstanceID();

                if (_object2target.ContainsKey(instanceId))
                {
                    target = _object2target[instanceId];
                }
                else
                {
                    string assetPath = AssetDatabase.GetAssetPath(o);
                    string key       = assetPath;
                    //Builtin,内置素材,path为空
                    if (string.IsNullOrEmpty(assetPath))
                    {
                        key = string.Format("Builtin______{0}", o.name);
                    }
                    else
                    {
                        key = string.Format("{0}/{1}", assetPath, instanceId);
                    }

                    if (_assetPath2target.ContainsKey(key))
                    {
                        target = _assetPath2target[key];
                    }
                    else
                    {
                        if (assetPath.StartsWith("Resources"))
                        {
                            assetPath = string.Format("{0}/{1}.{2}", assetPath, o.name, o.GetType().Name);
                        }
                        FileInfo file = new FileInfo(Path.Combine(ProjectPath, assetPath));
                        target = new AssetTarget(o, file, assetPath);
                        _object2target[instanceId] = target;
                        _assetPath2target[key]     = target;
                    }
                }
            }
            return(target);
        }
示例#8
0
 public void AddRootTargets(DirectoryInfo bundleDir, string[] partterns = null, SearchOption searchOption = SearchOption.AllDirectories)
 {
     if (partterns == null)
     {
         partterns = new string[] { "*.*" }
     }
     ;
     for (int i = 0; i < partterns.Length; i++)
     {
         FileInfo[] prefabs = bundleDir.GetFiles(partterns[i], searchOption);
         foreach (FileInfo file in prefabs)
         {
             if (file.Extension.Contains("meta"))
             {
                 continue;
             }
             AssetTarget target = AssetBundleUtils.Load(file);
             target.exportType = AssetBundleExportType.Root;
         }
     }
 }
示例#9
0
        protected void SaveDepAll(List <AssetTarget> all)
        {
            string path = Path.Combine(Application.dataPath + "/Resources/", AssetBundlePathResolver.DependFileName + ".txt");

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            List <AssetTarget> exportList = new List <AssetTarget>();

            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.needSelfExport)
                {
                    exportList.Add(target);
                }
            }
            AssetBundleDataWriter writer = dataWriter;

            writer.Save(path, exportList.ToArray());
        }
示例#10
0
        public override void Export()
        {
            base.Export();

            List <AssetBundleBuild> list = new List <AssetBundleBuild>();
            //标记所有 asset bundle name
            var all = AssetBundleUtils.GetAll();

            if (_isToSign)
            {
                // YAO: 不依赖Unity自动保证资源完整性,给所有资源手动分组
                for (int i = 0; i < all.Count; i++)
                {
                    AssetTarget target = all[i];
                    if (target.needSelfExport)
                    {
                        AssetImporter asset = AssetImporter.GetAtPath(target.assetPath);
                        Debug.Log(asset.assetBundleName);
                        if (asset.assetBundleName == "")
                        {
                            asset.assetBundleName    = target.bundleName;
                            asset.assetBundleVariant = "";
                            Debug.Log(target.bundleShortName + " -> " + target.bundleName);
                        }
                    }
                    else
                    {
                        AssetTarget           root    = null;
                        HashSet <AssetTarget> rootSet = new HashSet <AssetTarget>();
                        target.GetRoot(rootSet);
                        if (rootSet.Count == 1)
                        {
                            foreach (AssetTarget dep in rootSet)
                            {
                                root = dep;
                            }
                        }
                        else
                        {
                            Debug.LogError(target.assetPath + " rootSet.Count != 1");
                        }
                        AssetImporter asset = AssetImporter.GetAtPath(target.assetPath);
                        if (asset.assetBundleName == "")
                        {
                            asset.assetBundleName    = root.bundleName;
                            asset.assetBundleVariant = "";
                            Debug.Log(target.bundleShortName + " -> " + root.bundleShortName);
                        }
                    }
                }
                AssetDatabase.Refresh();
                BuildPipeline.BuildAssetBundles(pathResolver.BundleSavePath, BuildAssetBundleOptions.UncompressedAssetBundle, EditorUserBuildSettings.activeBuildTarget);
            }
            else
            {
                for (int i = 0; i < all.Count; i++)
                {
                    AssetTarget target = all[i];
                    if (target.needSelfExport)
                    {
                        AssetBundleBuild build = new AssetBundleBuild();
                        build.assetBundleName = target.bundleName;
                        build.assetNames      = new string[] { target.assetPath };
                        list.Add(build);
                        AssetImporter asset = AssetImporter.GetAtPath(target.assetPath);
                        asset.assetBundleName    = target.bundleName;
                        asset.assetBundleVariant = "";
                        Debug.Log(target.bundleShortName + " -> " + target.bundleName);
                    }
                }
                BuildPipeline.BuildAssetBundles(pathResolver.BundleSavePath, list.ToArray(), BuildAssetBundleOptions.UncompressedAssetBundle, EditorUserBuildSettings.activeBuildTarget);
            }

            AssetBundle ab = AssetBundle.LoadFromFile(pathResolver.BundleSavePath + "/AssetBundles");

            AssetBundleManifest manifest = ab.LoadAsset("AssetBundleManifest") as AssetBundleManifest;

            //hash
            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.needSelfExport)
                {
                    Hash128 hash = manifest.GetAssetBundleHash(target.bundleName);
                    target.bundleCrc = hash.ToString();
                }
            }
            this.SaveDepAll(all);
            ab.Unload(true);
            this.RemoveUnused(all);

            AssetDatabase.RemoveUnusedAssetBundleNames();
            AssetDatabase.Refresh();

            // 处理lua
            FileUtil.DeleteFileOrDirectory(pathResolver.LuacSavePath);
            // TODO:加密
            FileUtil.CopyFileOrDirectory(AppConfigs.AssetsExportPath + "/" + AppConfigs.ScriptPath, pathResolver.LuacSavePath);
        }