Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrDude · Jun 21, 2016 at 08:31 AM · scriptableobjectcustomeditor

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrDude · Jun 21, 2016 at 08:33 AM 0
Share

p.s. I tried SetDirty(gd) and the deprecated gd.SetDirty() also. Same result...

avatar image Bunny83 MrDude · Jun 21, 2016 at 09:08 AM 0
Share

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.

avatar image MrDude Bunny83 · Jun 21, 2016 at 09:46 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrDude · Jun 21, 2016 at 09:33 AM 0
Share

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.

  1. 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

45 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges