Different method for finding if we're in a prefab?

Before seeing how Sam checked if we were in prefab mode using “gameobject.scene.path”, I went digging in the Unity Docs and used:

if (PrefabStageUtility.GetPrefabStage(gameObject)) return;

From what i can tell this does the same thing. Will this work ok for the project? Is there anything i should be wary about?

There is one issue that you should be aware of. PrefabStageUtility is in the namespace UnityEditor.SceneManagement When you build a project to be a standalone player, Editor code is not allowed. The compiler will just ignore the

using UnityEditor;

but will not automatically remove any code that relies on Editor code, and will not automatically remove

using UnityEditor.SceneManagement;

You can get around this by surrounding any Editor code with

#if UNITY_EDITOR
using UnityEditor.SceneManagement;
#endif

and

#if UNITY_EDITOR
if (PrefabStageUtility.GetPrefabStage(gameObject)) return;
#endif

Now in this instance, I believe we’re in the middle of an if #UNITY_EDITOR block so you’ll just need to surround the using clause in an #if block.

Privacy & Terms