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 VelociraptorGames · Jun 14, 2017 at 08:25 AM · editorguiserializedpropertyserializedobject

CustomEditor & Serialization not keeping Overrides & not working with Animation

I'm trying to expose my class via a CustomEditor, but I'm running into a few roadblocks.

 public class ZExample : MonoBehaviour
 {
     [SerializeField] public float mColor0Factor = 1;
 }
 
 
 [CustomEditor(typeof(ZExample))]
 [CanEditMultipleObjects]
 public class ZExampleEditor : Editor
 {
     SerializedProperty      mColor0Factor;
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update(); //BEGIN
 
         ZExample lZExample = (ZExample)target;
         lZExample.mColor0Factor =
         EditorGUILayout.Slider( lZExample.mColor0Factor, 0.0f, 2.0f );
 
         serializedObject.ApplyModifiedProperties(); //END
     }
 }

//1 When adding a controller for example, it will revert to the prefab value once you hit play. So it only retains the prefab values, not the local override.

 [SerializeField] public AnimatorController mController = null;

As far as I can tell, [System.Serializable] on the class and [SerializeField] on the properties don't do anything to help with this. Are these needed, or am I mixing custom editor & property drawers ?

//2 When keying "mColor0Factor" via animation, the Inspector GUI doesn't turn red and any changes made to the value via the Inspector GUI are NOT keyed and are overridden by the values already keyed.

This should suposedly be fixed by marking it as dirty on change, but again, this doesn't work on my end.

 EditorGUI.BeginChangeCheck();
 { 
     lZExample.mColor0Factor =
     EditorGUILayout.Slider( lZExample.mColor0Factor, 0.0f, 2.0f );
 }
 if(EditorGUI.EndChangeCheck())
 { EditorUtility.SetDirty(target); }

//3 Is it usefull to get the Property from the class on initialisation and why ?

 mColor0Factor = serializedObject.FindProperty("mColor0Factor");


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 Adam-Mechtley · Jun 14, 2017 at 08:55 AM

As you have no doubt surmised, the answers to all of these questions are related.

In many cases (e.g., simple types and UnityEngine.Object references), if you have a public field, there is no need for explicitly masking the field with SerializeFieldAttribute. This attribute is only necessary for private fields. Respectively, adding System.SerializableAttribute should only be necessary if you have a custom struct or class you need to serialize. You might find this page in the documentation helpful.

In general, whenever you do a custom editor, property drawer, or similar, you should get a handle to properties on the target object using SerializedProperty rather than reading directly from the target object. In your case, it needs to look something like this:

 [CustomEditor(typeof(ZExample)), CanEditMultipleObjects]
 public class ZExampleEditor : Editor {
     SerializedProperty mColor0Factor;

     void OnEnable() {
         mColor0Factor = serializedObject.FindProperty("mColor0Factor");
     }

     public override void OnInspectorGUI() {
         serializedObject.Update();
         // note method override passing SerializedProperty directly
         EditorGUILayout.Slider(mColor0Factor, 0f, 2f);
         serializedObject.ApplyModifiedProperties();
     }
 }

When you use this approach, it will properly handle Undo/Redo, dirtying, animation, and so on. In most cases that's it—you're done!

When you must use some kind of control that cannot accept a SerializedProperty for some reason (or if you make your own control that cannot for some reason), you need to do 2 things:

  1. Use Undo.RecordObject() right before you modify the object (so first thing inside of the if (EditorGUI.EndChangeCheck()) block. This will dirty any modified fields so that you won't lose changes when entering play mode, and also makes the modification compatible with the Undo system.

  2. Enclose the control in calls to EditorGUI.BeginProperty() and EditorGUI.EndProperty(). Per the documentation, these will handle styling and context menus for indicating property dirtiness, reverting to prefab values and so on. What's not as explicit however, is that they are also responsible for the color styling applied when the properties are animated.

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

66 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 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

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

How to get fields of a custom script through SerializedProperty.FindPropertyRelative? 1 Answer

Draw custom Property Field in Custom Editor 1 Answer

Replicate "Apply" and "Revert" button functionality in ScriptableObject Editor Inspector 2 Answers

Initialising List array for use in a 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