using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadABFiles : MonoBehaviour
{
private Slider slider;
public Text textName;
public Text textProgress;
bool isProgressing = false;
UnityWebRequest pro;
int abID = 0;
string filepath;
string[] dirs;
private void Awake()
{
filepath = System.Environment.CurrentDirectory + "/panoramaABs/";
dirs = System.IO.Directory.GetFileSystemEntries(filepath);
}
private void Start()
{
slider = transform.Find("Slider").GetComponent<Slider>();
StartCoroutine(LoadingAssetBundle());
StartCoroutine(ChangeText());
StartCoroutine(UpdateProgress());
}
private IEnumerator LoadingAssetBundle()
{
for (int i = 0; i < MainManager.instance.bundles.Count; i++)
{
if (i < dirs.Length)
{
string parh = filepath + i + "/" + i + ".unity3d";
using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(parh))
{
isProgressing = true;
pro = uwr;
abID = i;
yield return uwr.SendWebRequest();
if (!uwr.isHttpError)
{
MainManager.instance.bundles[i] = DownloadHandlerAssetBundle.GetContent(uwr);
isProgressing = false;
}
yield return new WaitForFixedUpdate();
}
}
}
yield return new WaitForSeconds(0.1f);
Debug.Log("资源加载完毕");
}
#region
IEnumerator UpdateProgress()
{
while (true)
{
if (isProgressing)
{
if (pro.downloadProgress < 0) yield break;
}
textProgress.text = ((int)(GetProgress * 10000)) / 100f + "%";
slider.value = (((int)(GetProgress * 10000)) / 10000f);
yield return 0;
}
}
public float GetProgress
{
get
{
if (isProgressing)
return pro.downloadProgress;
else
return 1;
}
}
#endregion
#region 开启文本动画
int textAnim = 0;
IEnumerator ChangeText()
{
while (true)
{
if (textAnim == 0 && dirs.Length > 0)
{
textName.text = "资源[" + (abID + 1) + "/" + dirs.Length + "]加载中";
yield return new WaitForSeconds(0.2f);
textName.text = "资源[" + (abID + 1) + "/" + dirs.Length + "]加载中.";
yield return new WaitForSeconds(0.2f);
textName.text = "资源[" + (abID + 1) + "/" + dirs.Length + "]加载中..";
yield return new WaitForSeconds(0.2f);
textName.text = "资源[" + (abID + 1) + "/" + dirs.Length + "]加载中...";
yield return new WaitForSeconds(0.2f);
}
}
}
#endregion
}
|