private void drawHeaderBuildType(AssetElement element, Rect cellRect, AssetBuildRule buildRule) { int buildIndex = 0, newBuildIndex = 0; if (element.FileType == FileType.Folder) { buildIndex = Array.FindIndex(Styles.FolderBundleBuildEnums, ev => ev == buildRule.BuildType); newBuildIndex = EditorGUI.Popup(cellRect, buildIndex, Styles.FolderBundleBuildOptions); if (newBuildIndex != buildIndex) { buildRule.BuildType = Styles.FolderBundleBuildEnums[newBuildIndex]; this.assetTreeModel.ReflushChildrenRecursive(element); } } else { buildIndex = Array.FindIndex(Styles.FileBundleBuildEnums, ev => ev == buildRule.BuildType); newBuildIndex = EditorGUI.Popup(cellRect, buildIndex, Styles.FileBundleBuildOptions); if (newBuildIndex != buildIndex) { buildRule.BuildType = Styles.FileBundleBuildEnums[newBuildIndex]; this.assetTreeModel.ReflushChildrenRecursive(element); } } }
/// <summary> /// 搜索根目录下的所有的指定打包规则的文件 /// </summary> /// <param name="root"></param> /// <param name="ruleMap"></param> /// <returns></returns> public static List <string> SearchFiles(AssetBuildRule root, Dictionary <string, List <AssetBuildRule> > ruleMap) { if (root.BuildType == (int)BundleBuildType.Ignore) { return(null); } HashSet <string> includeSuffixs = new HashSet <string>(); List <AssetBuildRule> pathRules = ruleMap[root.Path]; for (int i = 0; i < pathRules.Count; i++) { AssetBuildRule rule = pathRules[i]; if (rule.BuildType == (int)BundleBuildType.Ignore) { continue; } string[] extends = GetFileExtension(rule.FileFilterType); for (int j = 0; j < extends.Length; j++) { includeSuffixs.Add(extends[j]); } } return(searchFilesRecuivse(root.Path, ruleMap, includeSuffixs)); }
public static void SetAssetbundleName(AssetImporter importer, AssetBuildRule bundleRule) { string bundleName = FormatBundleName(bundleRule); importer.assetBundleName = bundleName; importer.assetBundleVariant = BuilderPreference.VARIANT_V1; }
private bool findParentRecursive(AssetBuildRule buildRule, AssetBuildRule parent) { if (buildRule.Equals(parent) || !buildRule.Path.StartsWith(parent.Path + "/")) { return(false); } bool result = false; if (parent.Childrens != null) { foreach (AssetBuildRule child in parent.Childrens) { result = findParentRecursive(buildRule, child); if (result) { break; } } } if (!result) { parent.AddChild(buildRule); } return(true); }
public void InitChildElements(AssetBuildRule rule, AssetElement parent) { string suffix = Path.GetExtension(rule.Path); FileSystemInfo fsi = null; if (string.IsNullOrEmpty(suffix)) { fsi = new DirectoryInfo(rule.Path); } else { fsi = new FileInfo(rule.Path); } AssetElement newEle = CreateTreeViewItemForGameObject(fsi, rule, parent, Int32.MaxValue); m_Data.Add(newEle); if (rule.Childrens != null) { for (int i = 0; i < rule.Childrens.Length; i++) { InitChildElements(rule.Childrens[i], newEle); } } }
/// <summary> /// 格式化Bundle名称,添加文件类型,避免不同目录Bundle同名 /// </summary> /// <param name="rule"></param> /// <returns></returns> public static string FormatBundleName(AssetBuildRule rule) { if (rule.FileFilterType == FileType.Shader || rule.FileFilterType == FileType.Font) { return(rule.AssetBundleName); //特殊处理shader/font } return(string.Format("{0}_{1}", rule.AssetBundleName, rule.FileFilterType).ToLower()); }
public AssetElement(string name, int depth, int id) { this.name = name; this.depth = depth; this.id = id; FileType = FileType.Folder; BuildRule = new AssetBuildRule(); BuildRule.FileFilterType = FileType; }
public AssetElement(FileSystemInfo info, AssetBuildRule rule) { this.name = info.Name; fileName = this.name.Split('.')[0].ToLower(); FileType = BuildUtil.GetFileType(info); BuildRule = rule; depth = BuildRule.Path.Split('/').Length - 1; GUID = AssetDatabase.AssetPathToGUID(BuildRule.Path); }
public void RemoveChild(AssetBuildRule rule) { if (Childrens == null) { return; } List <AssetBuildRule> list = new List <AssetBuildRule>(Childrens); list.Remove(rule); Childrens = list.ToArray(); }
public void TreeToList(AssetBuildRule rule, List <AssetBuildRule> buildRules) { buildRules.Add(rule); if (rule.Childrens == null) { return; } foreach (AssetBuildRule childRule in rule.Childrens) { TreeToList(childRule, buildRules); } }
public AssetBuildRule Copy() { AssetBuildRule copyRule = new AssetBuildRule(); copyRule.Path = Path; copyRule.AssetBundleName = AssetBundleName; copyRule.Order = Order; copyRule.FileFilterType = FileFilterType; copyRule.BuildType = BuildType; copyRule.LoadType = LoadType; copyRule.PackageType = PackageType; copyRule.DownloadOrder = DownloadOrder; return(copyRule); }
public AssetElement(FileSystemInfo info) { this.name = info.Name; fileName = this.name.Split('.')[0].ToLower(); FileType = BuildUtil.GetFileType(info); BuildRule = new AssetBuildRule(); BuildRule.AssetBundleName = info.Name.ToLower(); BuildRule.Path = BuildUtil.RelativePaths(info.FullName); BuildRule.FileFilterType = FileType; BuildRule.Order = BuildUtil.GetFileOrder(FileType); BuildRule.BuildType = (int)(FileType != FileType.Folder ? BundleBuildType.TogetherFiles : BundleBuildType.Separate); depth = BuildRule.Path.Split('/').Length - 1; GUID = AssetDatabase.AssetPathToGUID(BuildRule.Path); }
public void InitTreeElement(AssetBuildRule rule) { index++; AssetElement ele = AddParentRecursive(rule.Path, m_Data); ele.BuildRule = rule; m_Data.Add(ele); if (rule.Childrens != null) { for (int i = 0; i < rule.Childrens.Length; i++) { InitChildElements(rule.Childrens[i], ele); } } }
static AssetElement CreateTreeViewItemForGameObject(FileSystemInfo file, AssetBuildRule rule, AssetElement parent, int insertIndex) { // We can use the GameObject instanceID for TreeViewItem id, as it ensured to be unique among other items in the tree. // To optimize reload time we could delay fetching the transform.name until it used for rendering (prevents allocating strings // for items not rendered in large trees) // We just set depth to -1 here and then call SetupDepthsFromParentsAndChildren at the end of BuildRootAndRows to set the depths. index++; AssetElement newEle = new AssetElement(file, rule); newEle.id = index; setParent(newEle, parent, insertIndex); return(newEle); }
public void SaveConfig(AssetBuildRule[] rules) { if (rules == null || rules.Length <= 0) { return; } string defaultConfigPath = BuilderPreference.DEFAULT_CONFIG_NAME; if (File.Exists(defaultConfigPath)) { File.Delete(defaultConfigPath); } this.rootRules = rules; Dictionary <string, AssetBuildRule> ruleMap = new Dictionary <string, AssetBuildRule>(); for (int i = 0; i < rules.Length; i++) { List <AssetBuildRule> treeList = rules[i].TreeToList(); for (int j = 0; j < treeList.Count; j++) { ruleMap[treeList[j].Path] = treeList[j]; } } AssetBuildRule[] ruleArr = new AssetBuildRule[ruleMap.Count]; ruleMap.Values.CopyTo(ruleArr, 0); Array.Sort(ruleArr, (x, y) => x.Path.CompareTo(y.Path)); ABConfigs configs = ScriptableObject.CreateInstance <ABConfigs>(); configs.Rules = ruleArr; if (File.Exists(defaultConfigPath)) { AssetDatabase.DeleteAsset(defaultConfigPath); } AssetDatabase.CreateAsset(configs, defaultConfigPath); // Debug.Log("<color=#2fd95b>Save Success !</color>"); }
public void AddChild(AssetBuildRule childRule) { List <AssetBuildRule> list = new List <AssetBuildRule>(); if (Childrens != null) { list.AddRange(Childrens); } if (list.Contains(childRule)) { return; } list.Add(childRule); Childrens = list.ToArray(); }
public void Save() { if (root.children == null || root.children.Count <= 0) { string defaultConfigPath = BuilderPreference.DEFAULT_CONFIG_NAME; if (File.Exists(defaultConfigPath)) { File.Delete(defaultConfigPath); AssetDatabase.Refresh(); } return; } AssetBuildRule[] rules = new AssetBuildRule[root.children.Count]; for (int i = 0; i < root.children.Count; i++) { rules[i] = ((AssetElement)root.children[i]).BuildRule; } rulManger.SaveConfig(rules); }
/// <summary> /// 查找最小打包顺序的打包规则 /// </summary> /// <returns></returns> private AssetBuildRule findRuleByOrder(List <AssetMap> assets, AssetBuildRule srcRule) { int minOrder = srcRule.Order; AssetBuildRule rule = srcRule; int sceneUnityRef = 0; //是否是场景引用的资源 for (int i = 0; i < assets.Count; i++) { AssetBuildRule assetRule = assets[i].Rule; if (assetRule.Path.EndsWith(".unity")) { sceneUnityRef++; } if (assetRule.Order < minOrder) { minOrder = assetRule.Order; rule = assetRule; } } if (sceneUnityRef > 0) { int ruleOrder = rule.Order; int sceneOrder = BuildUtil.GetFileOrder(FileType.Scene); rule = null; if (sceneUnityRef > 1 && ruleOrder >= sceneOrder) { //存在多个场景引用,就打到公共场景资源包内 rule = new AssetBuildRule(); rule.Order = sceneOrder - 100; rule.AssetBundleName = "scenes/scene_publics"; } } return(rule); }
/// <summary> /// 绘制Bundle Rule 详细配置 /// </summary> private void drawBundleRulePropertys() { EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); GUILayout.Toggle(true, "Bunld Rule Propertys", EditorStyles.toolbarButton, GUILayout.MaxWidth(120)); GUILayout.Toolbar(0, new[] { "" }, EditorStyles.toolbar, GUILayout.ExpandWidth(true)); EditorGUILayout.EndHorizontal(); GUILayout.Space(5); AssetElement lastClickItem = treeModel.Find(this.treeViewState.lastClickedID); if (lastClickItem != null && lastClickItem.BuildRule != null) { AssetBuildRule bundleRule = lastClickItem.BuildRule; EditorGUIUtility.labelWidth = 120; if (EditorGUILayout.Toggle("Preload", bundleRule.LoadType == ELoadType.PreLoad)) { bundleRule.LoadType = ELoadType.PreLoad; } bundleRule.PackageType = (PackageAssetType)EditorGUILayout.EnumPopup("Package Type", bundleRule.PackageType); if (bundleRule.PackageType == PackageAssetType.OutPackage) { //非整包资源 bundleRule.DownloadOrder = EditorGUILayout.IntField("Download Order", bundleRule.DownloadOrder); } // GUILayout.BeginHorizontal(); // GUILayout.Label("Path", GUILayout.Width(50)); // EditorGUILayout.TextField(bundleRule.Path); // GUILayout.EndHorizontal(); GUILayout.Space(5); GUILayout.TextArea(bundleRule.Path, GUILayout.MaxWidth(letfGUIWidth - 10), GUILayout.MaxHeight(40)); } }
//设置依赖文件的Assetbundle分配 // 1.生成引用与依赖的映射关系 // 2.查找打包规则中的最小Order进行设置 private void checkDependency(Dictionary <string, AssetMap> files) { //设置依赖文件的Assetbundle名称 foreach (AssetMap asset in files.Values) { if (!asset.IsBinding) { continue; //过滤非显示bundle rule下的资源 } List <AssetMap> dependencys = asset.Dependencys; if (dependencys == null) { continue; } for (int j = 0; j < dependencys.Count; j++) { AssetImporter importer = AssetImporter.GetAtPath(dependencys[j].AssetPath); if (!string.IsNullOrEmpty(importer.assetBundleName)) { continue; //已经被设置 } AssetMap depAsset = dependencys[j]; //查询引用文件中最小的Order,使用最小Order的Assetbundle名称 AssetBuildRule depAssetRule = findRuleByOrder(depAsset.References, depAsset.Rule); if (depAssetRule == null || depAssetRule.BuildType == (int)BundleBuildType.Ignore) { continue; } // Builder.AddBuildLog(string.Format("set dep assetbundle name , path {0} : {1}", depAsset.AssetPath, depAssetRule.AssetBundleName)); BuildUtil.SetAssetbundleName(importer, depAssetRule); } } }
public AssetMap(string path, AssetBuildRule rule) { this.assetPath = path; this.rule = rule; }
private static List <string> searchFilesRecuivse(string rootPath, Dictionary <string, List <AssetBuildRule> > ruleMap, HashSet <string> includeSuffixs) { DirectoryInfo dirInfo = new DirectoryInfo(rootPath); if (!dirInfo.Exists) { return(null); } List <string> files = new List <string>(); string[] allFiles = Directory.GetFiles(rootPath, "*.*", SearchOption.TopDirectoryOnly) .Where(f => includeSuffixs.Contains(Path.GetExtension(f).ToLower())).ToArray(); for (int i = 0; i < allFiles.Length; i++) { string relativePath = BuildUtil.RelativePaths(allFiles[i]); bool isIgnore = false; List <AssetBuildRule> buildRule = null; if (ruleMap.TryGetValue(relativePath, out buildRule)) { //一个文件只有一个规则 if (buildRule.Count > 1) { Debug.LogError("One file have mul bundle rules !" + buildRule.Count + ", path is " + relativePath); } isIgnore = buildRule[0].BuildType == (int)BundleBuildType.Ignore; } if (!isIgnore) { files.Add(relativePath); } } DirectoryInfo[] childDirs = dirInfo.GetDirectories(); foreach (DirectoryInfo childDir in childDirs) { List <AssetBuildRule> buildRules = null; string relativePath = BuildUtil.RelativePaths(childDir.FullName); List <string> childFiles = null; if (ruleMap.TryGetValue(relativePath, out buildRules)) { HashSet <string> newIncludeSuffixs = new HashSet <string>(); for (int i = 0; i < buildRules.Count; i++) { AssetBuildRule rule = buildRules[i]; if (rule.BuildType == (int)BundleBuildType.Ignore) { continue; } string[] extends = GetFileExtension(rule.FileFilterType); for (int j = 0; j < extends.Length; j++) { newIncludeSuffixs.Add(extends[j]); } } childFiles = searchFilesRecuivse(relativePath, ruleMap, newIncludeSuffixs); } else { childFiles = searchFilesRecuivse(relativePath, ruleMap, includeSuffixs); } if (childFiles != null) { files.AddRange(childFiles); } } return(files); }
public static void SetAssetbundleName(string path, AssetBuildRule bundleRule) { AssetImporter importer = AssetImporter.GetAtPath(path); SetAssetbundleName(importer, bundleRule); }
void CellGUI(Rect cellRect, TreeViewItem <AssetElement> item, AssetTreeHeader column, ref RowGUIArgs args) { // Center cell rect vertically (makes it easier to place controls, icons etc in the cells) CenterRectUsingSingleLineHeight(ref cellRect); AssetElement element = item.data; AssetBuildRule buildRule = element.BuildRule; switch (column) { case AssetTreeHeader.Icon: { // EditorGUI.BeginDisabledGroup(!item.data.IsBuild); if (buildRule.BuildType == 0) { GUI.DrawTexture(cellRect, iconIgnore, ScaleMode.ScaleToFit); } else { GUI.DrawTexture(cellRect, icons[GetIconByIndex(item)], ScaleMode.ScaleToFit); } // EditorGUI.EndDisabledGroup(); } break; //case MyColumns.Icon2: // { // GUI.DrawTexture(cellRect, s_TestIcons[GetIcon2Index(item)], ScaleMode.ScaleToFit); // } // break; case AssetTreeHeader.AssetName: { if (!Toggle) { args.rowRect = cellRect; base.RowGUI(args); } else { // Do toggle Rect toggleRect = cellRect; toggleRect.x += GetContentIndent(item); toggleRect.width = kToggleWidth; if (toggleRect.xMax < cellRect.xMax) { // EditorGUI.BeginDisabledGroup(false); bool beforeToggle = item.data.Toggle; // item.data.IsBuild = EditorGUI.Toggle(toggleRect, item.data.IsBuild); // hide when outside cell rect item.data.Toggle = EditorGUI.Toggle(toggleRect, item.data.Toggle); if (item.data.Toggle != beforeToggle) { // if (element.FileType == FileType.Folder) // { // if (item.data.Before != item.data.IsBuild) // { if (item.data.children != null) { for (int i = 0; i < item.data.children.Count; i++) { item.data.children[i].Toggle = item.data.Toggle; } } // } // item.data.Before = item.data.IsBuild; // } } // EditorGUI.EndDisabledGroup(); } // EditorGUI.BeginDisabledGroup(false);//!item.data.IsBuild // Default icon and label args.rowRect = cellRect; base.RowGUI(args); // EditorGUI.EndDisabledGroup(); } } break; case AssetTreeHeader.NameAB: buildRule.AssetBundleName = EditorGUI.TextField(cellRect, buildRule.AssetBundleName); // break; case AssetTreeHeader.Order: int newOrder = EditorGUI.IntField(cellRect, buildRule.Order); if (newOrder != buildRule.Order) { buildRule.Order = Math.Min(newOrder, BuildUtil.GetFileOrder(buildRule.FileFilterType) + 999); } break; case AssetTreeHeader.File: if (element.FileType == FileType.Folder) { FileType fileFilterType = (FileType)EditorGUI.EnumPopup(cellRect, buildRule.FileFilterType); if (fileFilterType != buildRule.FileFilterType) { buildRule.FileFilterType = fileFilterType; buildRule.Order = BuildUtil.GetFileOrder(fileFilterType); //刷新子结点 assetTreeModel.AddChildrens(new [] { element.id }); } } else { EditorGUI.LabelField(cellRect, buildRule.FileFilterType.ToString()); } break; case AssetTreeHeader.Build: drawHeaderBuildType(element, cellRect, buildRule); break; // case AssetTreeHeader.Ignore: // buildRule.BuildType = (PackageAssetType)EditorGUI.EnumPopup(cellRect, buildRule.BuildType); // break; } }
/// <summary> /// 设置资源的AB名称 /// </summary> private void SetAbName() { string savePath = BuilderPreference.BUILD_PATH + "/tempsizefile.txt"; if (File.Exists(savePath)) { File.Delete(savePath); } AssetDatabase.Refresh(); // 设置ab名 AssetBuildRule[] rules = AssetBuildRuleManager.Instance.Rules; Builder.AddBuildLog("<Assetbundle Building> Start set AssetBundleName..."); Dictionary <string, List <AssetBuildRule> > path2ruleMap = new Dictionary <string, List <AssetBuildRule> >(); for (int i = 0; i < rules.Length; i++) { List <AssetBuildRule> ruleList = rules[i].TreeToList(); for (int j = 0; j < ruleList.Count; j++) { AssetBuildRule rule = ruleList[j]; List <AssetBuildRule> pathRules = null; if (!path2ruleMap.TryGetValue(rule.Path, out pathRules)) { pathRules = new List <AssetBuildRule>(); path2ruleMap[rule.Path] = pathRules; } pathRules.Add(rule); } } //获取根目录下的所有文件 List <string> files = new List <string>(); for (int i = 0; i < rules.Length; i++) { List <string> rootFiles = BuildUtil.SearchFiles(rules[i], path2ruleMap); if (rootFiles != null) { files.AddRange(rootFiles); } } Builder.AssetMaps = new Dictionary <string, AssetMap>(); Dictionary <string, AssetMap> assetMaps = Builder.AssetMaps; //构建映射关系 for (int i = 0; i < files.Count; i++) { AssetMap fileAssetMap = null; FileType fileType = BuildUtil.GetFileType(new FileInfo(files[i])); if (!assetMaps.TryGetValue(files[i], out fileAssetMap)) { AssetBuildRule rule = findRuleByPath(files[i], path2ruleMap, fileType); if (rule == null) { Debug.LogError("Cant find bundle rule!" + files[i]); } fileAssetMap = new AssetMap(files[i], rule); assetMaps[files[i]] = fileAssetMap; } fileAssetMap.IsBinding = true; //显示设置bundle规则的文件 //被忽略的规则不查找依赖 if (fileAssetMap.Rule.BuildType == (int)BundleBuildType.Ignore) { continue; } string[] dependency = AssetDatabase.GetDependencies(files[i]); for (int j = 0; j < dependency.Length; j++) { string relativePath = BuildUtil.Replace(dependency[j]); string extension = Path.GetExtension(dependency[j]); if (BuilderPreference.ExcludeFiles.Contains(extension) || relativePath.Equals(files[i])) { continue; } AssetMap assetMap = null; FileType depFileType = BuildUtil.GetFileType(new FileInfo(relativePath)); if (!assetMaps.TryGetValue(relativePath, out assetMap)) { AssetBuildRule rule = findRuleByPath(relativePath, path2ruleMap, depFileType); rule = rule == null ? fileAssetMap.Rule : rule; assetMap = new AssetMap(relativePath, rule); assetMaps[relativePath] = assetMap; } assetMap.AddReference(fileAssetMap); fileAssetMap.AddDependency(assetMap); } } //根据明确的子目录设置AB名,即定义了指定的打包规则的目录 foreach (AssetMap asset in assetMaps.Values) { if (asset.Rule.BuildType == (int)BundleBuildType.Ignore || !asset.IsBinding) { continue; } // Builder.AddBuildLog(string.Format("set assetbundle name , path {0} : {1}", asset.AssetPath, asset.Rule.AssetBundleName)); BuildUtil.SetAssetbundleName(asset.AssetPath, asset.Rule); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); Builder.AddBuildLog("<Assetbundle Building> set assetbundle name ... end"); //设置依赖文件的Assetbundle分配 this.checkDependency(assetMaps); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); Builder.AddBuildLog("<Assetbundle Building> Check Dependency... end"); }