top of page

006-Adding Buttons to custom Editors

1.1 Introduction

1. In this tutorial we will learn to add buttons to custom editors in Unity.

This will allow us to extend the functionality of monobehaviours by adding additional buttons. We are going to be adding 3 buttons in this tutorial to our Test006 class.

2. Open the 006 folder available on patreon.

1.2 Basic Script

1. Let us open the Test006 script in the unity editor and analyze it in the script as well as in the inspector.

image.png
image.png
1.2 Adding a custom inspector

1. Let us make a script called as TestEditor006.cs that we will use as custom editor for Test006.

2. To use this as custom editor:

a. Make it inherit from Editor class in UnityEditor namespace.

b. Use a CustomEditor attribute with the type for which this is a custom editor

c. Override the default GUI function to write our own functionality.

base.OnInspectorGUI() tells the unity to draw the default test006 editor.

image.png
1.3 Adding buttons

1. To add a button use GUILayout.Button("ButtonName") to add a a button

2. Let us add three buttons in the custom editor

image.png
image.png

We have added the button in the custom editor but currently they do not perform any function.

1.3 Adding Functionality to buttons

1. Whenever the button is clicked the statement of GUILayout.Button("") is going to return true.

2. Let us add an if statement and any code within that will be executed whenever the button is clicked.

image.png
image.png
image.png
End

Kindly support me through my Patreon account if you find these tutorials useful. Feel free to connect with me on Twitter. This support enables me to create more educational content.

bottom of page