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

Face Detection Using Python and OpenCV

I am currently working on face detection implementation using Python and OpenCV. Here is the result i collected using a placeholder image. Before After It detects faces pretty well. I am so excited to start working on real time face detection while capturing video. My goal is to work on face recognition and then will start working on detecting multiple types of objects.

Create Unity Custom Editor - Basics (Part 1)

In this basic lessons we will be going to create our own custom inspectors inside unity. Before we dive in lets get to know what custom inspectors are A custom inspector is basically an overridden inspector window which can help artist and designers to interact with the inspector in a more user friendly manner. It also helps to create a neat looking inspector for your scripts attached to gameobjects. So lets get started with the initial project setup    Steps to follow: 1) Start a fresh project 2) Create a folder name Scripts This will hold all the scripts(typically monobehaviour) which will be attached to a gameobject in the scene. 3) Create a folder name Editor ( Note : it should be the exact same name ) This will hold all the Editor scripts and Unity will automatically detect weather or not the project contains any editor scripts for component scripts. With this done we have the basic fundamental structu...

Eyes detection using Python and OpenCV

In my last post i've worked on detecting the face. In progress to that, i am able to detect eyes as well. Its pretty simple as of now. Here is the result Before After So as you can see it starts detecting eyes as well. I am making a little progress now but, still a lot to learn.  I am super excited to work towards my goal. * The image has been taken from google images and is used only for testing purpose.