using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
public class LoadAB : MonoBehaviour {
// Use this for initialization
void Start () {
// LoadFile();
StartCoroutine(LoadWeb());
}
IEnumerator LoadWeb()
{
string uri = "file:///" + Application.dataPath + "/StreamingAssets/mainpanel";
UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
yield return request.Send();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
GameObject go = ab.LoadAsset<GameObject>("22");//22:预设体的名称
GameObject g = Instantiate(go);
g.transform.SetParent(GameObject.Find("Canvas").transform, false);
}
// Update is called once per frame
void LoadFile()
{
//加载本地文件
AssetBundle ab = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "mainpanel"));//mainpanel包的名字
GameObject go = ab.LoadAsset<GameObject>("22");//22:预设体的名称
GameObject g = Instantiate(go);
g.transform.SetParent(GameObject.Find("Canvas").transform, false);
}
}
1.AssetBundle.LoadFromFile:从本地加载,速度最快。
如果是未压缩或者LZ4压缩的,从本地直接读取。
如果是LZMA压缩的,先解压缩包,然后加载到内存中。
2.UnityWebRequest的DownloadHandlerAssetBundle
|