Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class LoadingScreen : MonoBehaviour
- {
- [SerializeField] private int _sceneToLoad = 2; // Индекс уровня для загрузки
- [SerializeField] private Slider _progressBar;
- void Start()
- {
- StartCoroutine(LoadAsyncScene());
- }
- IEnumerator LoadAsyncScene()
- {
- _progressBar.value = 0;
- AsyncOperation operation = SceneManager.LoadSceneAsync(_sceneToLoad);
- operation.allowSceneActivation = false;
- while (!operation.isDone)
- {
- float progress = Mathf.Clamp01(operation.progress / 0.9f);
- if (_progressBar != null)
- _progressBar.value = progress;
- // Когда загрузка завершена (до 0.9), активируем сцену
- if (operation.progress >= 0.9f)
- {
- yield return new WaitForSeconds(1f); // пауза, но можно удалить
- operation.allowSceneActivation = true;
- }
- yield return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment