Unity Custom Inspector Oluşturma

For example, we have created a script that will change the color of the image we added to our canvas. However, in cases like this, where we don't want the color field to be visible without making an assignment to the image, we need to use the Custom Inspector feature.

using UnityEngine;
using UnityEngine.UI;
namespace alisahanyalcin
{
    public class ImageColorChanger : MonoBehaviour
    {
        public Image image;
        public Color color;
    }
}

When we create a script like this in our scene and assign it to any game object, it will appear as follows:

But we only want the color field to be visible when the Image field is filled. For this, we need to create a folder named "Editor" and create a script file named ImageColorChangerEditor inside it.

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();
        }
    }
}

The result we will achieve will be as follows: