using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class TestAB : MonoBehaviour { // Start is called before the first frame update void Start() { StartCoroutine(DoLoadAssetBundle()); } private IEnumerator DoLoadAssetBundle() { string url = Application.streamingAssetsPath + "/mrvideo"; Debug.Log(url); using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url)) { yield return request.SendWebRequest(); if (request.isHttpError || request.isNetworkError) { // 下载出错 print(request.error); } else { // 下载完成 AssetBundle assetBundle = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle; GameObject obj = assetBundle.LoadAsset(assetBundle.GetAllAssetNames()[0]); GameObject obj2 = GameObject.Instantiate(obj); obj2.AddComponent(); obj2.transform.parent = transform; obj2.transform.localPosition = Vector3.zero; // 优先释放request 会降低内存峰值 // request.Dispose(); } } } }