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
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 :
and the results :
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));