Hi, I have some MonoBehaviours that have Interfaces attached.
I want to write Editor code that gets all these interfaces and calls a Clear() method on them.
What the Clear() method does it, that it removes some references in serialized Inspector fields.
I already managed to call Clear, but this doesn’t save the changes (which can be inside Scenes or Prefabs).
How would you do it?
This is what I did so far:
string[] assetsPaths = AssetDatabase.GetAllAssetPaths();
foreach (string assetPath in assetsPaths)
{
Object[] data = LoadAllAssetsAtPath(assetPath);
foreach (Object o in data)
if (o is IMyInterface interface)
interface.Clear();
}
Object[] LoadAllAssetsAtPath(string assetPath)
{
bool objectIsScene = typeof(SceneAsset) == AssetDatabase.GetMainAssetTypeAtPath(assetPath);
return objectIsScene
?
// prevent error "Do not use readobjectthreaded on scene objects!"
new[] {AssetDatabase.LoadMainAssetAtPath(assetPath)}
: AssetDatabase.LoadAllAssetsAtPath(assetPath);
}