How to detect if "Prefab Mode" is being used in a custom inspector?

Previously I wanted to detect if you had the prefab selected in Project View, which I guess is impossible after nested prefabs and Prefab Mode were introduced in Unity 2018.2. Unless someone has figured it out since?

However, is there a way to detect if we’re editing the prefab (the user clicked “Open Prefab” to edit the prefab itself)? Because it shows a continuous hourglass there when that’s selected for some reason and I could show a warning that you’re not allowed to edit the prefab (it’s for one of my plugins) and exit. Which would solve the issue, if it’s detectable at all from code.

Thanks!
-Brian

2 Likes

To check if a GameObject is part of a preview scene, you can use:
Unity - Scripting API: SceneManagement.EditorSceneManager.IsPreviewScene with gameObject.scene
(Prefab Mode loads Prefab contents into preview scenes.)

To check if a GameObject is a Prefab Asset (not Prefab Mode content) you can check the GameObject with

10 Likes

Wow, thanks!