Skip to content

Unity Custom Inspector

Learn how to create a custom inspector in Unity.


For example, we have created a script that will change the color of the image we added to our canvas. We want to show the color field only if the image field is not empty. To do this, we need to create a custom inspector.

csharp
using UnityEngine;
using UnityEngine.UI;
namespace alisahanyalcin
{
    public class ImageColorChanger : MonoBehaviour
    {
        public Image image;
        public Color color;
    }
}
csharp
using UnityEditor;
namespace alisahanyalcin
{
    [CustomEditor(typeof(ImageColorChanger))]
    public class ImageColorChangerEditor : Editor
    {
        private SerializedProperty _color;
        private SerializedProperty _image;
        
        private void OnEnable()
        {
            _image = serializedObject.FindProperty("image");
            _color = serializedObject.FindProperty("color");
        }
        
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            if (_image.objectReferenceValue != null)
                EditorGUILayout.PropertyField(_color);
            
            serializedObject.ApplyModifiedProperties();
        }
    }
}

Result

Game Developer Ali Şahan Yalçın