Skip to main content

Create Unity Custom Editor - Basics (Part 3)

In the previous lesson we covered the core structure of our project and now we are ready to create our own custom inspector.

In this lesson we will link our custom inspector script with our monobehaviour script.

So lets get started

1) Create a new GameObject inside the hierarchy and attach the TestScript that we created in the previous lesson.
















2) Now open the TestScript and write the following code inside it



3) Now go back to Unity and see the inspector window and you will notice two GUI elements that gets populated(Since the variables in our script our marked as public so those variables gets exposed in the inspector window in the form of GUI elements)












Now lets link our editor script with our current test script.

4) Open the TestScriptEditor and write the following code inside it


Notice a few things we added
  • A new using statement called UnityEditor as this is required to create a custom editor script.(This will allow us to get access to all the core classes that unity has provided us to build custom editors and custom windows)
  • A custom editor attribute on top of the class name to tell that it is going to be our custom editor of the type TestScript.(This will link the current editor script with our TestScript)
  • Instead of extending from Monobehaviour now we extend the class from Editor as it contains core methods which will help us in overriding stuff onto inspector.
  • We create a variable of type TestScript and assign to it the instance of the editor script which is stored inside the target variable which is of type object so we need to cast it to the proper type.
  • We implemented a method called OnInspectorGUI() which basically overrides the default inspector created by unity.
  • Writing the code DrawDefaultInspector() inside the OnInspectorGUI() method shows us the default inspector again that Unity has created.
5) Now go back to unity and we see nothing has changed but now we have linked our custom editor script with our test script and we can now have a better control over the inspector.

6) To test it. Remove the line DrawDefaultInspector() from the code and then check the inspector again you will see that both the public fields which were exposed are no longer visible.

So this ends with the part 3 of this lesson.

In the next lesson we will actually start adding some cool stuff to our custom editor we just created.

So lets head towards the next lesson

Comments

Popular posts from this blog

Detect If Your Device Is Connected To Wifi using Python

Have you ever wanted to see the list of devices connected to your local home network? Linux provides a cool solution to list all the devices connected to your network using arp scanning. You need to follow these simple steps:(These are for Linux) 1) Make sure you have arp-scan package installed (if not use  sudo apt-get install arp-scan  command in terminal to install the package) 2) In order to verify type  sudo arp-scan --interface=wlan0 --localnet   in terminal. You will probably see the list of devices connected to your local network using wifi. This sets our base and now we can move on to write our python script to detect particular devices and see  there status if its connected to wifi or not. NOTE: We need to know the mac address of the devices we want to track. Now Let's create a new python script and name it anything you like and paste this code  I named mine detectDeviceOnWifi.py Now run your script using  sudo python detectDeviceOnWifi.py  in

Create Unity Custom Editor - Basics (Part 6)

In this lesson we will add some GUI content to our custom inspector which will help us to add either Image Icon Text Tooltip to our GUI elements. So lets start by adding a sample icon to our GUI elements. I'll be using this image as a sample You can use any image that you would like to based on your needs. We need to import this image inside Unity. Create a folder named Resources inside the Editor Folder and then import the image inside this folder like this Now select the image and set the texture property to GUI as we will be using this image for our GUI elements. Now in our Code add the following lines We create a Texture2D object to hold our GUI texture that we just imported. We create a GUIContent object that will help us to add text,icon and tooltip field to our GUI elements. In OnInspectorGUI() method we will load our texture from our resources folder. Next we add the image,text and tooltip to our GUIContent object