赞
踩
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- enum Phase3 { Start, Middle, End }
- public class loading : MonoBehaviour
- {
- static AsyncOperation async = null;
- public Text text;
- public Image Loading;
- [Tooltip("下个场景的名字,在Build Settings发布设置中需要先将该Scene加载进来才能识别!")]
- public string Scene_name;
- public Image float_tips;
- public float start_positon;
- public float end_positon;
- private Phase3 _phase = Phase3.Start;
- private float _progressValue = 0;
- private float START_LIMITER = 0.1f;
- private float _endStart = 0;
- private float _endValue = 0;
- private float MIDDLE_LIMITER = 1.0f;
- private float _middleValue = 0;
- //void Awake()
- // {
- // Screen.SetResolution(1920,1080,true);
- // }
- void Start ()
- {
- StartCoroutine(LoadScene());
- }
- // Update is called once per frame
- void Update ()
- {
- if (text && Loading && async != null)
- {
- if (this._phase == Phase3.Start)
- {
- // 正常
- }
- else if (this._phase == Phase3.Middle)
- {
- // 1秒加1个单位
- this.middle(Time.deltaTime);
- }
- else
- {
- // 1秒内从当前位置跳到结束
- this.end(Time.deltaTime);
- }
- text.text = ( (int)( _progressValue * 100 ) ).ToString() + "%";
- Loading.fillAmount = _progressValue;
- float_tips.GetComponent<RectTransform>().localPosition = new Vector3(start_positon + end_positon * _progressValue, float_tips.GetComponent<RectTransform>().localPosition.y, 0);
- //temp = Mathf.Lerp(temp, async.progress, Time.deltaTime);
- //text.text = ( (int)( temp * 100 * 10 / 9 ) ).ToString() + "%";
- //Loading.fillAmount = temp * 10 / 9;
- if (this._progressValue >= 1)
- {
- async.allowSceneActivation = true;
- }
- //Debug.Log(timer);
- //if (temp / 9 * 10 >= 0.95)
- //{
- // text.text = 100.ToString() + "%";
- // Loading.fillAmount = 1;
- // asyn.allowSceneActivation = true;
- //}
- }
- }
- private void middle (float delta_time)
- {
- this._middleValue += delta_time;
- if (this._middleValue >= MIDDLE_LIMITER)
- {
- this._middleValue -= MIDDLE_LIMITER;
- if (this._progressValue < 0.99f)
- {
- this._progressValue += 0.01f;
- }
- }
- }
- private void end (float delta_time)
- {
- this._endValue += delta_time;
- this._progressValue = Mathf.Lerp(this._endStart, 1, this._endValue);
- }
- IEnumerator LoadScene ()
- {
- //异步读取场景。
- async = SceneManager.LoadSceneAsync(Scene_name);
- async.allowSceneActivation = false;
- while (async.progress < 0.9f)
- {
- Debug.Log(async.progress);
- //Debug.Log(async.progress + "----" + (async.progress <= 0.9f));
- if (async.progress <= START_LIMITER)
- {
- this._progressValue = async.progress;
- }
- else
- {
- this._phase = Phase3.Middle;
- }
- yield return null;
- }
- this._phase = Phase3.End;
- this._endStart = this._progressValue;
- //while (_progressValue < 1)
- //{
- // yield return new WaitForEndOfFrame();
- //}
- //Debug.LogWarning(_progressValue);
- yield return async;
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。