You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.4 KiB
85 lines
2.4 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
namespace EduCoderTool
|
|
{
|
|
/*
|
|
* @func AB资源加载
|
|
* @author lz
|
|
*/
|
|
public class DownloadRes : MonoBehaviour
|
|
{
|
|
|
|
[Header("加载进度")]
|
|
public Text txt_percent;
|
|
|
|
[Header("场景主包地址")]
|
|
public string mainABUrl;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
//string filepath = "file://D:/UnityProjects/ZQCourse/ZQCourse/AssetBundles/WebGL/main";
|
|
//string filepath = "http://localhost/Missile/AssetBundles/WebGL/main";
|
|
StartCoroutine(LoadScene(mainABUrl));
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 服务端加载ab场景包
|
|
/// </summary>
|
|
/// <param name="fileUrl"></param>
|
|
/// <returns></returns>
|
|
private IEnumerator LoadScene(string fileUrl)
|
|
{
|
|
using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(fileUrl))
|
|
{
|
|
uwr.SendWebRequest();
|
|
if (uwr.isNetworkError || uwr.isHttpError)
|
|
Debug.Log(uwr.error);
|
|
|
|
while (!uwr.isDone)
|
|
{
|
|
float percent = Mathf.Floor(uwr.downloadProgress * 100);
|
|
Debug.Log(percent);
|
|
txt_percent.text = "正在加载:"+percent + "%";
|
|
yield return 1;
|
|
}
|
|
|
|
if (uwr.isDone)
|
|
{
|
|
txt_percent.text = "加载完成,准备进入课程";
|
|
}
|
|
|
|
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
|
|
string[] scenePath = bundle.GetAllScenePaths();
|
|
|
|
SceneManager.LoadScene(scenePath[0]);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 本地加载ab包
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
/// <returns></returns>
|
|
private IEnumerator LoadLocalScene(string filePath)
|
|
{
|
|
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(filePath);
|
|
yield return request;
|
|
|
|
AssetBundle ab = request.assetBundle;
|
|
|
|
string[] scenePath = ab.GetAllScenePaths();
|
|
SceneManager.LoadScene(scenePath[0]);
|
|
}
|
|
|
|
}
|
|
|
|
}
|