1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 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());
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- private IEnumerator DoLoadAssetBundle()
- {
- string url = Application.streamingAssetsPath + "/AssetBundles/Android/saloonmain";
- 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<GameObject>(assetBundle.GetAllAssetNames()[0]);
- GameObject obj2 = GameObject.Instantiate(obj);
- // 优先释放request 会降低内存峰值
- request.Dispose();
- }
- }
- }
- }
|