public static void Build() { PrefabPathAsset configAsset; List <PrefabPathConfig> configs; if (File.Exists(PrefabListPath)) { configAsset = AssetDatabase.LoadAssetAtPath <PrefabPathAsset>(PrefabListPath); configs = new List <PrefabPathConfig>(configAsset.Configs); } else { configAsset = new PrefabPathAsset(); configs = new List <PrefabPathConfig>(); } DirectoryInfo root = new DirectoryInfo(Application.dataPath + "/Art/SceneRes"); FileInfo[] fileInfos = root.GetFiles("*.prefab", SearchOption.AllDirectories); int index = configs.Count == 0 ? 1 : configs[configs.Count - 1].ID + 1; for (int i = configs.Count - 1; i >= 0; i--) { string path = configs[i].Path; if (!File.Exists(path)) { configs.Remove(configs[i]); } } foreach (var item in fileInfos) { string path = item.FullName.Replace("\\", "/"); path = path.Replace(Application.dataPath, "Assets"); if (configs.Find(p => p.Path == path) == null) { PrefabPathConfig config = new PrefabPathConfig(); config.ID = index++; config.Path = path; configs.Add(config); } } configAsset.Configs = configs.ToArray(); if (File.Exists(PrefabListPath)) { EditorUtility.SetDirty(configAsset); } else { AssetDatabase.CreateAsset(configAsset, PrefabListPath); } }
private static int SearchPrefabIndex(string path) { if (m_PrefabList == null) { m_PrefabList = new List <PrefabPathConfig>(AssetDatabase.LoadAssetAtPath <PrefabPathAsset>(PrefabListPath).Configs); } PrefabPathConfig condig = m_PrefabList.Find(p => p.Path == path); if (condig != null) { return(condig.ID); } else { Debug.LogError($"资源列表中没有{path}"); return(-1); } }