编辑器通用资源校验工具

编辑器通用资源校验工具 问题情景从根配置出发读取若干个配表对每个配表的每个资源路径检查资源或者预制体身上必要的脚本存在。从总配表读取每个配表路径要反序列化成的类不一样要参数化需要传入字段名类型字典。可以定义一个配置基类或接口里面有校验资源方法。然后发现Assembly CSharp不能访问Assemble CSharp Editor。只有AB包资源校验能这么搞。配置基类/接口那么这样吧配置类继承配置基类必须实现一个返回一个string,Type字典包含所有要检查的路径和类型。这里为了对任意配置类检查id不为空需要往接口里放字段只好改成抽象类。这样避免了编辑器工具里为七八十来个配置类专门写校验方法变成分散在各配置类提交须校验路径和类型的方法以及一个编辑器里的通用方法。配置类里的提交方法代码较少省去重复写七八个有循环的函数。public abstract class ConfigBase { public string id; public abstract void CheckResAB(); /// summary /// 返回此配置类要检查的资源路径和类型。预制体则返回它身上 /// 必须的脚本 /// /summary /// returns/returns public abstract Dictionarystring, Type CheckDic(); }public class PlantConfig:ConfigBase { //public string id; public string name; public int growthThreshold 2; public int growthDelta 1; public int wateredGrowthDelta 2; public string prefabPath; public string fruitID; /// summary /// 采摘一次就消失 /// /summary public bool once; public override Dictionarystring, Type CheckDic() { Dictionarystring, Type dic new(); dic.Add(prefabPath, typeof(PlantBehavior)); return dic; } public override void CheckResAB() { throw new NotImplementedException(); } }这个方法用于校验在某配置类中所有需要校验的资源的路径是否有指定的类型可以是资源类型GameObject、Sprite也可以是身上的Mono脚本void CheckConfigsT(Dictionarystring, T itemDic, string ABName)where T:ConfigBase { foreach (var pair in itemDic) { if (string.IsNullOrEmpty(pair.Value.id)) { Debug.LogError(id为空); } var dic pair.Value.CheckDic(); foreach (var pair2 in dic) { CheckRes(pair2.Key, pair2.Value, ABName); } } } static UnityEngine.Object CheckRes(string path,Type type,string ABName) { UnityEngine.Object t AssetDatabase.LoadAssetAtPath(path,type); if (t null) { Debug.LogError($在{path}找不到资源); } else { Print(${path}存在。, Color.cyan); } var importer AssetImporter.GetAtPath(path); if (!string.IsNullOrEmpty(ABName)) { if (importer.assetBundleName ! ABName) { importer.assetBundleName ABName; Print($设置了{path}的AB包名为{ABName}, Color.cyan); } } return t; }