maxhacker11

LevelManager.cs

Jan 16th, 2024
1,371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | Source Code | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5. using System.Linq;
  6.  
  7. public class LevelManager : MonoBehaviour
  8. {
  9.     public static LevelManager Instance;
  10.  
  11.     public Slider progressBar;
  12.     public GameObject transitionsContainer;
  13.  
  14.     private SceneTransition[] transitions;
  15.  
  16.     private void Awake()
  17.     {
  18.         if (Instance == null)
  19.         {
  20.             Instance = this;
  21.             DontDestroyOnLoad(gameObject);
  22.         }
  23.         else
  24.         {
  25.             Destroy(gameObject);
  26.         }
  27.     }
  28.  
  29.     private void Start()
  30.     {
  31.         transitions = transitionsContainer.GetComponentsInChildren<SceneTransition>();
  32.     }
  33.  
  34.     public void LoadScene(string sceneName, string transitionName)
  35.     {
  36.         StartCoroutine(LoadSceneAsync(sceneName, transitionName));
  37.     }
  38.  
  39.     private IEnumerator LoadSceneAsync(string sceneName, string transitionName)
  40.     {
  41.         SceneTransition transition = transitions.First(t => t.name == transitionName);
  42.  
  43.         AsyncOperation scene = SceneManager.LoadSceneAsync(sceneName);
  44.         scene.allowSceneActivation = false;
  45.  
  46.         yield return transition.AnimateTransitionIn();
  47.  
  48.         progressBar.gameObject.SetActive(true);
  49.  
  50.         do
  51.         {
  52.             progressBar.value = scene.progress;
  53.             yield return null;
  54.         } while (scene.progress < 0.9f);
  55.  
  56.         yield return new WaitForSeconds(1f);
  57.  
  58.         scene.allowSceneActivation = true;
  59.  
  60.         progressBar.gameObject.SetActive(false);
  61.  
  62.         yield return transition.AnimateTransitionOut();
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment