GetPrefabAssetType returns incorrect value

Hi,

I’m currently trying to create a tool to speed up workflow. The function of this tool is to add component to a list of selected prefabs, but to prevent double adding on regular/variants, I want to check if the prefab I might have accidentally selected is already a variant.

Here come’s the problem, I am unable to get a return value of variant even though the asset I selected is a prefab variant, and I know it is because the icon of the prefab is with the black lines. Strangely if I create a variant of the variant, the engine returns me a value of variant instead of regular.

Recreating the scenario :
Create empty gameobject in scene;
Set gameobject as prefab;
Right click (prefab)gameobject and create prefab variant (gameobject variant);
Test code returns the (gameobject variant) as a regular prefab;
Right click (prefab)gameobject variant and create prefab variant (gameobject variant variant)
Test code returns the (gameobject variant variant) as variant prefab;

I’m using Unity 2019.3.9 if that should affect anything

By the way, most parts of the test code that I’m currently using is from the prefab utility page : Unity - Scripting API: PrefabUtility.LoadPrefabContents

Hope someone can enlighten on this. Thanks!

Here is the snippet of my code :

public class ParticleCheckResult
    {
        public GameObject myParSys;
        public string particleAddedResult;
    }


    public List<ParticleCheckResult> parSysForChecks = new List<ParticleCheckResult>();

public void AddManaged()
    {
        foreach (var currPar in parSysForChecks)
        {
            GameObject assetRoot = currPar.myParSys as GameObject;
            string assetPath = AssetDatabase.GetAssetPath(assetRoot);
            GameObject contentsRoot = PrefabUtility.LoadPrefabContents(assetPath);

            Debug.Log(currPar.myParSys.name + " is " + PrefabUtility.GetPrefabAssetType(contentsRoot));
            continue;
}
}

the GameObjects I placed in the list :
6655636--760675--Capture012.PNG

and the results :
6655636--760672--Capture011.PNG

as you can see, the base prefab is not even treated as a prefab, and the first level variant is treated as regular. Can someone enlighten me on whether this is intended behaviour, or should I use another method to check the prefab type? Thanks!

When you are using PrefabUtility.LoadPrefabContents the same rules as Prefab mode in the editor applies. Meaning that a regular Prefab becomes regular GameObjects.

This should work better

GameObject assetRoot = currPar.myParSys as GameObject;
Debug.Log(currPar.myParSys.name + " is " + PrefabUtility.GetPrefabAssetType(assetRoot));