EditorWindow and play mode

If you’re trying to write custom editors in Unity you may run into a snag. I just spent several hours hitting my head against a wall trying to solve this issue.

When you enter play mode, Unity will reload all mono assemblies. This means that your EditorWindow object is recreated, and you will need to explicitly handle serialising and deserialising any settings that you want to persist between edit mode and play mode. This is detailed on the Unity blog but unfortunately is not really covered in the documentation.

This is all well and good but Texture2D instances don’t seem to be serialised, even if you use the Serializable attribute. In my case, I am using small 1×1 instances of Texture2D when drawing a horizontal group of controls in order to draw highlighted selections. These textures are initialised in my EditorWindow’s constructor every time. However, when entering play mode, although the constructor and OnEnable are called, at the next OnGUI call the textures are again null!

I am not sure if this is by design, but it certainly isn’t documented except in various forum posts. My workaround for this is to re-create the textures in OnGUI if they are null. It’s ugly but it works!

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *