• RuntimesUnity
  • spine timeScale=0 , Keep setting the animation , StackOverflow

This problem should be caused by the excessive amount of mixed animations , There should be an upper limit on the number of mixed animations,This is a code design problem, and the js language also has this problem

CODE:

void Awake()
{
    skeleton.timeScale = 0;
    skeleton.AnimationState.GetCurrent(0).SetMixDuration(0.2f, 0);
}
public SkeletonGraphic skeleton;
public void Update()
{
    for (int i = 0; i < 10; i++)
    {
        skeleton.AnimationState.SetAnimation(0, "animation", false);
    }
}

ERROR:

StackOverflowException: The requested operation caused a stack overflow.
Spine.CurveTimeline1.GetRelativeValue (System.Single time, System.Single alpha, Spine.MixBlend blend, System.Single current, System.Single setup) (at Assets/Plugins/Spine/Runtime/spine-csharp/Animation.cs:449)
Spine.RotateTimeline.Apply (Spine.Skeleton skeleton, System.Single lastTime, System.Single time, Spine.ExposedList`1[T] firedEvents, System.Single alpha, Spine.MixBlend blend, Spine.MixDirection direction) (at Assets/Plugins/Spine/Runtime/spine-csharp/Animation.cs:575)
Spine.AnimationState.ApplyRotateTimeline (Spine.RotateTimeline timeline, Spine.Skeleton skeleton, System.Single time, System.Single alpha, Spine.MixBlend blend, System.Single[] timelinesRotation, System.Int32 i, System.Boolean firstFrame) (at Assets/Plugins/Spine/Runtime/spine-csharp/AnimationState.cs:522)
Spine.AnimationState.ApplyMixingFrom (Spine.TrackEntry to, Spine.Skeleton skeleton, Spine.MixBlend blend) (at Assets/Plugins/Spine/Runtime/spine-csharp/AnimationState.cs:427)
Spine.AnimationState.ApplyMixingFrom (Spine.TrackEntry to, Spine.Skeleton skeleton, Spine.MixBlend blend) (at Assets/Plugins/Spine/Runtime/spine-csharp/AnimationState.cs:358)
Spine.AnimationState.ApplyMixingFrom (Spine.TrackEntry to, Spine.Skeleton skeleton, Spine.MixBlend blend) (at Assets/Plugins/Spine/Runtime/spine-csharp/AnimationState.cs:358)
Spine.AnimationState.ApplyMixingFrom (Spine.TrackEntry to, Spine.Skeleton skeleton, Spine.MixBlend blend) (at Assets/Plugins/Spine/Runtime/spine-csharp/AnimationState.cs:358)
Spine.AnimationState.ApplyMixingFrom (Spine.TrackEntry to, Spine.Skeleton skeleton, Spine.MixBlend blend) (at Assets/Plugins/Spine/Runtime/spine-csharp/AnimationState.cs:358)

Sometimes, I need to use spine as a picture, so I set the time scale to 0, hoping to fix this problem

Related Discussions
...

The StackOverflowException you're encountering is likely due to repeatedly setting the animation in a loop without allowing the animation state to progress, especially with timeScale set to 0. This causes an excessive buildup of mixed animations, leading to a stack overflow. To address this, you should avoid setting the animation multiple times in a single frame. Instead, set the animation once and let it complete before setting it again. You can also consider limiting the number of mixed animations by checking if an animation is already set before setting it again.

void Awake() {
    skeleton.timeScale = 0;
    skeleton.AnimationState.SetAnimation(0, "animation", false);
}

public SkeletonGraphic skeleton;

public void Update() {
    // Ensure the animation is only set once
    if (skeleton.AnimationState.GetCurrent(0) == null) {
        skeleton.AnimationState.SetAnimation(0, "animation", false);
    }
}

This approach ensures that you don't repeatedly set the same animation, which should help prevent the stack overflow issue.

    @a674878955 Unfortunately we could not reproduce this issue. Which exact version of the spine-unity runtime are you using (name of the unitypackage, also listed in Assets/Spine/version.txt or in the Package Manager window)?

    If you are using the latest 4.2 spine-unity runtime package, could you please send us a minimal Unity reproduction project which still shows this issue? You can send it as a zip file to contact@esotericsoftware.com, briefly mentioning this forum thread URL so that we know the context.

      Spinebot I know the cause of the problem, but I think there should be an option to limit the maximum number of mixed animations, sometimes I just need to set animations when timescale=0

      Harald I have sent it to you. The subject of the email is spine StackOverflow

      • Misaki ha risposto a questo messaggio

        a674878955 I have confirmed that I can open the Unity project attached to the email you sent us. I'm not sure if it's because I didn't open it with exactly the same version of Unity, but I didn't get any errors when I opened the "SpineTest" scene in it. (I opened your project with Unity 2022.3.29f1.) Could you provide the steps to see the error with the project?

          Misaki
          I don't know why you didn't repeat that success, but I'll tell you more about how it happened

          First, the animation End event is not executed when the condition is met

          Then, when animation is set, the current track is assigned to the mixingFrom of the new track

          SetAnimation=>SetCurrent=>current.mixingFrom = from


          Finally, enable breakpoint debugging, where you can see unlimited mixingFrom

          Harald
          I'm not sure why you didn't repeat the problem, maybe your unity automatically handled the stackflow problem, maybe you didn't wait long enough, I explain how the problem happened ⬆⬆⬆

          Misaki
          So setting mixingFrom to null will solve this problem

                  //internal mixingFrom => public mixingFrom
                  skeleton.AnimationState.GetCurrent(0).mixingFrom = null;//or ClearTrack()
                  skeleton.AnimationState.SetAnimation(0, "a1", false);
          • Misaki ha risposto a questo messaggio

            a674878955 Thank you for the additional information! We have tested again and have confirmed that we can reproduce the issue at higher FPS. We are currently investigating the cause and will let you know as soon as we find out.

              7 giorni dopo

              @a674878955 The spine-csharp runtime has been updated (in this commit) to automatically cover this situation and no longer produce a stack overflow.

              A new spine-unity 4.2 package is available for download here as usual:
              https://esotericsoftware.com/spine-unity-download

              Regardless, you should in general not repeatedly call AnimationState.SetAnimation every frame. We've added a documentation section here (the "Important Note" subsection):
              https://esotericsoftware.com/spine-unity-main-components#Setting-Animations

                Harald Misaki
                Thank you very much for your help and support. I wish spine better and better. Also, hopefully ts/js will fix this issue as well

                @a674878955 Glad it helped. The commit will be ported to all runtimes, so TypeScript will be covered as well. You can watch this issue ticket which lists porting progress of the different runtimes:
                EsotericSoftware/spine-runtimes2705

                @a674878955 And a few minutes later, the commit has already been ported to TypeScript (in this commit). 🙂

                  Yup! Available in all ts runtimes in 4.2.68!