- Home /
PropertyDrawers not working with ScriptableObjects
Hi,
I have a ScriptableObject derived class called LocalPlayer.cs that is being created in an EditorWindow instance and saved as a .asset object in the Assets folder. The LocalPlayer class has the Serializable attribute, yet if I create a property drawer for that class, it does not seems to show the propery drawer GUI, but the default GUI. Does anybody know why? Thanks. FYI this is just the basis of the class, which I intend to make much more complex when this basic stuff is done. So any comments on why I use ScriptableObject are not relevant to me.
//LocalPlayer.cs
using UnityEngine;
using System.Collections;
[System.Serializable]
public class LocalPlayer : ScriptableObject {
public int id = -1;
}
//PlayerPropertyDrawer.cs (in some Editor folder ofc.)
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomPropertyDrawer(typeof(LocalPlayer))]
public class PlayerPropertyDrawer : PropertyDrawer {
override public void OnGUI(Rect position, SerializedProperty serializedProperty, GUIContent label) {
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
label = EditorGUI.BeginProperty(position, label, serializedProperty);
// Draw label
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
// Don't make child fields be indented
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// Calculate rects
Rect playerIdRect = position;
playerIdRect.width = position.width * 0.2f;
// Draw fields - passs GUIContent.none to each so they are drawn without labels
EditorGUI.LabelField(playerIdRect, serializedProperty.FindPropertyRelative("id").intValue.ToString());
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
}
Can you show the class where you use your LocalPlayer class as variable? That property drawer doesn't seem to offer a way to actually assign a LocalPlayer instance. $$anonymous$$aybe you confused a PropertyDrawer with a CustomEditor ?
A bit more background would help i guess.