/// <summary> /// 遍历指定目录以及子目录,生成默认数据 /// </summary> static AssetBundleBuildData.AssetBuild.Element GenerateAssetBundleRuleData(string path , emAssetBundleNameRule rule = emAssetBundleNameRule.None) { try { AssetBundleBuildData.AssetBuild.Element result = null; if (Directory.Exists(path)) { if (!EditorCommon.IsIgnoreFolder(path)) { DirectoryInfo dir_info = new DirectoryInfo(path); //生成自身信息 result = new AssetBundleBuildData.AssetBuild.Element(dir_info.Name); result.Rule = (int)rule; result.IsFolder = true; //遍历所有文件夹 foreach (DirectoryInfo d in dir_info.GetDirectories()) { string str = d.ToString(); AssetBundleBuildData.AssetBuild.Element child = GenerateAssetBundleRuleData(str); if (child != null) { result.Add(child); } } //遍历所有子文件 foreach (FileInfo f in dir_info.GetFiles()) //查找文件 { string str = f.ToString(); AssetBundleBuildData.AssetBuild.Element child = GenerateAssetBundleRuleData(str); if (child != null) { result.Add(child); } } result.SortChildren(); } } else if (File.Exists(path)) { if (!EditorCommon.IsIgnoreFile(path)) { //生成自身信息 FileInfo info = new FileInfo(path); result = new AssetBundleBuildData.AssetBuild.Element(info.Name); result.Rule = (int)rule; result.IsFolder = false; } } return(result); } catch (System.Exception e) { Debug.LogWarning(e.Message); } return(null); }
/// <summary> /// 调整数据(匹配现有的文件&文件夹结构,删除无用的数据) /// </summary> static void MatchAssetRuleElement(string path, AssetBundleBuildData.AssetBuild.Element element) { try { if (Directory.Exists(path)) { uint bit_0 = 0x1; // 存在数据中 uint bit_1 = 0x2; // 存在文件或文件夹 Dictionary <string, uint> folder_dic = new Dictionary <string, uint>(); Dictionary <string, uint> file_dic = new Dictionary <string, uint>(); if (element.Children != null && element.Children.Count > 0) { foreach (var elem in element.Children) { if (elem.IsFolder) { if (!folder_dic.ContainsKey(elem.Name)) { folder_dic.Add(elem.Name, bit_0); } else { folder_dic[elem.Name] |= bit_0; } } else { if (!file_dic.ContainsKey(elem.Name)) { file_dic.Add(elem.Name, bit_0); } else { file_dic[elem.Name] |= bit_0; } } } } DirectoryInfo dir_info = new DirectoryInfo(path); foreach (DirectoryInfo d in dir_info.GetDirectories()) { if (EditorCommon.IsIgnoreFolder(d.Name)) { continue; } if (!folder_dic.ContainsKey(d.Name)) { folder_dic.Add(d.Name, bit_1); } else { folder_dic[d.Name] |= bit_1; } } foreach (FileInfo f in dir_info.GetFiles()) { if (EditorCommon.IsIgnoreFile(f.Name)) { continue; } if (!file_dic.ContainsKey(f.Name)) { file_dic.Add(f.Name, bit_1); } else { file_dic[f.Name] |= bit_1; } } //删除不存在的文件夹或文件 if (element.Children != null && element.Children.Count > 0) { element.Children.RemoveAll((elem) => { if (elem.IsFolder) { return(folder_dic[elem.Name] == bit_0); } else { return(file_dic[elem.Name] == bit_0); } }); //更新子文件夹数据 for (int i = 0; i < element.Children.Count; ++i) { if (element.Children[i].IsFolder) { string full_name = path + "/" + element.Children[i].Name; MatchAssetRuleElement(full_name, element.Children[i]); } } } //增加文件夹 foreach (var pair in folder_dic) { if (pair.Value == bit_1) { string full_name = path + "/" + pair.Key; element.Add(GenerateAssetBundleRuleData(full_name, (emAssetBundleNameRule)element.Rule)); } } //增加文件 foreach (var pair in file_dic) { if (pair.Value == bit_1) { string full_name = path + "/" + pair.Key; element.Add(GenerateAssetBundleRuleData(full_name, (emAssetBundleNameRule)element.Rule)); } } if ((folder_dic != null && folder_dic.Count > 0) || (file_dic != null && file_dic.Count > 0)) { element.SortChildren(); } } } catch (System.Exception ex) { Debug.LogWarning(ex.Message); } }
/// <summary> /// /// </summary> static void ChangeAssetBundleName(string folder_full_name , AssetBundleBuildData.AssetBuild.Element element , System.Action <string> change_callback = null) { if (cancel_running_nametool_) { return; } if (element == null) { return; } DirectoryInfo dir = new DirectoryInfo(folder_full_name); if (!dir.Exists) { return; } if ((emAssetBundleNameRule)element.Rule == emAssetBundleNameRule.Ignore) { return; } //遍历文件,并设置其AssetBundleName FileInfo[] all_files = dir.GetFiles(); foreach (var f in all_files) { AssetBundleBuildData.AssetBuild.Element child = element.FindFileElement(f.Name); emAssetBundleNameRule my_rule = child != null ? (emAssetBundleNameRule)child.Rule : emAssetBundleNameRule.None; if (!EditorCommon.IsIgnoreFile(f.Name) && my_rule != emAssetBundleNameRule.Ignore) { if (my_rule == emAssetBundleNameRule.SingleFile) { SetAssetBundleName(f.FullName); } else { ClearAssetBundleName(f.FullName); } } if (child != null) { if (change_callback != null) { change_callback(f.FullName); } } } //遍历文件夹 DirectoryInfo[] all_dirs = dir.GetDirectories(); foreach (DirectoryInfo d in all_dirs) { if (!EditorCommon.IsIgnoreFolder(d.Name)) { AssetBundleBuildData.AssetBuild.Element child = element.FindFolderElement(d.Name); emAssetBundleNameRule my_rule = child != null ? (emAssetBundleNameRule)child.Rule : emAssetBundleNameRule.None; if (my_rule == emAssetBundleNameRule.Folder) { SetAssetBundleName(d.FullName); } else { ClearAssetBundleName(d.FullName); } if (child != null) { if (change_callback != null) { change_callback(d.FullName); } } ChangeAssetBundleName(d.FullName, child, change_callback); } } //刷新 AssetDatabase.Refresh(); }