[Editor] Find component for prefab from PrefabUtility
Hey folks, I have the following problem.
I'm writing an editor to auto-fix missing sprite references in SpriteRenderer / Image components which should be replaced by an atlased Sprite supplied to the editor window.
There is one problem when applying the fix, however. I have a scene with a prefab inside, this prefab has one nested prefab which contains a child with an Image on it. Since I shouldn't just apply the prefab active in the scene I load the nearest prefab asset which contains this Image.
What I get back is the prefab's root, so I have to search for the Image again which isn't possible due to changed instance IDs... Is there a way for Unity to return the component I gave it to lookup the Prefab in the first place?
string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(component);
GameObject loadedPrefab = PrefabUtility.LoadPrefabContents(prefabPath);
List<Image> images = new List<Image>(loadedPrefab.GetComponentsInChildren<Image>());
Object loadedComponent = images.Find(x => ????????????????);
if (loadedComponent != null)
{
FixSprite(loadedComponent);
PrefabUtility.SaveAsPrefabAsset(loadedPrefab , prefabPath );
}
PrefabUtility.UnloadPrefabContents(loadedObject);
Your answer
Follow this Question
Related Questions
Drop down menu for public variables on a component in the inspector. 0 Answers
Spawn random letters 0 Answers
Creating Modular Firing Mechanism 1 Answer
Set Component property using a var 0 Answers
How to re-enable multiple components in one script 0 Answers