My custom property drawers are being applied to fields of MonoBehaviours when drawing a gameobject inspector. But an inspector of a ScriptableObject asset ignores custom property drawers, it shows the default field drawers instead.
Thank you both for your replies. LightStriker is right, I’m working with .asset files that are ScriptableObjects. They contain rules for population a scene with GameObjects, which is why they are not part of a scene themselves. Selecting such a file will bring up the usual inspector, with default property drawers for all public fields. It’s just that this inspector ignores my custom drawers.
It works fine for me, within ScriptableObject assets. Here’s a cut-down example:
// MyPropertyType.cs
using System;
[Serializable]
public class MyPropertyType
{
public string Value;
}
// Editor/MyPropertyDrawer.cs
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(MyPropertyType))]
public class MyPropertyDrawer : PropertyDrawer
{
private float _height = 100;
private float _indent = 30;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return _height;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive),
new GUIContent(label.text));
var valueProperty = property.FindPropertyRelative("Value");
float labelHeight = base.GetPropertyHeight(property, label);
var region = new Rect(position.x + _indent, position.y + labelHeight, position.width - _indent - 10, position.height - labelHeight - 10);
EditorGUI.BeginChangeCheck();
var newValue = EditorGUI.TextArea(region, valueProperty.stringValue);
if (EditorGUI.EndChangeCheck())
{
valueProperty.stringValue = newValue;
}
EditorGUI.EndProperty();
}
}
// TestScriptableObject.cs
using UnityEngine;
public class TestScriptableObject : ScriptableObject
{
public MyPropertyType Field1;
public MyPropertyType Field2;
}
// Editor/CreateTestScriptableObject.cs
using UnityEditor;
using UnityEngine;
public class CreateTestScriptableObject
{
[MenuItem("Assets/Create/TestScriptableObject")]
public static void CreateAsset()
{
var asset = ScriptableObject.CreateInstance<TestScriptableObject>();
AssetDatabase.CreateAsset(asset, "Assets/Whatever.asset");
AssetDatabase.SaveAssets();
}
}
Execute the menu option to create “Whatever.asset”, then select it, and the Inspector should show multi-line TextArea boxes for editing the strings within the MyPropertyType fields.
Make sure you haven’t got the Inspector in “debug” mode though - in that mode it won’t use custom PropertyDrawers. I guess it’s a feature, as it lets you edit things the PropertyDrawers don’t expose.
Thank you for the full example. Based on it I figured out why my custom drawer was ignored: the ScriptableObject class name has to match the name of the file it lives in, like it is for MonoBehaviours.
This is pretty obscure, because with non-matching names the asset still works fine besides ignoring custom drawers.
Hey one question, i have also a Scriptable Object type and used the scripts above to get my PropertyDrawer working, it works well for single Objects but as soon as i want to display a list of this Object in the inspector i get a strange behaviour. When i change lets say element 1 of the list every other element of this List gets the same value assigned. How can i fix this?
I’ve had the same problem as OP, except all my files were already matching the classes names.
In the end it turned out I was using an old asset I created very early, the PropertyDrawer did in fact work on newly created assets.