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 /
  • Help Room /
avatar image
0
Question by jana-jarecki · Jun 21, 2017 at 08:51 AM · c#custom editorcustom-inspectorpropertydrawerserializedproperty

ApplyModifiedProperties() ignored on nested serialized property w custom editor and custom property drawer

Hi, I am a bit lost:

I have written a custom editor which draws a serializable property sp, but all values I assign to sp vanish on play. Importantly, the serialized property is the first child of an array that itself is a serialized property. The serialized property sp is drawn with a custom property drawer.

  • I have added sp.Update() and sp.ApplyModifiedProperties() before and after modifying the serialized property sp in the editor script

  • The custom editor script includes EditorUtility.SetDirty(t) at the end as well as Undo.RecordObject(t, "xxx") on the target object t.

What could go wrong here?

Any help would be very much appreachiated!


The screenshot shows the custon editor. If I click play, the fields "nuts", "berries" (...) disappear. Anything else that is not part of the seralized property stays.

alt text


This is the Feature Class that I want to assign values to

 [System.Serializable]
 public struct Feature
 {
     public string Name;
     public string Label;
     public Sprite[] FeatureValues;
     public string[] FeatureValueLabels;
 }


It is the child of another class, "NestedFeatures"

 [System.Serializable]
 public class NestedFeature
 {
     [HideInInspector]
     public string Name;
     [HideInInspector]
     public string Label;
     [HideInInspector]
     public GameObject Prefab;
     public Feature[] Features;
 }

I draw them in a custom editor for the "StimulusManager"

 [CustomEditor(typeof(StimulusManager))]
 public class StimulusManagerEditor : Editor
 {
     void OnEnable()
     {
         _target = (StimulusManager) target;        
         _targetSerial = new SerializedObject(target);
      }

     public override void OnInspectorGUI()
     {
         // save changes from the editor in the game
         _targetSerial.Update();
         Undo.RecordObject(_target, "Define stimulus");

          // HERE THE NESTED PROPERtY IS DRAWN BUT THE VALUES ARE NOT KEPT
           // Get the property field as child from the NestedFeatures property
          _featuresProperty = _targetSerial.FindProperty("NestedFeatures.Array.data[0]"); //Get the property
         EditorGUILayout.PropertyField(_featuresProperty, true); //draw the property


        // Keep changes
          _targetSerial.ApplyModifiedProperties();
         EditorUtility.SetDirty(_target);
         EditorApplication.update.Invoke();
 }


The custom property drawer is this

 using UnityEngine;
 using UnityEditor;
 [CustomPropertyDrawer (typeof (NestedFeature))]
 public class NestedFeatureDrawer : PropertyDrawer
 {
      public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
         // Get the array called Features in the NestedFeature class
         SerializedProperty FeaturesProperty = property.FindPropertyRelative("Features");
         EditorGUI.BeginProperty(position, label, property);

         var indent = EditorGUI.indentLevel; //store old indent
         EditorGUI.indentLevel = 0; //new indent


         // HERE SOMETHING GOES WRONG, THE CONTENT OF THIS PART VANISHES ON PLAY
         for (int j=0; j < FeaturesProperty.arraySize; j++)
         {

             EditorGUILayout.PropertyField(FeaturesProperty.GetArrayElementAtIndex(j), GUIContent.none, true);
         }

         EditorGUI.indentLevel = indent; //reset indent

         EditorGUI.EndProperty();

     }   
 }




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

2 Replies

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

Answer by TonyLi · Jun 21, 2017 at 04:10 PM

The Editor class already comes with a serializedObject reference, so you can simplify it to:

 [CustomEditor(typeof(StimulusManager))]
 public class StimulusManagerEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         // save changes from the editor in the game
         serializedObject.Update();
         // HERE THE NESTED PROPERTY IS DRAWN BUT THE VALUES ARE NOT KEPT
         // Get the property field as child from the NestedFeatures property
         _featuresProperty = serializedObject.FindProperty("NestedFeatures.Array.data[0]"); //Get the property
         EditorGUILayout.PropertyField(_featuresProperty, true); //draw the property
         // Keep changes
         serializedObject.ApplyModifiedProperties();
         EditorApplication.update.Invoke();
     }
  } 

If something else is going on in that script, this streamlining may resolve the problem.

If not, what happens if you disable your NestedFeatureDrawer class temporarily and let Unity use the default editor for NestedFeature? Does it work correctly?

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
avatar image
0

Answer by jana-jarecki · Jun 22, 2017 at 09:41 PM

ah cool, this did it! Thanks a lot!

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

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

SetDirty not working in Custom Editor with Nested Custom Property drawer 1 Answer

Inheritance, List and CustomPropertyDrawer 0 Answers

No type in ObjectField created in uxml (UIElements) 3 Answers

How can I modify 'SerializedProperty' that contain 'Array' in Custom inspector? 0 Answers

Inheritance, List and PropertyDrawer,CustomPropertyDrawer and Inheritance 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