- Home /
How to create a custom editor for a ScriptableObject?
I keep seeing the same question asked and I keep seeing the same answers and the same "I did that but it didn't work" and now I am in that same boat. No idea what to do but every time I tab out of my field I just edited, the value is lost. Also, how do I actually show an array or a custom serialized type when doing a custom display?
[System.Serializable]
public class GameData : ScriptableObject {
public int
layout;
[SerializeField]
public GameDataWord[]
words;
}
[System.Serializable]
public class GameDataWord {
public string word;
public int x, y;
}
[CustomEditor(typeof(GameData))]
public class GameDataEditor : Editor {
public override void OnInspectorGUI() {
GameData gd = (GameData)target;
EditorGUILayout.IntField ("Layout number", gd.layout);
if(GUI.changed)
EditorUtility.SetDirty(target);
}
}
I tried doing the whole serializedObject thing but again, I had no idea how to specify my custom types or display them via the EditorUtility or GUI functions. So what am I doing wrong here? Why can't I edit my values and have them persist once the field loses focus?
p.s. I tried SetDirty(gd) and the deprecated gd.SetDirty() also. Same result...
btw: It makes no difference if you pass "gd" or "target". Both are the same reference. The parameter of SetDirty is of type UnityEngine.Object. So you can only pass references to objects which are derived from UnityEngine.Object. From the method's point of view it always receives a UnityEngine.Object reference.
Yeah, like I said below, this is my first attempt at a custom editor. When I got stuck I turned to Google. That sent me here and to a number of other places and from what I read Scriptable objects are harder to create editors for than $$anonymous$$onoBehaviors and one guy went "Yeah, that worked. I just changed from ScriptableObject to $$anonymous$$onoBehavior and now it works". Another guy went "Your entire tutorial was fine but it didn't work for me until I used GetComponent on the target". Then I read about using the deprecated function on the ScriptableObject and I just tried everything than anyone threw my way to try...
I did run into problems when I tried to set serializedProperties manually, though, and trying to pass them to functions that want int's and strings etc. I tried casting but just got errors about not being able to cast to the relevant types so I started showing the values via scriptableObject and manually modifying it via gd.
But yeah... I thought they were the same thing but tried everything I read others had done before me. got nowhere fast... but we all know why THAT was, now, don't we? :P
Answer by Bunny83 · Jun 21, 2016 at 09:00 AM
Well, first of all you don't change the value at all. IntField will return the changed value. You pass your "layout" value to the method. Since method parameters are always passed by value (unless it's a ref or out parameter) the method has no way to actually change the variable.
So you have to do this:
gd.layout = EditorGUILayout.IntField ("Layout number", gd.layout);
Next thing is, why do you want to implement a custom editor for that class? Handling arrays manually can be a real pain. In most cases the default editor works just fine for classes like you have here.
If you just want to change the way your array elements are drawn, implementing a PropertyDrawer would be more efficient.
Also if you're not familiar with the concept of ScriptableObjects, make sure you watched this introduction video just to make sure you actually handle it right.
Finally i just want to mention that the CustomEditor for a scriptableObject works 100% the same for MonoBehaviour components. ScriptableObjects are just a bit more complicated in the way you create them as asset.
Hi Bunny.... Suddenly I wish I didn't post this question. Forgetting to assign the value from IntField and not realizing it.... Good grief what a facepalm moment... The rest of your reply sounds like you are treating me like a complete n00b but with THAT facepalm moment behind me, I can't blame you...
As for why I am doing this, well, this is my first ever custom GUI and what I posted above was just how far I had gotten. I managed to get it working just fine using SerializedProperties, eventually, but now just need to figure out how to actually place the array elements under the parent, not appear as standalone entries. I am assu$$anonymous$$g the FoldOut is gonna come to my rescue, I think...
The thing is, I am trying to create a data object that contains 2 strings and then a bunch of arrays that include an array and a custom data type. Each top level array entry is a different board layout and showing all layouts as one long list and exposing each array entry and each custom data object's entries for every single layout as one insanely long list of inspector properties just looks like the thing is way more complicated than it really is.
$$anonymous$$y goal is to have an int value that is displayed as a label, then have First, Previous, Next and Last buttons next to it and then display only the two arrays corresponding to the current board layout. Do a lot of extra work to make the final result look a lot less intimidating when working on it. That is the goal.
Like I said, I managed to get quite far already. So far I only have two problems left to figure out: 1. Since EditorUtility doesn't have a Button function I need to rely on GUILayout but using 4 GUILayout.Buttons after a EditorGUILayout.LabelField inside a EditorGUILayout.BeginHorizontal() is making my final button go over the edge of the screen. Don't know why but I think a simple solution would be to replace the LabelField with a simple GUILayout.Label ins$$anonymous$$d.
Showing my array entries under the foldout ins$$anonymous$$d of as independent items. currently doing this:
EditorGUILayout.PropertyField (blanks); if (blanks.hasChildren) for( int i = 0; i < blanks.arraySize; i++) { EditorGUILayout.PropertyField (blanks.GetArrayElementAtIndex(i)); }
Clearly there is nothing in there to indicate a parent child relationship. I am thinking of using a foldout and getting rid of trying to show the parent since that clearly doesn't work. Unless there is a simpler way...???
Your answer
Follow this Question
Related Questions
Problem with ScriptableObject and Custom Editor 2 Answers
Show scriptable objects parameter into an array 2 Answers
Custom Editor for a scriptable object with an abstract list 2 Answers
Prevent double serialization of a custom class 0 Answers
Manually Saving ScriptableObject due to 2D Array... 0 Answers