Hello,
I’m trying to use PrefabUtility.GetCorrespondingObjectFromOriginalSource() to find the originating prefab asset for a prefab instance in my project, but I’m getting a whole bunch of different results when I start to use prefab variants.
I created a test project with 3 scenarios - Simple nesting of prefabs, nesting of prefabs with prefab variants, and complex nesting of prefabs with variants and variants of variants. Then I use PrefabUtility.GetCorrespondingObjectFromOriginalSource(), and PrefabUtility.GetCorrespondingObjectFromSource() using my own iteration to cycle through all nesting of the prefab. The results of the 3 tests are very bizarre.
Test 1 - Everything works as intended. As long as there are no variants, I can successfully find the originating prefab asset.
Test 2 - The moment variants enter the picture, things start to fall apart. If I try to find the prefab asset, the variant prefab instance will also return the non-variant prefab asset. That makes sense, but it’s unfortunate it’s done that way since there’s no way to strip out the variant results. The weird part though, is that the variant prefab instance is not recognized as a variant (i.e. PrefabUtility.GetPrefabAssetType returns PrefabAssetType.Regular) if the variant is a child of another prefab.
Test 3 - All hell breaks loose. If you search for a prefab asset variant that is a child of another variant, no results are returned. The only way I can get it to work is if I use my own iteration system as outlined above. But even then, PrefabUtility.GetPrefabAssetType still doesn’t return the correct PrefabAssetType for any of the prefab assets I find.
This is all difficult to explain because it’s variants nested in variants of variants, so I attached a project file using the latest stable Unity (2019.1.8f1). It’s simple to test. Just go to Window > Find Prefab Reference In Scene, then drag in “Test3_ChildPrefab2 Variant Variant” and press the GetCorrespondingObjectFromOriginalSource button to see nothing work.
Project Link: https://cellardoorgames.box.com/s/gqff20dnrlsmnameqq9hrty57excw2i4
I guess I have a few questions from this. Has anyone else experienced problems with this method? It looks like both GetCorrespondingObjectFromOriginalSource() and GetPrefabAssetType() aren’t behaving as expected when nested variants are involved. Am I perhaps using those methods incorrectly? And lastly, is there a better, more reliable way to find the originating prefab asset of a prefab instance?
Many thanks for any help with this,
Kenny