Beispiel #1
0
        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);
                }
            }
        }
        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();
        }
Beispiel #3
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");
                }
            }
        }
Beispiel #4
0
        void Export(AssetTarget target)
        {
            if (target.needExport)
            {
                //写入 .assetbundle 包
                target.BuildBundle(options);

                if (target.isNewBuild)
                {
                    newBuildTargets.Add(target);
                }
            }
        }
Beispiel #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();
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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;
         }
     }
 }
Beispiel #9
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());
        }
Beispiel #10
0
        public override void Export()
        {
            #region 源代码
            base.Export();

            var platform = PathResolver.GetPlatformName();
            var filePath = Path.Combine(Path.Combine(Environment.CurrentDirectory, PathResolver.BundleSaveDirName), platform).Replace("\\", "/") + "/";
            if (Directory.Exists(filePath))
            {
                Directory.Delete(filePath, true);
            }
            Directory.CreateDirectory(filePath);

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

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

#if UNITY_5_1 || UNITY_5_2
            AssetBundle ab = AssetBundle.CreateFromFile(PathResolver.BundleSavePath + PathResolver.GetPlatformForAssetBundles(Application.platform));
#else
            var ab = AssetBundle.LoadFromFile(PathResolver.BundleSavePath + "/" + PathResolver.GetPlatformName());
#endif
            var 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);
            this.SaveSpriteAll(all);
            this.SaveAssetAll(all);
            this.ExportResourcesManifestFile(manifest);
            ab.Unload(true);
            this.RemoveUnused(all);


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

            Debug.Log("[Assets]Build Finish!");
            #endregion
        }