Question by
chuakckc92 · Dec 03, 2018 at 10:11 AM ·
scriptable objectpropertydrawerserializedobjectpropertyfield
,Custom PropertyAttribute not saving in ScriptableObject
I have a ScriptableObject:
public class MySO : ScriptableObject
{
[FileName] public string fileName;
}
that contains a custom PropertyAttribute I'm trying to write:
public class FileNameAttribute : PropertyAttribute
{
public string name;
}
I then have a custom PropertyDrawer to draw that attribute so that when an object is dragged to the propertyfield, it inputs the name: [CustomPropertyDrawer(typeof(FileNameAttribute))] public class FileNameDrawer : PropertyDrawer { private Vector2 mousePosition;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position,label,property);
Rect contentPosition = EditorGUI.PrefixLabel(position, label);
FileNameAttribute fileName = (FileNameAttribute)attribute;
EditorGUI.TextField(contentPosition, fileName.name);
Event e = Event.current;
if(e.type == EventType.DragUpdated)
{
e.Use();
}
else if(e.type == EventType.DragExited)
{
var objects = DragAndDrop.objectReferences;
if(objects.Length > 0 && contentPosition.Contains(e.mousePosition))
{
fileName.name = objects[0].name;
property.serializedObject.ApplyModifiedProperties();
}
e.Use();
}
EditorGUI.EndProperty();
}
}
Everything works as intended, except that when I click away from the SO and back to it, the propertyfield resets to null. What is wrong and how do I solve this?
Comment