- Home /
Accessing EditorWindow variables from game code (only on editor play) is not possible?
I am writing a plugin. The folder structure is as follows :
Assets
--- MyEditorPlugin
------Editor
----------MyEditorWindow.cs
------Scripts
----------MyScript.cs
In MyEditorWindow.cs
I have MyEditorWindow : EditorWindow
class declared with several public static
variables.
I wanted to access these variables from MyScript.cs
code with for example
EditorWindow.GetWindow().staticVariable
But the editor says it cannot find MyEditorWindow
class.
I know that in the build, MyEditorWindow
will not be available, then how can I specify that some part of the code is editor only? Surrounding it with #if UNITY_EDITOR
does not work either. Same error thrown.
Answer by Baste · Mar 27, 2015 at 09:05 AM
All the code you put into Editor folders gets compiled into their own assembly, named something like "Assembly-CSharp-Editor". This assembly has access to your normal scripts, but those scripts does not have access to your Editor scripts, as you have noticed.
This is to allow Unity to safely ditch all of your Editor scripts when you build, and not include anything in the UnityEditor namespace in builds. It reduces file size and complexity. And, as a general rule, having your runtime code be dependent on code that should only be used editor time is a bad thing.
Now, if this is absolutely neccessary for you, you'll have to move MyEditorWindow out of the Editor folder, and into the Scripts folder. Then, you surround the entire script with #if UNITY_EDITOR - otherwise your project won't build, as it can't find the UnityEditor namespace when it's compiling for build, as I stated above.
Note that you'll also have to surround every call to the editor script - like the above EditorWindow.GetWindow().staticVariable - with #if UNITY_EDITOR, as the code won't be found when compiling.
Hope that helps!