| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using TMPro;
- using System.Collections;
- public class LoadingManager : MonoBehaviour
- {
- public static string sceneToLoad;
- [Header("UI")]
- public TMP_Text continueText;
- public Image progressFill;
- public float minimumLoadTime = 2f;
- public TMP_Text tipText;
- [Header("Tips")]
- [TextArea(2, 4)]
- public string[] tips;
- void Start()
- {
- StartCoroutine(LoadAsync());
- }
- void Update()
- {
- if (progressFill != null)
- progressFill.fillAmount = Mathf.PingPong(Time.time, 1f);
- }
- IEnumerator LoadAsync()
- {
- AsyncOperation operation =
- SceneManager.LoadSceneAsync(sceneToLoad);
- operation.allowSceneActivation = false;
- float displayedProgress = 0f;
- if (continueText != null)
- continueText.gameObject.SetActive(false);
- if (tips.Length > 0 && tipText != null)
- {
- int randomIndex = Random.Range(0, tips.Length);
- tipText.text = "Tip: \n" + tips[randomIndex];
- }
- while (!operation.isDone)
- {
- float targetProgress = Mathf.Clamp01(operation.progress / 0.9f);
- displayedProgress = Mathf.MoveTowards(
- displayedProgress,
- targetProgress,
- Time.deltaTime * 0.5f
- );
- if (progressFill != null)
- progressFill.fillAmount = displayedProgress;
- if (displayedProgress >= 1f)
- {
- continueText.gameObject.SetActive(true);
- if (Input.anyKeyDown)
- {
- operation.allowSceneActivation = true;
- }
- }
- yield return null;
- }
- }
- }
|