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)); } /// /// 服务端加载ab场景包 /// /// /// 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]); } } /// /// 本地加载ab包 /// /// /// 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]); } } }