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 noio · Jan 07, 2016 at 05:57 PM · custom-inspectorserializedobjectproperty fields

Saving changes to mixed CustomInspector

I'm often in the situation where I need some GUI behaviour that does not have a serializedProperty override. For example, I want to use BeginToggleGroup which is a classic control, but the controls inside the toggle group can use a Property Field. In this case I'm mixing the Property Field method with the classic method of directly assigning to the target object.

Examples to show what I mean by "classic" vs "Property Field".

Classic:

 target.someVariable = EditorGUILayout.Toggle("someVariable", target.someVariable)

Property Field:

 EditorGUILayout.PropertyField(serializedObject.FindProperty("someVariable"));

I can't figure out how to save the data using the PropertyField in this case. Whatever I do, changes to someValue in the example below get reverted after deselecting the field. Changes to anotherValue and enabled are preserved fine; because they use the "classic" method of newValue = EditorGUILayout.SomeField( "label", oldValue ).

But the way I understand it, property fields are preferable because they support undo, multi-object editing, and show local prefab changes.

 public class ComplexComponent : MonoBehaviour { //not really, haha
     public bool enabled;
     public float someValue;
     public bool anotherValue;
 }

The editor looks like this:

     public override void OnInspectorGUI () {
 
         ComplexComponent comp = (ComplexComponent)target;
     
         comp.enabled = EditorGUILayout.BeginToggleGroup("Enabled", comp.enabled);
 
         EditorGUILayout.PropertyField(serializedObject.FindProperty("someValue"));
         comp.anotherValue = EditorGUILayout.ToggleLeft("Another Value", comp.anotherValue);
 
         EditorGUILayout.EndToggleGroup();
     }

Additionally, changes to the "classic" fields are not even saved when saving/quitting the scene!

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
2
Best Answer

Answer by brunocoimbra · Jan 07, 2016 at 06:30 PM

Try adding those 2 lines:

 public override void OnInspectorGUI () {
     // It must be on top:
     serializedObject.Update();
 
     ComplexComponent comp = (ComplexComponent)target;
     comp.enabled = EditorGUILayout.BeginToggleGroup("Enabled", comp.enabled);
 
     EditorGUILayout.PropertyField(serializedObject.FindProperty("someValue"));
     comp.anotherValue = EditorGUILayout.ToggleLeft("Another Value", comp.anotherValue);
 
     EditorGUILayout.EndToggleGroup();
 
     // It must be at bottom:
     serializedObject.ApplyModifiedProperties();
 }

Not sure, but maybe it will solve your problems.

Comment
Add comment · Show 6 · 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 noio · Jan 07, 2016 at 08:16 PM 0
Share

This helps! Changes to the Property Field are now preserved! I tried these but I did not know about the Update() call at the start.

However, direct changes to the object still don't result in the scene being marked as "edited".

avatar image brunocoimbra noio · Jan 07, 2016 at 09:27 PM 1
Share

Try using the serialized object, not the target instance, like that:

 using System.Collections;
 using UnityEditor;
 using UnityEngine;
 
 [CustomEditor(typeof(ComplexComponent))]
 [CanEdit$$anonymous$$ultipleObjects]
 public class TestEditor : Editor
 {
     SerializedProperty enabled;
     SerializedProperty someValue;
 
     void OnEnable()
     {
         enabled = serializedObject.FindProperty("enabled");
         someValue = serializedObject.FindProperty("someValue");
     }
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
 
         enabled.boolValue = EditorGUILayout.BeginToggleGroup("Enabled", enabled.boolValue);
 
         EditorGUILayout.PropertyField(serializedObject.FindProperty("someValue"));
 
         // I've removed the "anotherValue", ComplexComponent class doens't have that variable...
         // Why were you trying to get it?
 
         EditorGUILayout.EndToggleGroup();
 
         serializedObject.Apply$$anonymous$$odifiedProperties();
     }
 }
avatar image noio brunocoimbra · Jan 07, 2016 at 09:30 PM 0
Share

This is a really good suggestion, I never thought of doing that! Will modifying the boolValue on the serializedObject also set the correct Undo state and deal with multi-object editing?

Show more comments
avatar image noio · Jan 08, 2016 at 10:08 AM 0
Share

I never saw the reference for "Editor" before, but it would have been really helpful, they even compare using PropertyField and using "old" (what I called "classic") method! http://docs.unity3d.com/ScriptReference/Editor.html

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

32 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

Related Questions

ScriptableObject with Custom Editor resetting data in inspector 1 Answer

Cannot convert serializedObject to float 1 Answer

How to make a custom editor for a serializedObject that is in a script component 0 Answers

Showing the editors of list elements 0 Answers

Draw custom Property Field in Custom Editor 1 Answer


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