Objective

Main objective of this blog post is to give you an idea about to how to Create Custom Inspector

 

 

Step 1 Inspector Overview

When you choose any GameObject in the Hierarchy or Scene View, the Inspector will demonstrate the Properties of all the Components and Materials and then permits you to alter them. For a custom Script segment, general society variables of that script are likewise demonstrated in the inspector and can be seen and altered like the properties of Unity's implicit parts. This permits you to set parameters and default values in your scripts effectively without changing the code.

Here is a screenshot of inspector. 

screenshot-of-inspector

Moreover, the Inspector is utilized to alter import choices for Assets (records containing surfaces, 3D models, text styles, and so on). Some scenes and undertaking wide settings are likewise seen in the inspector, the Settings Managers being the principle illustration.

 

Step 2 Custom Inspector

Custom inspectors permit you to include a ton of power and adaptability to your work process. In this feature you will find out about the profits of custom inspectors and additionally know how to construct them. A key to expanding the pace of diversion creation is to make the custom editors for normally utilized parts.

In unity we can create custom inspector to control which data is visible and how it should be represented to the editor.

 

Step 3 How to Create Custom Inspector

Follow the steps below to create custom inspector:

 

3.1 Create Script

Create a simple script. Here, the script is named “ScriptDemo.cs” in which you can write your own logic and functionality.

using UnityEngine;
using System.Collections;
 
public class ScriptDemo : MonoBehaviour
{
        //Write Your own code below code is just for demo
        public int experience;
        
        public int Level {
                get{ return experience / 750; }
        }
}
 

3.2 Assign Script

Assign script to objects in hierarchy.
Select the object and drag and drop the script to inspector or you can add component from inspector or you can use the components menu in unity.

I have used drag and drop to assign the script to inspector or on object in hierarchy. 

assign-script-to-object-in-hierarchy

 

3.3 Create Folder

Create a folder named “Editor”, which contains unity editor scripts. They affect the editor in real time. 

create-folder-named-editor

 

3.4 Create Editor Script

Create the editor script “DemoEditorScript.cs” as mentioned in step 3.

using UnityEngine;
using System.Collections;
using UnityEditor;
 
[CustomEditor(typeof(ScriptDemo))]
public class DemoEditorScript : Editor
{
        public override void OnInspectorGUI ()
        {
                ScriptDemo myTarget = (ScriptDemo)target;
        
                myTarget.GamePause = EditorGUILayout.Toggle ("Game Pause", myTarget.GamePause);
                EditorGUILayout.IntField ("Level", myTarget.Level);
//Add Your code
        }
}

The script will execute automatically so there isn’t any need to map it onto any object.

But see to it that the script is in “Editor” folder only.
Editor script must extend the “Editor” class from “UnityEditor” namespaces

Override OnInspectorGUI() method of “Editor” class.

You can see in the image below that the script changed to component. 

script-changed-to-component

I hope you find this project very helpful while Creating Custom Inspector in Unity. Let me know in comment if you have any questions regarding Unity. I will reply you ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.

I am Game Developer at TheAppGuruz. I love all things about game development and try to help others to create games through this website. I tend to focus more on creating games and playing them, it's still just as fun.

face mask Belial The Demon Headgear Pocket Staff Magic