Home > Enterprise >  How to turn off scene transition animations
How to turn off scene transition animations

Time:05-27

How to turn off scene transition animations? I would like to disable the animation ONLY for the RestartGame command. so that the animation works for other commands. Is there any script for such a thing? this is my animation script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelLoader : MonoBehaviour
{
    public Animator transition;
    public float transitionTime = 1f;
    
    public void LoadNextLevel()
    {
        StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex   1));
    }

    public AudioClip impact;
    IEnumerator LoadLevel(int LevelIndex)
    {
        transition.SetTrigger("Start");
        yield return new WaitForSeconds(transitionTime);
        SceneManager.LoadScene(LevelIndex);
        yield return new WaitForSeconds(0.3f);
        AudioSource.PlayClipAtPoint(impact, transform.position);
    }
}

and this is my RestartGame command which is in another script:

public void RestartGame()
{
    SceneManager.LoadScene(PlayerPrefs.GetInt("SavedScene"));
}

CodePudding user response:

In your actualy code, the RestartGame method don't use de animation, but if you have an animation on your scene when this starts, you can save a PlayerPref variable inside the RestartGame method before load the scene, and then check the variable before the animation.

For example:

public void RestartGame()
{
    PlayerPrefs.SetInt("isRestarting",1);
    SceneManager.LoadScene(PlayerPrefs.GetInt("SavedScene"));
}

then in the start of the new scene

void Start ()
{
    if(PlayerPrefs.GetInt("isRestarting",0)!=1)
    {
      //Do the animation
    }

}

CodePudding user response:

You can add another option to IEnumerator that controls the animation.

IEnumerator LoadLevel(int LevelInde, bool hasAnimation = true)
{
    if (hasAnimation)
    {
        transition.SetTrigger("Start");
        yield return new WaitForSeconds(transitionTime);
    }
    SceneManager.LoadScene(LevelIndex);
    yield return new WaitForSeconds(0.3f);
    AudioSource.PlayClipAtPoint(impact, transform.position);
}

Now set it to true when restart and false to other cases. On LoadNextLevel:

StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex   1, false));
  • Related