- Home /
Accessing Editor Window from Game Code
I have a custom editor window which I made in unity 3.5. I wanna know if there's any way I could open up that window from the Game Code when the Game is Playing. I have read through the blogs but I cudn't find anything that allows me to access my editor script from the game code.
Answer by Bunny83 · May 01, 2012 at 04:30 AM
In general it isn't possible since the UnityEditor namespace isn't available at runtime. If you use anything from there you can't build your game. However you can use platform dependent compilation to open your editor window. Of course this will only work when you play your game inside the editor. When you build your game the code between #if
and #endif
will be automatically removed.
//C#
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
#if UNITY_EDITOR
UnityEditor.EditorWindow.GetWindow<YourEditorWindow>();
#endif
}
}
Yes I tried had this approach but Unity would still throw "The type or namespace name '$$anonymous$$yEditor' could not be found". I also tried keeping both the editor script and the monobehaviour script in the same namespace but that didn't work also adding a namespace wouldn't let me open my editor in the editor mode itself. Am I supposed to keep this script in a "Editor" folder ?? Wat am I missing ?
You could also put a delegate (callback) in your game code, which editor code can then set the delegate to call another editor method.
Thanks for this idea, great inversion.
public class ScreenshotUtility : EditorWindow
{
private void OnEnable(){
GameCam$$anonymous$$anager.CaptureScreenDelegate += RenderScreenshot;
}
}
...
class GameCam$$anonymous$$anager extends $$anonymous$$onoBehavior {
public static Action CaptureScreenDelegate;
public void CaptureScreen() => CaptureScreenshotDelegate?.Invoke();
}
No need for compile flags, and fails gracefully in release build.