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

GetPrefabAssetType returns incorrect value

Discussion in 'Prefabs' started by ZackCee, Dec 20, 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;

    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 : https://docs.unity3d.com/ScriptRefe...47.1315843227.1608449610-549580194.1603079518

    Hope someone can enlighten on this. Thanks!
     
  2. ZackCee

    ZackCee

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

    and the results :
    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!
     
  3. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    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

    Code (CSharp):
    1. GameObject assetRoot = currPar.myParSys as GameObject;
    2. Debug.Log(currPar.myParSys.name + " is " + PrefabUtility.GetPrefabAssetType(assetRoot));
    3.