Unity Tips: Wrapping Editor/GUI Layout

Martin Gonzalez
3 min readJan 21, 2019

The majority of developers who have worked with Unity, at some stage have been faced with the task of developing a tool for the editor.

Unity provides us with some classes, such as EditorGUILayout or GUILayout to create properties and display them in the editor, such as a label, a toggle or an image.

Example of a simple Editor Window

But even if what Unity provides works, at some point the code becomes confusing because of how we should develop with these classes. Let’s see the code of the previous image.

If you have already worked making tools for the editor, you will know the code above, but for those who do not know it, basically everything that is between BeginHorizontal and EndHorizontal will be distributed horizontally. Simple right? But what happens when the code scales? Let’s add a little more code to the previous one.

Well, we only add the Bool Value and String Value fields and a label, let’s see what the code looks like.

What?? We have added 3 elements and the code now is confusing. So What should we do when this happens? Clean Code. I’m going to make a small refactor here separating things in methods.

Well, this is better, but we still do not get rid of those methods BeginHorizontal and BeginVertical that at a certain moment can bring us reading problem. For this we are going to wrap the GUILayout class in a custom class to see how it can improve our code.

We create a static class called Layout where we will create methods similar to those of GUILayout.

This static method receives as a parameter a block that will be executed between BeginHorizontal and EndHorizontal making our code more readable. Let’s see now how is our class using Layout.

I have used a lambda, although I could also have used a defined method, to indicate which elements are going to run between BeginHorizontal and EndHorizontal. Not only our code is more readable, but we can create layouts by default, here I leave a project you can use and see some examples created and if you want you can create more and do pull request.

Unity Editor Layout Wrapper

Code examples:

Layout:

Summary

Creating Unity editor tools is very fun, but more fun is if we make our code readable, flexible and easy to understand, believe me you will not like to see your own editor code in 2 months.

Creating your own layouts will help you to standardize your layouts and also save you a lot of time.

If you have any advice or question you can write them here if you want. and I will try to help you! :)

--

--

Martin Gonzalez

Software Developer working at Tactile Games Denmark, Unity developer for 10 years and I can not sit still. I love helping and learning from others.