protected void AddRootTargets(AssetBundleBuildRuleData.Rule rule)
        {
            var bundleDir = new DirectoryInfo(rule.directory);
            //Debug.Log(bundleDir.FullName);
            var          patterns     = rule.patterns;
            SearchOption searchOption = rule.recursion ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

            for (int i = 0; i < patterns.Length; i++)
            {
                FileInfo[] prefabs = bundleDir.GetFiles(patterns[i], searchOption);
                foreach (FileInfo file in prefabs)
                {
                    if (file.Extension.Contains("meta"))
                    {
                        continue;
                    }

                    AssetTarget target = AssetBundleUtils.Load(file);
                    target.exportType = AssetBundleExportType.Root;
                    if (!string.IsNullOrEmpty(rule.bundle))
                    {
                        target.SetBundleName(Path.Combine(rule.directory, rule.bundle));
                    }
                }
            }
        }
        public virtual void Export()
        {
            this.Analyze();


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

            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.NeedSelfExport)
                {
                    string assetName = target.assetPath.Replace('\\', '/');
                    if (dic.ContainsKey(target.bundleName))
                    {
                        AssetBundleBuild build = dic[target.bundleName];
                        string[]         ans   = new string[build.assetNames.Length + 1];
                        for (int j = 0; j < build.assetNames.Length; j++)
                        {
                            ans[j] = build.assetNames[j];
                        }
                        ans[build.assetNames.Length] = assetName;
                        build.assetNames             = ans;
                        dic[target.bundleName]       = build;
                    }
                    else
                    {
                        AssetBundleBuild build = new AssetBundleBuild()
                        {
                            assetBundleName = target.bundleName,
                            assetNames      = new string[] { assetName },
                        };
                        dic[target.bundleName] = build;
                    }
                }
            }

            AssetBundleBuild[] builds = dic.Values.ToArray();


            //开始打包
            AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(pathResolver.BundleSavePath, builds, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);

            //
            CheckAssetBundleName(manifest, builds);

            SaveFileList(builds);

            //移除无用资源
            RemoveUnused(manifest);


            //
            AssetDatabase.RemoveUnusedAssetBundleNames();
            AssetDatabase.Refresh();
        }
Exemple #3
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);
        }