123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System.Collections;
- using System.Collections.Generic;
- using Sirenix.OdinInspector;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class AsyncLoad_Single: MonoBehaviour
- {
- [Header("是否使用内置模式")]
- [SerializeField]
- private bool m_isUseBuiltinMode = true;
-
- [Header("基础熟悉设定")]
- [SerializeField]
- private Slider m_slider;
- /// <summary>
- /// 进度显示
- /// </summary>
- [SerializeField]
- private Text m_ProgressTxt;
- /// <summary>
- /// 文字显示
- /// </summary>
- [SerializeField]
- private Text m_txtDisplay;
- [SerializeField]
- private GameObject m_hintEffect;
- /// <summary>
- /// 加载速度
- /// </summary>
- [SerializeField]
- private float m_loadingSpeed = 1;
- /// <summary>
- /// 是否开始执行
- /// </summary>
- private bool m_isStart = false;
-
- /// <summary>
- /// 目标值
- /// </summary>
- private float m_targetValue;
- /// <summary>
- /// 当前进度
- /// </summary>
- private int m_currentProgress = 0;
-
- private AsyncOperation m_asyncOperation;
- public string m_SceneName;
-
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="_sceneName"></param>
- /// <param name="_isStart"></param>
- public void Init(bool _isStart)
- {
- m_slider.value = 0;
- m_currentProgress = 0;
- m_isStart = false;
-
- if (m_isUseBuiltinMode)
- {
- if (_isStart)
- {
- StartCoroutine(AsyncLoadScene(m_SceneName, _isStart));
- }
- }
- else
- {
- StartCoroutine(LoadProgress(m_SceneName));
- }
- }
- #region 内置模式
-
- IEnumerator AsyncLoadScene(string _name,bool _isStart)
- {
- m_asyncOperation = SceneManager.LoadSceneAsync(_name);
- m_asyncOperation.allowSceneActivation = false;
- m_isStart = _isStart;
- yield return m_asyncOperation;
- }
-
- void FixedUpdate()
- {
- if (m_isStart)
- {
- Debug.Log("进入");
- m_targetValue = m_asyncOperation.progress;
- if (m_asyncOperation.progress >= 0.9f)
- {
- m_targetValue = 1.0f;
- }
- if (m_targetValue != m_slider.value)
- {
- m_slider.value = Mathf.Lerp(m_slider.value, m_targetValue, Time.deltaTime * m_loadingSpeed);
- if (Mathf.Abs(m_slider.value - m_targetValue) < 0.01f)
- {
- m_slider.value = m_targetValue;
- }
- }
- m_ProgressTxt.text = ((int)(m_slider.value * 100)).ToString() + "%";
- if ((int)(m_slider.value * 100) == 100)
- {
- m_isStart = false;
- m_asyncOperation.allowSceneActivation = true;
- }
- }
- }
- #endregion
- /// <summary>
- /// 平滑加载
- /// </summary>
- /// <param name="_name"></param>
- private IEnumerator LoadProgress(string _name)
- {
- while (m_slider.value < 1f)
- {
- m_targetValue += m_loadingSpeed * Time.deltaTime;
- m_ProgressTxt.text = (int)(m_targetValue * 100) + "%";
- m_slider.value = Mathf.Clamp01(m_targetValue);
-
- yield return null;
- }
- if (m_slider.value == 1)
- {
- SetSubsequentOperations();
- SceneManager.LoadSceneAsync(_name);
- }
- }
- /// <summary>
- /// 设置模型状态
- /// </summary>
- private void SetSubsequentOperations()
- {
- m_txtDisplay.text = "资源加载完成";
- m_hintEffect.SetActive(false);
- }
- }
|