Sunday 17 September 2017

I made a Unity editor window because of a weird dream I had

Yep, that's right. I've been thinking about custom Unity editor windows so much that I had a dream about them.

I originally wanted to make editor windows to create some in-editor tools, and automate certain processes. I had been experimenting and playing, but I hadn't really made anything functional. Then I had the dream.

Making an editor window itself is pretty simple. I was able to follow the Unity docs to make one when I first started learning. If you follow that documentation, you will be able to create an editor window. It won't do much yet, though.

Unity editor window scripts derive from UnityEditor.EditorWindow as opposed to UnityEngine.MonoBehaviour, and I needed to figure out how to get clicks and events from the custom window to affect game objects and other UnityEngine objects. After a lot of searching, I found a tutorial on The Knights Of Unity, and I was able to use the examples they provided to help me.

Since I already knew how to create editor windows, I had gotten as far as:

Apologies for the weird Gist formatting.

This gave me an editor window with a toggle (defaulted to false/unchecked), but it didn't actually do anything.



Obviously, I needed a way to find a way to make this toggle's event actually trigger an action in the scene. For this, I needed to make use of EditorPrefs. Making use of what I had learnt from The Knights Of Unity's blog, I created the following script:


The script has a public bool (with some editor-only properties), as well as a public reference to a GameObject which gets turned on and off in Update(). This is great, but it means I need to change the window script from

mangoEnabled = EditorGUILayout.Toggle("Mango Toggle", mangoEnabled);

to

ClickToMango.mangoEnabled = EditorGUILayout.Toggle("Mango Toggle", ClickToMango.mangoEnabled);

which references the bool in the ClickToMango class/script.

My Click To Mango dream became a reality.


Could I have done this just using a regular script in the inspector? Yes, probably. Did I learn more about how custom editor windows by doing this? Yes, definitely.

You can look at this on GitHub if you want.

PS I got this mango from the Unity Asset Store <3