Esempio n. 1
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");
                }
            }
        }
Esempio n. 2
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++)
            {
                if (!bundleDir.Exists)
                {
                    continue;
                }

                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;
                }
            }
        }
        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);

                //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.Close();
        }
        void BuildExportTree(AssetTarget parent, List <List <AssetTarget> > tree, int currentLevel)
        {
            if (parent.level == -1 && parent.type != AssetType.Builtin)
            {
                List <AssetTarget> levelList = null;
                if (tree.Count > currentLevel)
                {
                    levelList = tree[currentLevel];
                }
                else
                {
                    levelList = new List <AssetTarget>();
                    tree.Add(levelList);
                }
                levelList.Add(parent);
                parent.UpdateLevel(currentLevel + 1, levelList);

                foreach (AssetTarget ei in parent.dependsChildren)
                {
                    if (ei.level != -1 && ei.level <= parent.level)
                    {
                        ei.UpdateLevel(-1, null);
                    }
                    BuildExportTree(ei, tree, currentLevel + 1);
                }
            }
        }
Esempio n. 5
0
        public void ExportImp()
        {
            AssetBundleManager.Log("building... cur Time " + Time.realtimeSinceStartup);

            List <AssetBundleBuild> list = new List <AssetBundleBuild>();
            //标记所有 asset bundle name
            var all      = AssetBundleUtils.GetAll();
            var abAssets = new Dictionary <string, HashSet <AssetTarget> >();

            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.needSelfExport)
                {
                    if (!abAssets.ContainsKey(target.abFileName))
                    {
                        abAssets.Add(target.abFileName, new HashSet <AssetTarget>());
                    }
                    abAssets[target.abFileName].Add(target);
                    AssetBundleBuild build = new AssetBundleBuild();
                    build.assetBundleName = target.abFileName;
                    build.assetNames      = new string[] { target.assetPath };
                    list.Add(build);
                }
            }

            AssetBundleManager.Log("building... cur Time " + Time.realtimeSinceStartup);
            //开始打包
            BuildAssetBundleOptions buildOptions = BuildAssetBundleOptions.DeterministicAssetBundle |
                                                   BuildAssetBundleOptions.ChunkBasedCompression |
                                                   BuildAssetBundleOptions.DisableWriteTypeTree;

            BuildPipeline.BuildAssetBundles(pathResolver.BundleSavePath, list.ToArray(), buildOptions,
                                            EditorUserBuildSettings.activeBuildTarget);
            AssetBundleManager.Log("building... cur Time " + Time.realtimeSinceStartup);
            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.abFileName);
                    target.bundleCrc = hash.ToString();
                }
            }
            this.SaveDepAll(all, manifest, abAssets);
            this.RemoveUnused(all, manifest);
            ab.Unload(true);
            AssetBundleManager.Log("building... cur Time " + Time.realtimeSinceStartup);
            AssetDatabase.RemoveUnusedAssetBundleNames();
            AssetDatabase.Refresh();
            AssetBundleManager.Log("building... cur Time " + Time.realtimeSinceStartup);
        }
        public override void Export()
        {
            base.Export();

            List <AssetBundleBuild> list = new List <AssetBundleBuild>();
            //标记所有 asset bundle name
            var all = AssetBundleUtils.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);
                }
            }
            ///加入图集
            for (int index = 1; index <= 1; index++)
            {
                Debug.Log(index);
                string           assetResPath = "Assets/Textures/Common" + index;
                AssetBundleBuild rABB         = new AssetBundleBuild();
                rABB.assetBundleName    = "common" + index;
                rABB.assetBundleVariant = "ab";
                rABB.assetNames         = new string[] { assetResPath };
                list.Add(rABB);
            }
            //开始打包
            BuildPipeline.BuildAssetBundles(pathResolver.BundleSavePath, list.ToArray(), BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
#if UNITY_5_1 || UNITY_5_2
            AssetBundle ab = AssetBundle.CreateFromFile(pathResolver.BundleSavePath + "/AssetBundles");
#else
            AssetBundle ab = AssetBundle.LoadFromFile(pathResolver.BundleSavePath + "/AssetBundles");
#endif
            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();
        }
