• Unity
  • Editor doesn't show new animation set from another component

  • Изменено

I have a GameObject that has an AnimationController component, with an AnimationName property. The Spine object is a child object of this GameObject.

Inside OnValidate on the parent object, I set the AnimationName property of the SkeletonAnimation component, expecting it will change the look of the animation inside Unity Editor. However, this doesn't happen. If I manually change the AnimationName property of the Spine game object, it re-draws the animation correctly.

I took a look inside SkeletonAnimationInspector and I noticed there's a lot of things going on inside TrySetAnimation. Is there an easy way I can trigger the call to this method, just like it gets called from the inspector?

Basically, what I want is to change AnimationName programmatically from the editor, and have it do exactly the same thing as changing AnimationName from SkeletonGraphic's inspector.

This is how I'm changing the name right now:

    private void OnValidate()
    {
        if (animation == null)
        {
            animation = GetComponentInChildren<SkeletonAnimation>();
        }

    if (!animation.valid)
    {
        animation.Initialize(false);
    }

    animation.AnimationName = AnimationName;
}
Related Discussions
...
  • Изменено

It's a bit confusing that in your code you call GetComponentInChildren with SkeletonAnimation as type, while you are are talking about using SkeletonGraphic. Anyway, you are most likely missing the call to the methods in SkeletonGraphic.OnValidate() such as the following:

private void OnValidate () {
   if (animation == null) {
      animation = GetComponentInChildren<SkeletonGraphic>();
   }

   if (!animation.IsValid) {
      animation.Initialize(false);
   }

   animation.startingAnimation = AnimationName;

   // from SkeletonGraphic.OnValidate
   if (!string.IsNullOrEmpty(AnimationName)) {
      animation.AnimationState.ClearTrack(0);
      animation.Skeleton.SetToSetupPose();
      animation.AnimationState.SetAnimation(0, AnimationName, animation.startingLoop);
      animation.Update(0f);
   }
}

I meant SkeletonAnimation, not SkeletonGraphic, I was writing the post somewhat hastily, sorry!

I used your code and it works perfectly now! I tried something similar before, but I was calling LateUpdate() instead of Update(). Thank you for your help. 🙂

You're welcome, glad to hear you've figured it out!