Unity. Get the current IMGUI drawing target. Screen or EditorWindow/Popup

83 views Asked by At

I'm developing a class that can draw the interface both in the game and in the editor. Now I manually specify which mode to work. Because some functions are not available in different modes

class MyWindow : MonoBehaviour {
  void OnGUI() {
    myClass.OnGUI(GUIMode.Screen);
  }
}
 
class MyEditorWindow : EditorWindow {
  void OnGUI() {
    myClass.OnGUI(GUIMode.Inspector);
  }
}

I would like to learn how to detect this automatically. Is it possible?

What have already I tried: Google. Read documentation. Read Unity GUI-classes source code

1

There are 1 answers

1
hijinxbassist On

There are scripting symbols you can use to conditionally compile things.
They can also be used to determine specific configurations of the environment, and set variables.
eg.

private bool isEditor;
...
#if UNITY_EDITOR

isEditor = true;

#endif

For specifically checking if you are in the editor, you can use the flag isEditor from the Application class.

if (Application.isEditor)
{
    Debug.Log("In Editor");
}
else
{
    Debug.Log("In Build");
}

There are more flags in the Application class to determine common environment context, such as isMobilePlatform, isConsolePlatform, platform, etc.