Esempio n. 7
0
        public override void Export()
        {
            base.Export();

            //标记所有 asset bundle name
            var all = AssetBundleUtils.GetAll();

            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget   target   = all[i];
                AssetImporter importer = AssetImporter.GetAtPath(target.assetPath);
                if (importer)
                {
                    if (target.needSelfExport)
                    {
                        importer.assetBundleName = target.bundleName;
                    }
                    else
                    {
                        importer.assetBundleName = null;
                    }
                }
            }

            //开始打包
            BuildPipeline.BuildAssetBundles(this.pathResolver.BundleSavePath, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);

#if UNITY_5_1 || UNITY_5_2
            AssetBundle ab = AssetBundle.CreateFromFile(pathResolver.BundleSavePath + "/AssetBundles");
#else
            AssetBundle ab = AssetBundle.LoadFromFile(pathResolver.BundleSavePath + "/AssetBundles");
#endif
            AssetBundleManifest manifest = ab.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
            //清除所有 asset bundle name
            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                Hash128     hash   = manifest.GetAssetBundleHash(target.bundleName);
                target.bundleCrc = hash.ToString();

                AssetImporter importer = AssetImporter.GetAtPath(target.assetPath);
                if (importer)
                {
                    importer.assetBundleName = null;
                }
            }
            this.SaveDepAll(all);
            ab.Unload(true);
            this.RemoveUnused(all);

            AssetDatabase.RemoveUnusedAssetBundleNames();
            AssetDatabase.Refresh();
        }
        void Export(AssetTarget target)
        {
            if (target.needExport)
            {
                //写入 .assetbundle 包
                target.BuildBundle(options);

                if (target.isNewBuild)
                {
                    newBuildTargets.Add(target);
                }
            }
        }
        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();
        }
        public override void Export()
        {
            base.Export();

            List <AssetBundleBuild> list = new List <AssetBundleBuild>();
            //标记所有 asset bundle name
            var all = AssetBundleUtils.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);
                }
            }
            //这是自动根据依赖关系分析出来的bundle划分
            //接下来我们可以再自定义一部分assetbundleBuild 自定义一部分bundle  可以假设一群asset其实就是一个assetTarget来去参与分析依赖关系


            //开始打包
            BuildPipeline.BuildAssetBundles(pathResolver.BundleSavePath, list.ToArray(), BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);

#if UNITY_5_1 || UNITY_5_2
            AssetBundle ab = AssetBundle.CreateFromFile(pathResolver.BundleSavePath + "/AssetBundles");
#else
            AssetBundle ab = AssetBundle.LoadFromFile(pathResolver.BundleSavePath + "/AssetBundles");
#endif
            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();
        }
Esempio n. 11
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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        public void AddRootTargets(DirectoryInfo bundleDir, PackMode fPackMode, string parttern = null,
                                   SearchOption searchOption = SearchOption.AllDirectories)
        {
            if (string.IsNullOrEmpty(parttern))
            {
                parttern = "*.*";
            }

            FileInfo[] prefabs = bundleDir.GetFiles(parttern, searchOption);

            foreach (FileInfo file in prefabs)
            {
                if (file.Extension.Contains("meta"))
                {
                    continue;
                }
                AssetTarget target = AssetBundleUtils.Load(file);
                target.abDebugName = AssetBundleUtils.GetPackTag(bundleDir, file, fPackMode, parttern, out var abname);
                target.abFileName  = abname;
                target.exportType  = AssetBundleExportType.Root;
            }
        }
Esempio n. 14
0
        protected void SaveDepAll(List <AssetTarget> all)
        {
            string path = Path.Combine(pathResolver.BundleSavePath, pathResolver.DependFileName);

            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());
        }