Created
June 12, 2020 10:55
-
-
Save Glavak/5eb73604fc362f1932bb6a7e110c6331 to your computer and use it in GitHub Desktop.
Скрипт для смены сцен в Unity 3D с загрузочным экраном, для туториала https://youtu.be/QfO11O4pUp8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
using UnityEngine.UI; | |
public class SceneTransition : MonoBehaviour | |
{ | |
public Text LoadingPercentage; | |
public Image LoadingProgressBar; | |
private static SceneTransition instance; | |
private static bool shouldPlayOpeningAnimation = false; | |
private Animator componentAnimator; | |
private AsyncOperation loadingSceneOperation; | |
public static void SwitchToScene(string sceneName) | |
{ | |
instance.componentAnimator.SetTrigger("sceneClosing"); | |
instance.loadingSceneOperation = SceneManager.LoadSceneAsync(sceneName); | |
// Чтобы сцена не начала переключаться пока играет анимация closing: | |
instance.loadingSceneOperation.allowSceneActivation = false; | |
instance.LoadingProgressBar.fillAmount = 0; | |
} | |
private void Start() | |
{ | |
instance = this; | |
componentAnimator = GetComponent<Animator>(); | |
if (shouldPlayOpeningAnimation) | |
{ | |
componentAnimator.SetTrigger("sceneOpening"); | |
instance.LoadingProgressBar.fillAmount = 1; | |
// Чтобы если следующий переход будет обычным SceneManager.LoadScene, не проигрывать анимацию opening: | |
shouldPlayOpeningAnimation = false; | |
} | |
} | |
private void Update() | |
{ | |
if (loadingSceneOperation != null) | |
{ | |
LoadingPercentage.text = Mathf.RoundToInt(loadingSceneOperation.progress * 100) + "%"; | |
// Просто присвоить прогресс: | |
//LoadingProgressBar.fillAmount = loadingSceneOperation.progress; | |
// Присвоить прогресс с быстрой анимацией, чтобы ощущалось плавнее: | |
LoadingProgressBar.fillAmount = Mathf.Lerp(LoadingProgressBar.fillAmount, loadingSceneOperation.progress, | |
Time.deltaTime * 5); | |
} | |
} | |
public void OnAnimationOver() | |
{ | |
// Чтобы при открытии сцены, куда мы переключаемся, проигралась анимация opening: | |
shouldPlayOpeningAnimation = true; | |
loadingSceneOperation.allowSceneActivation = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment