從服務(wù)器加載資源:
AB資源打包后有一個【目錄文件】AssetBundle,他保存了所有AB資源的路徑與名稱,文章來源:http://www.zghlxwxcb.cn/news/detail-540417.html
通過aLLAssetBundleURL鏈接路徑 組拼 從【目錄文件】獲得的AB資源的名字,然后再協(xié)程方法內(nèi)編寫相關(guān)代碼,從而實現(xiàn)從服務(wù)器加載資源的功能。詳細見代碼。文章來源地址http://www.zghlxwxcb.cn/news/detail-540417.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
public class DownLoadAssetBundle : MonoBehaviour
{
//AB資源文件保存在服務(wù)器中的位置(我的服務(wù)器寄了,加載不到了)
private string mainAssetBundleURL = @"http://120.24.90.173/Luademo/AssetBundles/AssetBundles";
private string aLLAssetBundleURL = @"http://120.24.90.173/Luademo/AssetBundles/";
void Start()
{
StartCoroutine("DownLoadMainAssetBundle");
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
LoadAssetBundle();
}
}
/// <summary>
/// 下載主[目錄]AssetBundle文件
/// </summary>
IEnumerator DownLoadMainAssetBundle()
{
//創(chuàng)建一個獲取 AssetBundle 文件的 web 請求.
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(mainAssetBundleURL);
//發(fā)送這個 web 請求.
yield return request.SendWebRequest();
//從 web 請求中獲取內(nèi)容,會返回一個 AssetBundle 類型的數(shù)據(jù).
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
if (ab == null)
{
Debug.Log("not ab");
}
//從這個“目錄文件 AssetBundle”中獲取 manifest 數(shù)據(jù).
AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//獲取這個 manifest 文件中所有的 AssetBundle 的名稱信息.
string[] names = manifest.GetAllAssetBundles();
for (int i = 0; i < names.Length; i++)
{
//組拼出下載的路徑鏈接.
Debug.Log(aLLAssetBundleURL + names[i]);
//下載單個AssetBundle文件加載到場景中.
//StartCoroutine(DownLoadSingleAssetBundle(aLLAssetBundleURL + names[i]));
//下載AssetBundle并保存到本地.
StartCoroutine(DownLoadAssetBundleAndSave(aLLAssetBundleURL + names[i]));
}
}
/// <summary>
/// 下載單個AssetBundle文件加載到場景中
/// </summary>
IEnumerator DownLoadSingleAssetBundle(string url)
{
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
yield return request.SendWebRequest();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//通過獲取到的 AssetBundle 對象獲取內(nèi)部所有的資源的名稱(路徑),返回一個數(shù)組.
string[] names = ab.GetAllAssetNames();
for (int i = 0; i < names.Length; i++)
{
//Debug.Log(names[i]);
//截取路徑地址中的文件名,且無后綴名. 需要引入 System.IO 命名空間.
string tempName = Path.GetFileNameWithoutExtension(names[i]);
Debug.Log(tempName);
//實例化.
GameObject obj = ab.LoadAsset<GameObject>(tempName);
GameObject.Instantiate<GameObject>(obj);
}
}
/// <summary>
/// 下載AssetBundle并保存到本地
/// </summary>
IEnumerator DownLoadAssetBundleAndSave(string url)
{
//UnityWebRequestAssetBundle.GetAssetBundle(string uri)使用這個API下載回來的資源它是不支持原始數(shù)據(jù)訪問的.
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
//表示下載狀態(tài)是否完畢.
if (request.isDone)
{
//使用 IO 技術(shù)把這個 request 對象存儲到本地.(需要后綴)
//SaveAssetBundle(Path.GetFileName(url), request.downloadHandler.data, request.downloadHandler.data.Length);
SaveAssetBundle2(Path.GetFileName(url), request);
}
}
/// <summary>
/// 方法1
/// 存儲AssetBundle為本地文件
/// </summary>
private void SaveAssetBundle(string fileName, byte[] bytes, int count)
{
//創(chuàng)建一個文件信息對象.
FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + "http://" + fileName);
//通過文件信息對象的“創(chuàng)建”方法,得到一個文件流對象.
FileStream fs = fileInfo.Create();
//通過文件流對象,往這個文件內(nèi)寫入信息.
//fs.Write(字節(jié)數(shù)組, 開始位置, 數(shù)據(jù)長度);
fs.Write(bytes, 0, count);
//文件寫入存儲到硬盤,關(guān)閉文件流對象,銷毀文件對象.
fs.Flush();
fs.Close();
fs.Dispose();
Debug.Log(fileName + "下載完畢");
}
/// <summary>
/// 方法2
/// 存儲AssetBundle為本地文件
/// </summary>
private void SaveAssetBundle2(string fileName, UnityWebRequest request)
{
//構(gòu)造文件流.
FileStream fs = File.Create(Application.streamingAssetsPath + "http://" + fileName);
//將字節(jié)流寫入文件里,request.downloadHandler.data可以獲取到下載資源的字節(jié)流.
fs.Write(request.downloadHandler.data, 0, request.downloadHandler.data.Length);
//文件寫入存儲到硬盤,關(guān)閉文件流對象,銷毀文件對象.
fs.Flush();
fs.Close();
fs.Dispose();
Debug.Log(fileName + "下載完畢");
}
/// <summary>
/// 從本地加載 AB 資源并實例化
/// </summary>
private void LoadAssetBundle()
{
//從本地加載 AB 資源到內(nèi)存.
AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/player1.ab");
//從 AB 資源中獲取資源.
GameObject player = assetBundle.LoadAsset<GameObject>("Necromancer");
GameObject.Instantiate<GameObject>(player, Vector3.zero, Quaternion.identity);
}
}
到了這里,關(guān)于Unity 從服務(wù)器加載AssetBundle資源寫入本地內(nèi)存,并將下載保存的AB資源從本地內(nèi)存加載至場景的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!