top of page

010-Accessing custom editor variables using target

1.1 Introduction

1. In this lesson we will learn about how to modify variables using custom editor by using target.

2. This is not the preferred method as recommended by unity but let us take a look into it for sake of understanding.

3.  Using Custom editor we will print the variables,assign the value and perform some operations

1.2 Basic Script

1. Let us open the Test010 script in 010 folder and take a look at its contents

image.png
1.3 Making custom Editor

1. First let us make a custom editor for Test010 and call it as Test010Editor.

2. Make sure that this inherits from Editor and has CustomEditor attribute on top of it pointing to Test009 script.

3. Let us override the OnInspectorGUI() method and also add base.OnInspectorGUI() to draw the default inspector.

image.png
1.3 Adding button to print variables

1. Let us add a button called as PrintVariables that is going to be printing the variables of Test010 script.

2. Let us also get reference to Test010 class to be able to get its members.

​

image.png
image.png

1. As we have reference to Test010 class we can only get reference to public members and not private members(we will learn how to access even the private members in the future tutorials).

2. So let us print few of its public members.

image.png
image.png
image.png

As we can see from above we are able to access the variables and print their values.

1.4 Assigning values to variables

1. Let us now make a buttton to assign values to the Test010 script.

2. Let us make a button called as "Assign Variables" and when this button is clicked it will change the variables of the script.

image.png
image.png

​

Before pressing "Assign variables" button

 

image.png

​

After pressing "Assign variables" button

 

1.5 Adding functionality

1. Let us now add a button that allows us to populate children list of Test010 script

2. Call this button as "Get Children" and create a method callas GetChildren that returns the list of all the children for the current transform.

image.png
image.png

Let us now test this script.

image.png
image.png

Hierarchy

After pressing the button it assigns all the children gameObjects.

1.5 Multi Object editing.

1. Currenlty our custom editor doesn't support multi object editing and this is evident from the fact that when two objects with Test010 are selected nothing appears in the inspector.

image.png
image.png

2. To support multi object editing let us add [CanEditMultipleObjects] tag on the top of Test010Editor script.

image.png
image.png
image.png

3. Now we can perform multi object editing. But still button will be run for only one of the object. We will fix that in the future while covering scriptable objects.

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