Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Lokkij · Dec 28, 2013 at 11:02 PM · c#editorserializedproperty

Easy way to set multiple SerializedProperties

I am making a custom editor for one of my objects. A highly simplified version of that object looks like this:

 public class MyClass : MonoBehaviour {
     public int valueA;
     public int valueB;    
     // Whether we want to use value A or B
     public bool useA;
 }

When useA is true, valueB is irrelevant and does not need to be shown in the inspector. This is why the custom editor class looks like this:

 [CustomEditor(typeof(MyClass)), CanEditMulitpleObjects]
 public class MyClassEditor : Editor {
     public override void OnInspectorGUI() {
         SerializedObject tg = new SerializedObject(target);
         bool useA = tg.FindProperty("useA");
         useA.boolValue = EditorGUILayout.Toggle("Use A", useA.boolValue);
         if(useA.boolValue) {
             int valueA = tg.FindProperty("valueA");
             valueA.intValue = EditorGUILayout.Toggle("Value A", valueA.intValue);
         } else {
             // Do the same for valueB
         }
         tg.ApplyModifiedProperties();
     }
 }

Now, I am completely new to SerializedObjects, so this might be horribly ineffecient (if so, please tell me!). That is however not the issue. When you have a lot of values to be set in the inspector, the code for the custom editor becomes pretty convoluted and is a lot of work to maintain, as you have to do FindProperty() first, then set it using EditorGUILayout, etc. So my question is: is there an easier way of obtaining values from a SerializedObject? To write this code "cleaner"?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by vexe · Feb 20, 2014 at 11:45 AM

Editor codes tend to get quite messy and long, that's why it's recommended to have a GUIHelper type of library to manage your often-used stuff, makes life simpler, much less code.

AFAIK, there's no way around FindProperty - You could use EditorGUILayout.PropertyField instead of manually using Toggles, etc. Using PropertyField, it will automatically detect the type of the property, and it will handle undo/redo for you (your current code doesn't!) - Also, you don't need to create a new SerializedObject, there's already a reference to the SerializedObject of your target which is serializedObject. And finally, that code shouldn't compile, FindProperty returns a SerializedProperty, and you're assigning that to an int.

So here's the cut version of your code:

 [CustomEditor(typeof(MyClass)), CanEditMulitpleObjects]
 public class MyClassEditor : Editor {

     public override void OnInspectorGUI() {
         serializedObject.Update(); // don't forget this
         SerializedProperty useA = serializedObject.FindProperty("useA");
         EditorGUILayout.PropertyField(useA);
         if(useA.boolValue) {
             SerializedProperty valueA = serializedObject.FindProperty("valueA");
             EditorGUILayout.PropertyField(valueA);
         } else {
             // Do the same for valueB
         }
         serializedObject.ApplyModifiedProperties(); // nor this
     }
 }

If you want, you could make an InitSp method to initialize your serialized properties and make your gui code a little nicer, to do this you have to have them as member fields.

Comment
Add comment · 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

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

19 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

Related Questions

How do i stop my Editor window values from reseting? 0 Answers

Custom Inspector: Using SeralizedProperty changes the prefab values! 0 Answers

How to change inspector with non-Monobehaviour objects ? 1 Answer

Changing Inspector's Serialized Property Label (With Code Sample) 1 Answer

Select custom object from a list via PopUp in custom editor for ScriptableObject 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