I had to change the data format of one of the components we use frequently. I thought I could just quickly write a script to search through the entire project, find any prefabs containing the component, and then run some code to convert the data. But I'm not actually sure where to start -- how do I iterate through prefabs in editor code? I'm familiar with finding active game objects (i.e. in the scene/hierarchy) but not those in the project. I must be tired, but I just can't find out where to start (I looked in EditorApplication, EditorUtility, searched here, etc).
Could not find a satisfying solution, so I made my own method:
/// <summary>
/// Find all prefabs containing a specific component (T)
/// </summary>
/// <typeparam name="T">The type of component</typeparam>
public static List<GameObject> LoadPrefabsContaining<T>(string path) where T : UnityEngine.Component
{
List<GameObject> result = new List<GameObject>();
var allFiles = Resources.LoadAll<UnityEngine.Object>(path);
foreach (var obj in allFiles)
{
if (obj is GameObject)
{
GameObject go = obj as GameObject;
if (go.GetComponent<T>() != null)
{
result.Add(go);
}
}
}
return result;
}
Hi,
AssetDatabase.LoadAllAssetsAtPath(<string>)
I think this is what you need given your explanation. You will end up with a list of all objects in that path. Look at the rest of AssetDatabase functions, there is a lot related to that.
Then you could use
EditorUtility.GetPrefabType(<object>)
As you iterate through the return of AssetDatabase.LoadAllAssetsAtPath or whatever in the scene, if GetPrefabType() returns PrefabType.Prefab as a result, then you have a prefab.
Hope it helps,
Bye,
Jean