Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved GetPrefabAssetType returns incorrect value

Discussion in 'Scripting' started by ZackCee, Dec 28, 2020.

  1. ZackCee

    ZackCee

    Joined:
    Oct 21, 2020
    Posts:
    10
    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;


    Here is the snippet of my code :
    Code (CSharp):
    1.  
    2.     public class ParticleCheckResult
    3.     {
    4.         public GameObject myParSys;
    5.         public string particleAddedResult;
    6.     }
    7.     public List<ParticleCheckResult> parSysForChecks = new List<ParticleCheckResult>();
    8.     public void AddManaged()
    9.     {
    10.         foreach (var currPar in parSysForChecks)
    11.         {
    12.             GameObject assetRoot = currPar.myParSys as GameObject;
    13.             string assetPath = AssetDatabase.GetAssetPath(assetRoot);
    14.             GameObject contentsRoot = PrefabUtility.LoadPrefabContents(assetPath);
    15.             Debug.Log(currPar.myParSys.name + " is " + PrefabUtility.GetPrefabAssetType(contentsRoot));
    16.             continue;
    17.     }
    18.  
    the GameObjects I placed in the list :
    Capture011.PNG

    and the results :
    Capture012.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? I'm using Unity 2019.3.9 if that should affect anything. Thanks!
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    You want to use
    PrefabUtility.GetPrefabAssetType(assetRoot)
    , using the prefab asset (
    assetRoot
    ) instead of its contents (
    contentsRoot
    ).

    What you're getting the type of is the root object inside the prefab. With this, your results make sense: A regular prefab contains a non-prefab game object, a variant contains an instance of the original prefab and a variant of a variant contains an instance of the original variant.
     
    ZackCee likes this.
  3. ZackCee

    ZackCee

    Joined:
    Oct 21, 2020
    Posts:
    10
    Thanks a bunch man, never thought I had to use the prefab as a GameObject rather than an asset.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Thanks for this important clarification @Adrian.

    As a side note while reading the above my head exploded.

    But I understand it better now, so I got that going for me.