05AssetBundle的四种加载资源方式

时间:2022-11-02 13:58:09


05AssetBundle的四种加载资源方式


file:///C:/Program%20Files%20(x86)/unity2018/Editor/Data/Documentation/en/Manual/AssetBundles-Native.html

01:从内存中加载

05AssetBundle的四种加载资源方式

void Start()
{
StartCoroutine(LoadAssetBundles(Application.streamingAssetsPath + "/share", "share"));
StartCoroutine(LoadAssetBundles(Application.streamingAssetsPath + "/cubewall.unity3d", "cubewall"));
}


IEnumerator LoadAssetBundles(string path,string name)
{
AssetBundleCreateRequest request=AssetBundle.LoadFromFileAsync(path);
yield return request;
AssetBundle assetBundle=request.assetBundle;
GameObject item=assetBundle.LoadAsset<GameObject>(name);
if (item!=null)
{
GameObject go = GameObject.Instantiate(item);
go.name = "cubewall";
}

}

05AssetBundle的四种加载资源方式


02:从本地加载

05AssetBundle的四种加载资源方式

03从服务器加载

05AssetBundle的四种加载资源方式


本地加载

void Start()
{
string path= "file:" + Application.streamingAssetsPath + "/cubewall.unity3d";
StartCoroutine(WWWAssetBundles(path, "cubewall"));
IEnumerator WWWAssetBundles(string path,string name)
}
{
//是否准备好
while (Caching.ready==false)
{
yield return null;
}
//从本地加载
//WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:AssetBundleProject\AssetBundleProject\AssetBundles", 1);
//从服务器加载
//WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cubewall.unity3d", 1);
WWW www=WWW.LoadFromCacheOrDownload(path, 1);
yield return www;
//是否报错
if (string.IsNullOrEmpty(www.error)==false)
{
Debug.Log(www.error);
yield break; //停止协程
}

AssetBundle assetBundle=www.assetBundle;
GameObject item=Instantiate(assetBundle.LoadAsset<GameObject>(name));
item.name = "item";
}

服务器加载

string path= "http://localhost/StreamingAssets/cubewall.unity3d";
StartCoroutine(WWWAssetBundles(path, "cubewall"));

04:web加载

05AssetBundle的四种加载资源方式