Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Asynchronous Spine animation while loading the Gameplay Scene

Discussion in 'Animation' started by NightKotGames, Sep 26, 2021.

  1. NightKotGames

    NightKotGames

    Joined:
    Dec 14, 2020
    Posts:
    8
    Hi people. The problem has arisen.
    There is Spine animation on canvas and asynchronous level loading.
    It is necessary to ensure that the animation plays during the loading of the scene (tips for the game will still be shown in the cloud),
    and now the animation starts after loading the scene.



    the first script (hangs on the LoadingPlane component)



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class LoadingHelper : Singleton<LoadingHelper>
    9. {
    10. [SerializeField] GameObject _panel;
    11. [SerializeField] private Slider _progressSlider;
    12. [SerializeField] private TextMeshProUGUI _progressText;
    13. [SerializeField] private float _pause;
    14.  
    15. public void LoadScene(int sceneID)
    16. {
    17.  
    18. _panel.SetActive(true);
    19. StartCoroutine(AsyncLoad());
    20.  
    21. IEnumerator AsyncLoad()
    22. {
    23. AsyncOperation operation = SceneManager.LoadSceneAsync(sceneID);
    24. StartCoroutine(Pausing());
    25. while (!operation.isDone)
    26. {
    27. float progress = operation.progress / .9f;
    28. _progressSlider.value = progress;
    29. _progressText.text = $"Загружено {progress * 100} %";
    30. yield return null;
    31.  
    32. }
    33. StartCoroutine(Pausing());
    34. //_panel.SetActive(false);
    35. }
    36.  
    37. IEnumerator Pausing()
    38. {
    39. yield return new WaitForSeconds(_pause);
    40. StopCoroutine(Pausing());
    41. }
    42. }
    43.  
    44. }

    Second script that starts the animation (Hangs on Plane, turns on OnEnable when the loading bar appears)


    Code (CSharp):
    1. using Spine.Unity;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Threading.Tasks;
    5. using UnityEngine;
    6.  
    7. [RequireComponent(typeof(Animator),typeof(SkeletonGraphic))]
    8. public class LoadingAntsAnimations : MonoBehaviour
    9. {
    10.  
    11. private SkeletonGraphic _anim;
    12. private SkeletonDataAsset _asset;
    13.  
    14.  
    15. private void Awake()
    16. {
    17. _asset = GetComponent<SkeletonGraphic>().skeletonDataAsset;
    18. _anim = GetComponent<SkeletonGraphic>();
    19.  
    20. }
    21.  
    22. public void OnEnable()
    23. {
    24. SetAnimation();
    25. }
    26.  
    27. private async void SetAnimation()
    28. {
    29. await Task.Run(()=>
    30. {
    31. _anim.Skeleton.SetSkin("Running");
    32. _anim.Skeleton.SetSlotsToSetupPose();
    33. _anim.AnimationState.SetAnimation(0, "run", true);
    34. _anim.Update(0);
    35.  
    36. });
    37. }
    38.  
    39.  
    40. }
     
    Last edited: Oct 6, 2021