Pages

Tuesday, 14 July 2015

Create Unity Custom Editor - Basics (Part 8) -More GUI elements

In this section we'll create some more interactive gui elements inside our custom editor script
We'll use GUILayout.Button(); to create a button
Notice: This has a bunch of override's that you can look into based on your needs.
For additional reference click here

Now inside our editor script we'll add
GUILayout.Button("Click Me!", GUILayout.Height(40)); 
to create a button with a height of 40 pixels.
So our result will look something like this. Right now the button just sits there and does nothing when we click on it. Let's work on that functionality.
There's nothing much to do that, you just have to put an if statement as GUILayout.Button() returns a boolean value based on if its clicked or not.


if(GUILayout.Button("Click Me!", GUILayout.Height(40)))
{
      Debug.Log("Button Clicked");
}
This will now actually fire once we tap on the button inside the inspector window.

We can now call a method in our test script from our editor script whenever the button is pressed.
So let's create a method inside our test script and make sure it has public access modifier.
Now inside the editor script we can call this method by using the targetScript variable which stores a reference to the TestScript. Our whole editor code looks like this now
Look at the final output and try clicking on the buttons and see the result. It will actually move the gameobject by 1 unit to right or left.


No comments:

Post a Comment