/// <summary> /// 遍历指定目录以及子目录,生成默认数据 /// </summary> static AssetBundleBuildData.AssetBuild.Element GenerateAssetBundleRuleData(string path , emAssetBundleNameRule rule , Action <string> progress_report) { try { AssetBundleBuildData.AssetBuild.Element result = null; DirectoryInfo dir_info = new DirectoryInfo(path); if (dir_info.Exists) { if (progress_report != null) { progress_report(path); } if (!EditorCommon.IsIgnoreFolder(path)) { //生成自身信息 result = new AssetBundleBuildData.AssetBuild.Element(dir_info.Name); result.Rule = (int)rule; result.IsFolder = true; var dics = dir_info.GetDirectories(); var files = dir_info.GetFiles(); //遍历所有子文件 foreach (FileInfo f in files) //查找文件 { string str = f.ToString(); var child = GenerateAssetBundleRuleData(str, emAssetBundleNameRule.None, progress_report); if (child != null) { result.Add(child); } } //遍历所有文件夹 foreach (DirectoryInfo d in dics) { string str = d.ToString(); var child = GenerateAssetBundleRuleData(str, emAssetBundleNameRule.None, progress_report); 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); }
static Dictionary <string, uint> s_file_dic_temp_; ///< 辅助操作缓存 /// <summary> /// 调整数据(匹配现有的文件&文件夹结构,删除无用的数据) /// </summary> static void MatchAssetRuleElement(string path, AssetBundleBuildData.AssetBuild.Element element , Action <string> progress_report) { try { DirectoryInfo dir_info = new DirectoryInfo(path); if (!dir_info.Exists) { return; } if (progress_report != null) { progress_report(path); } uint bit_0 = 0x1; // 存在数据中 uint bit_1 = 0x2; // 存在文件或文件夹 if (s_folder_dic_temp_ == null) { s_folder_dic_temp_ = new Dictionary <string, uint>(512); } else { s_folder_dic_temp_.Clear(); } if (s_file_dic_temp_ == null) { s_file_dic_temp_ = new Dictionary <string, uint>(512); } else { s_file_dic_temp_.Clear(); } if (element.Children != null && element.Children.Count > 0) { foreach (var elem in element.Children) { if (elem.IsFolder) { if (!s_folder_dic_temp_.ContainsKey(elem.Name)) { s_folder_dic_temp_.Add(elem.Name, bit_0); } } else { if (!s_file_dic_temp_.ContainsKey(elem.Name)) { s_file_dic_temp_.Add(elem.Name, bit_0); } } } } foreach (DirectoryInfo d in dir_info.GetDirectories()) { if (EditorCommon.IsIgnoreFolder(d.Name)) { continue; } if (!s_folder_dic_temp_.ContainsKey(d.Name)) { s_folder_dic_temp_.Add(d.Name, bit_1); } else { s_folder_dic_temp_.Remove(d.Name); } } foreach (FileInfo f in dir_info.GetFiles()) { if (EditorCommon.IsIgnoreFile(f.Name)) { continue; } if (!s_file_dic_temp_.ContainsKey(f.Name)) { s_file_dic_temp_.Add(f.Name, bit_1); } else { s_file_dic_temp_.Remove(f.Name); } } //删除不存在的数据 if (element.Children != null && element.Children.Count > 0) { element.Children.RemoveAll((elem) => { if (elem.IsFolder) { return(s_folder_dic_temp_.ContainsKey(elem.Name) && s_folder_dic_temp_[elem.Name] == bit_0); } else { return(s_file_dic_temp_.ContainsKey(elem.Name) && s_file_dic_temp_[elem.Name] == bit_0); } }); } //记录旧的子对象数量 int oldChildrenCount = element.Children != null ? element.Children.Count : 0; //增加文件夹数据 foreach (var pair in s_folder_dic_temp_) { if (pair.Value == bit_1) { string full_name = path + "/" + pair.Key; element.Add(GenerateAssetBundleRuleData(full_name , emAssetBundleNameRule.None , progress_report)); } } //增加文件数据 foreach (var pair in s_file_dic_temp_) { if (pair.Value == bit_1) { string full_name = path + "/" + pair.Key; element.Add(GenerateAssetBundleRuleData(full_name , emAssetBundleNameRule.None , progress_report)); } } //更新子文件夹数据 if (oldChildrenCount > 0) { for (int i = 0; i < oldChildrenCount; ++i) { string full_name = path + "/" + element.Children[i].Name; if (element.Children[i].IsFolder) { MatchAssetRuleElement(full_name, element.Children[i], progress_report); } } } //重新排序 element.SortChildren(); } catch (System.Exception ex) { Debug.LogWarning(ex.Message); } }
/// <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); } }