FlyingFrog

LoadingScreen

Jul 31st, 2025
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | Gaming | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. using UnityEngine.UI;
  5.  
  6. public class LoadingScreen : MonoBehaviour
  7. {
  8.     [SerializeField] private int _sceneToLoad = 2; // Индекс уровня для загрузки
  9.     [SerializeField] private Slider _progressBar;
  10.  
  11.     void Start()
  12.     {
  13.         StartCoroutine(LoadAsyncScene());
  14.     }
  15.  
  16.     IEnumerator LoadAsyncScene()
  17.     {
  18.         _progressBar.value = 0;
  19.         AsyncOperation operation = SceneManager.LoadSceneAsync(_sceneToLoad);
  20.         operation.allowSceneActivation = false;
  21.  
  22.         while (!operation.isDone)
  23.         {
  24.             float progress = Mathf.Clamp01(operation.progress / 0.9f);
  25.  
  26.             if (_progressBar != null)
  27.                 _progressBar.value = progress;
  28.  
  29.             // Когда загрузка завершена (до 0.9), активируем сцену
  30.             if (operation.progress >= 0.9f)
  31.             {
  32.                 yield return new WaitForSeconds(1f); // пауза, но можно удалить
  33.                 operation.allowSceneActivation = true;
  34.             }
  35.  
  36.             yield return null;
  37.         }
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment