Editor script for scriptableobject not working.
Hello,
I wanted to add a way to select multiple enum values for an enum in my scriptable object and found out about enummaskfield, but edit scripts don't seem to work with scriptable objects? I tested it with a monobehavior and it works, it's just scriptable object not working. I'm not very familiar with editor scripting so let me know if there's something special I need to do for scriptable objects.
using UnityEditor;
[CustomEditor(typeof(Action))]
public class EditorActionTarget : Editor {
public override void OnInspectorGUI() {
Action myTarget = (Action) target;
myTarget.targetType = (TargetType) EditorGUILayout.EnumMaskField("Enums", myTarget.targetType);
}
}
Thanks
Answer by Adam-Mechtley · Feb 03, 2017 at 07:47 AM
The problem here is that you are writing directly to your target object's fields instead of to it serialized data stream, so the Editor does not know there are changes that are dirty and need to be flushed to disk. You may find e.g., my answer to this question helpful, but in short, you should instead use the Editor.serializedObject property and the SerializedObject.FindProperty() method to access the serialized data stream for the field in question. The result could look something like this:
using UnityEditor;
[CustomEditor(typeof(Action))]
public class EditorActionTarget : Editor {
static readonly GUIContent targetTypeLabel = new GUIContent("Enums");
SerializedProperty targetType;
void OnEnable {
// note that the string passed here needs to match the name of the actual serialized field, in case it is a backing field for an accessor; e.g., it might be m_TargetType in your case
targetType = this.serializedObject.FindProperty("targetType");
}
public override void OnInspectorGUI() {
this.serializedObject.Update();
EditorGUILayout.PropertyField(targetType, targetTypeLabel);
this.serializedObject.ApplyModifiedProperties();
}
}
If you need or want to draw that property using a special type of control, the best way to do so is to use a custom PropertyAttribute and PropertyDrawer. Done properly, this can often obviate the need to make a custom Editor at all.
Hi,
Thanks for the response. So maybe I'm missing something from what you said. I checked out your other question and it looks like his issue was just dealing with the data not saving when it resets. I'm not having that Issue. I just want to be able to multiselect enums using enummaskfield. You answer to me suggests I would have to write this from scratch using PropertyAttributes/Drawers with the above method. Is that right or can I use that method here?
Thanks.
Sorry for the lack of clarity: Yes that is probably what I would do. That said, there's really no reason Unity shouldn't be smart enough to simply use an enum mask drawer if the field type has System.FlagsAttribute
, so I will probably add that to my to-do list.
Your answer
Follow this Question
Related Questions
Changes to asset data persist - unable to discard changes 1 Answer
How to edit Properties of a ScriptableObject asset, specificly arrays/lists 0 Answers
Add ScriptableObject to collection upon creation 0 Answers
ScriptableObject alternative, editable at runtime 0 Answers
ScriptableObject not Serializing? 0 Answers