Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AnimationCurve not refreshed in inspector

Discussion in 'Scripting' started by iwHiteRabbiT, Oct 21, 2014.

  1. iwHiteRabbiT

    iwHiteRabbiT

    Joined:
    Feb 15, 2011
    Posts:
    47
    Hi everybody!

    I have a MonoBehavior with a public curve attribute:

    Code (CSharp):
    1. public class Sample : MonoBehavior
    2. {
    3.    public AnimationCurve curve;
    4. }
    With the associated editor class:

    Code (CSharp):
    1. public class SampleEditor : Editor
    2. {
    3.    public override void OnInspectorGUI()
    4.    {
    5.       DrawDefaultInspector();
    6.  
    7.       serializedObject.Update();
    8.  
    9.       if (GUILayout.Button("Do something"))
    10.       {
    11.          // Change anything in the curve like add, remove, move a keyframe
    12.          ((Sample)target).RemoveKey(0);
    13.       }
    14.    }
    15. }
    This works perfectly but the problem is that the preview of the AnimationCurve is not refreshed.
    If I change the inspector size by dragging the mouse, the curve representation is refreshed (although I've noticed that if I go back to exactly the same inspector size, the old curve preview came back).
    So I suspected a cache problem, but I now have tried everything I knew without any success, like:
    • serializedObject.SetIsDifferentCacheDirty();
    • serializedObject.Update();
    • serializedObject.ApplyModifiedProperties();
    • EditorUtility.SetDirty(target);
    • Repaint();
    • Re create my own Property, etc...

    And I can't see anything public in SerializedProperty or AnimationCurve to refresh the preview or tell to do it.
    However, the curve editing window do it very well...

    Any ideas are welcome! ^^'
     
  2. DomingoGD

    DomingoGD

    Joined:
    Jan 28, 2018
    Posts:
    16
    Old thread, but I am jumping into the same issue.

    I want to kept the animation curve window both opened and refreshing to display current status of the script output.

    Any updates on this? (Sorry for using old threat but I guess it is better than opening a new one and duplicate same content / issue).
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,915
    Since the old way of writing custom editors is used (which means direct access to the target property) you have to record the state of the object before you change it. Something like that:

    Code (CSharp):
    1.  
    2.       if (GUILayout.Button("Do something"))
    3.       {
    4.          Undo.RecordObject(target, "Done something");
    5.          ((Sample)target).RemoveKey(0);
    6.       }

    You guessed wrong ^^. Please read the sticky code of conduct post at the top of the scripting forum. You essentially try to hijack the thread for your own purpose because in almost all cases the actual content is not the same. You usually have a slightly different code than what was in the OP. So you would now start posting your code to have your specific problem solved. And that's hijacking a thread.
     
    DomingoGD and Kurt-Dekker like this.
  4. DomingoGD

    DomingoGD

    Joined:
    Jan 28, 2018
    Posts:
    16
    Thank you! I solved the issue with both recording the state and not extending the AnimationCurve Class. For some reason extending it and accessing to the property with 'extendedClassObject'.keys did not work.

    Even though I agree with the conde of conduct. I don't think this is the case. My doubt is exactly what the title of the tread says, exactly what a person with the same problem will google, and to where they will be redirected. So better to avoid hundreds of thread names variation to which you need 15 tabs to actually jump into the answer. IMO.