AnimationCurve not refreshed in inspector

Hi everybody!

I have a MonoBehavior with a public curve attribute:

public class Sample : MonoBehavior
{
   public AnimationCurve curve;
}

With the associated editor class:

public class SampleEditor : Editor
{
   public override void OnInspectorGUI()
   {
      DrawDefaultInspector();

      serializedObject.Update();

      if (GUILayout.Button("Do something"))
      {
         // Change anything in the curve like add, remove, move a keyframe
         ((Sample)target).RemoveKey(0);
      }
   }
}

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! ^^’

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).

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:

      if (GUILayout.Button("Do something"))
      {
         Undo.RecordObject(target, "Done something");
         ((Sample)target).RemoveKey(0);
      }

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.

2 Likes

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.