Building a Custom Inspector

Checked with version: 4.3

-

Difficulty: Intermediate

Custom inspectors allow you to add a lot of power and flexibility to your workflow. In this video you will learn about the benefits of custom inspectors as well as how to build them.

Building a Custom Inspector

Intermediate Interface & Essentials

LevelScript

Code snippet

using UnityEngine;
using System.Collections;

public class LevelScript : MonoBehaviour 
{
    public int experience;
    
    public int Level
    {
        get { return experience / 750; }
    }
}
#pragma strict

var experience : int;
    
function Level() : int
{
    return experience / 750; 
}
import UnityEngine
import System.Collections


public class LevelScript(MonoBehaviour):

    public experience as int

    
    public Level as int:
        get:
            return (experience / 750)

LevelScriptEditor

Code snippet

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(LevelScript))]
public class LevelScriptEditor : Editor 
{
    public override void OnInspectorGUI()
    {
        LevelScript myTarget = (LevelScript)target;
        
        myTarget.experience = EditorGUILayout.IntField("Experience", myTarget.experience);
        EditorGUILayout.LabelField("Level", myTarget.Level.ToString());
    }
}
#pragma strict

@CustomEditor (LevelScript)
class LevelScriptEditor extends Editor 
{
    function OnInspectorGUI()
    {   
        var myLevelScript : LevelScript = target;
        
        myLevelScript.experience = EditorGUILayout.IntField("Experience", myLevelScript.experience);
        EditorGUILayout.LabelField("Level", myLevelScript.Level().ToString());
    }
}
import UnityEngine
import System.Collections
import UnityEditor


[CustomEditor(typeof(LevelScript))]
public class LevelScriptEditor(Editor):

    public override def OnInspectorGUI():
        myTarget = (target cast LevelScript)
        
        myTarget.experience = EditorGUILayout.IntField('Experience', myTarget.experience)
        EditorGUILayout.LabelField('Level', myTarget.Level.ToString())

Related tutorials