Search Unity

[Possible bug] Undo.RecordObject not working after EditorGUILayout.CurveField

Discussion in 'General Discussion' started by Xtro, Feb 8, 2016.

  1. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    608
  2. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    608
    Bump... Please someone help.
     
  3. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    You have posted in the wrong forum section. It should probably be in the editor support section.
     
  4. DenisLemos

    DenisLemos

    Joined:
    May 1, 2015
    Posts:
    787
    The same problem is happening to me, I am implementing a curve system in my Asset Store plugin and Undo does not work with "EditorGUILayout.CurveField". All other parameters works, except the curve fields.
     
  5. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Have you tried forcing a SetDirty()? (Maybe wrapped in your own comparison)
     
  6. DenisLemos

    DenisLemos

    Joined:
    May 1, 2015
    Posts:
    787
    Hi!

    I will demonstrate what I'm doing!

    This code is in "MyScript".
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyScript : MonoBehaviour {
    5.  
    6.     public float          myFloat  = 1.0f;
    7.     public AnimationCurve myCurve  = AnimationCurve.Linear(0,1.0f,10,1.0f);
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.  
    12.     }
    13. }
    And this code is in the Script Editor.
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(MyScript))]
    6. public class MyScriptEditor : Editor {
    7.  
    8.     public override void OnInspectorGUI()
    9.     {
    10.         //Get target
    11.         MyScript Target = (MyScript)target;
    12.         Undo.RecordObject (Target, "Undo Values");
    13.  
    14.         Target.myFloat  =  EditorGUILayout.Slider(Target.myFloat,1.0f, 10.0f, GUILayout.Width(150));
    15.         Target.myCurve  =  EditorGUILayout.CurveField (Target.myCurve, Color.red, new Rect(0,0,10,10),GUILayout.Width (150));
    16.         EditorUtility.SetDirty(target);
    17.     }
    18. }
    If you give a try, you will realize that the slider works normally with Undo, but the curve does not return to the previous setting. It seems that the curves are not recorded in the undo stack, the reason I do not know why. Maybe I'm doing the wrong way or is a bug of Unity, because all other fields works great (ObjectField, ColorField, Toogle, GradientColor, etc...).
     
  7. DenisLemos

    DenisLemos

    Joined:
    May 1, 2015
    Posts:
    787
    Well, I think it should be a bug, because this also happens in normal Unity Inspector, not only in customized Inspectors. If you delete the "MyScriptEditor" and just attach the "MyScript" on any object in the scene and change the curve values. The undo function will only work within the curves editor and the field in the Inspector is not updated.

    I'm using the Unity 5.3.1!

    And the right way to write the scripts of my above post is to use " SerializedProperty " and " EditorGUILayout.PropertyField ":

    "MyScripty"
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyScript : MonoBehaviour {
    5.  
    6.     public float          myFloat  = 1.0f;
    7.     public AnimationCurve myCurve  = AnimationCurve.Linear(0,1.0f,10,1.0f);
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.  
    12.     }
    13. }
    "MyScriptEditor"
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(MyScript))]
    6. public class MyScriptEditor : Editor {
    7.  
    8.         SerializedObject   serObj;
    9.         SerializedProperty myCurve;
    10.  
    11.     void OnEnable(){
    12.                 serObj  = new SerializedObject (target);
    13.                 myCurve = serObj.FindProperty ("myCurve");
    14.     }
    15.  
    16.     public override void OnInspectorGUI()
    17.     {
    18.         serObj.Update ();
    19.         MyScript Target = (MyScript)target;
    20.         Undo.RecordObject (Target, "Undo Values");
    21.  
    22.         Target.myFloat  =  EditorGUILayout.Slider(Target.myFloat,1.0f, 10.0f, GUILayout.Width(150));
    23.         EditorGUILayout.PropertyField (myCurve, GUIContent.none, GUILayout.Width (127));
    24.         serObj.ApplyModifiedProperties ();
    25.     }
    26. }
    Thus the undo function will work within the curves editor, only the field of the curve in the Inspector will not update, but it should really be a bug of Unity.
     
  8. GlitchedPolygons

    GlitchedPolygons

    Joined:
    Jun 18, 2013
    Posts:
    210
    I had reported this bug a while back (doesn't work with SerializedProperty for me either), and they got back to me about this today. Apparently, they were able to repro, but just don't give a damn. Check it out:

     
  9. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    608
    HAHAHA. That's very familiar. I get the same response when I report other bugs. It's just a copy paste support message. Unity is going to a dead end because of the amount of reported but not fixed bugs over many years.
     
  10. GlitchedPolygons

    GlitchedPolygons

    Joined:
    Jun 18, 2013
    Posts:
    210
    So it took them almost half a year to send me a copy-pasta...