Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is there a way to prevent animation buffering during scene loading?

Discussion in 'Scripting' started by chealin, Oct 6, 2023 at 3:19 AM.

  1. chealin

    chealin

    Joined:
    Sep 10, 2017
    Posts:
    72
    Hello

    I made a loading scene

    I added a simple character animation to the loading scene
    (Animation that changes only the scale value)

    But the animation seems to be cut off during loading
    Do you happen to know how to make the animation unaffected by loading?

    I'd really appreciate it if you let me know


    upload_2023-10-6_11-18-52.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class LoadingManager : MonoBehaviour
    8. {
    9.  
    10.     public static string nextScene;
    11.  
    12.     [SerializeField]
    13.     Image progressBar;
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         StartCoroutine(LoadScene());
    19.     }
    20.  
    21.     public static void LoadScene(string sceneName)
    22.     {
    23.         nextScene = sceneName;
    24.         SceneManager.LoadScene("Loading");
    25.     }
    26.  
    27.     IEnumerator LoadScene()
    28.     {
    29.         yield return null;
    30.         AsyncOperation op;
    31.         op = SceneManager.LoadSceneAsync(nextScene);
    32.         op.allowSceneActivation = false;
    33.         float timer = 0.0f;
    34.  
    35.         while (!op.isDone)
    36.         {
    37.             yield return null;
    38.             timer += Time.deltaTime;
    39.  
    40.             if (op.progress < 0.9f)
    41.             {
    42.                 progressBar.fillAmount = Mathf.Lerp(progressBar.fillAmount, op.progress, timer);
    43.                 if (progressBar.fillAmount >= op.progress)
    44.                 {
    45.                     timer = 0f;
    46.                 }
    47.             }
    48.             else
    49.             {
    50.                 progressBar.fillAmount = Mathf.Lerp(progressBar.fillAmount, 1f, timer);
    51.                 if (progressBar.fillAmount == 1.0f)
    52.                 {
    53.                     op.allowSceneActivation = true;
    54.                     yield break;
    55.                 }
    56.             }
    57.         }
    58.     }
    59. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,283
    Unity scripting is single-threaded. Anything that interrupts that single thread will also interrupt every other thing done by that thread, such as your animation.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    MaskedMouse likes this.