How to find the prefab root in the prefab editor with an environment?

I am try to use the GetNearestPrefabInstanceRoot and FindPrefabRoot and transform.root, both of them gets the environment root but not the prefab root.
Is there any way to get the prefab root?

Hi,

When opening a prefab in Prefab Mode we load the contents of the prefab. This means that for a normal prefab the GameObjects of the prefab itself are plain GameObjects while nested prefabs are Prefab instances. (For Prefab Variants the root will be a Prefab instance).

To get the root GameObject of the opened Prefab you have to use the PrefabStageUtility (in namespace UnityEditor.Experimental.SceneManagement):

PrefabStage prefabStage = PrefabStageUtility.GetPrefabStage(yourGameObject);

The prefabStage will be null if yourGameObject is not part of a PrefabStage.
If the prefabStage is valid you can do the following to get the root of the prefab:

GameObject root = prefabStage.prefabContentsRoot;

1 Like

There are totally 3 distinct type/state of prefab. And they must be handled differently.

  • Prefab Asset (The one that sits in Asset folder)
  • Prefab Instance (The one that dragged into scene to become blue game object, or nested inside another prefab)
  • Prefab Stage (Editing prefab intermediate in UnityEditor prefab edit mode)

For example: from getting assetPath alone, these 3 cases will need different API

  • Prefab Asset => use UnityEditor.AssetDatabase.GetAssetPath()
  • Prefab Instance => PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot()
  • Prefab Stage => PrefabStageUtility.GetCurrentPrefabStage then editorPrefabStage.prefabAssetPath

Finding root of game object for each case also need different API

  • Prefab Asset => use gameObject.transform.root.gameObject()
  • Prefab Instance => PrefabUtility.GetNearestPrefabInstanceRoot()
  • Prefab Stage => PrefabStageUtility.GetCurrentPrefabStage then editorPrefabStage.prefabContentsRoot

(See deprecate message in old PrefabUtility.FindPrefabRoot function, it is very useful.)

Im not sure which case you are on.
But for PrefabUitility API example, you could look into my demo on GitHub - wappenull/UnityPrefabTester: Ultimate prefab property tester for nested prefab era.
It is editor script demo.

Edit: Add finding root APIs

7 Likes