using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using TMPro; public class SceneLoaderUI : MonoBehaviour { [Header("UI Elements")] public TextMeshProUGUI loadingText; public Image loadingBarFill; [Header("Settings")] public float fakeLoadDelay = 0.1f; // Small delay to smooth progress private void Start() { LoadSceneByName("Menu"); } public void LoadSceneByName(string sceneName) { StartCoroutine(LoadAsync(sceneName)); } private IEnumerator LoadAsync(string sceneName) { if (loadingText != null) loadingText.text = "Loading..."; AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName); operation.allowSceneActivation = false; float progress = 0f; while (!operation.isDone) { // Scene progress goes up to 0.9 — the last 0.1 is when allowSceneActivation = true float targetProgress = Mathf.Clamp01(operation.progress / 0.9f); // Smooth progress bar update progress = Mathf.MoveTowards(progress, targetProgress, fakeLoadDelay); if (loadingBarFill != null) loadingBarFill.fillAmount = progress; if (loadingText != null) loadingText.text = $"Loading... {(int)(progress * 100)}%"; // When progress reaches 1, activate the scene if (Mathf.Approximately(progress, 1f)) operation.allowSceneActivation = true; yield return null; } } }