string path = @"AssetBundles/scene/cubewall.ab";
string cacheDownloadPath = @"file://D:\UnityWorkSpace\FifthMonthWork_Groups\AssetBundle_Demo\AssetBundles\scene\cubewall.ab";
string tempWebAddress = @"http://localhost/AssetBundles/scene/wall.ab";
//第一種加載AB的方式,異步進行加載從二進制文件中
IEnumerator LoadAB()
{
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
yield return request;
AssetBundle ab = request.assetBundle;
GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
}
//第二種進行資源加載,同步加載
Instantiate(AssetBundle.LoadFromMemory(File.ReadAllBytes(path)).LoadAsset<GameObject>("CubeWall"));
//第三種加載AB的方式,LoadFromFile【從文件進行加載】
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/scene/wall.ab");
GameObject wallPrefab = ab.LoadAsset<GameObject>("Wall");
Instantiate(wallPrefab);
//第四種加載AB的方式,LoadFromFileAsync【從文件進行異步加載】
IEnumerator LoadFileAsync()
{
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
yield return request;
AssetBundle ab = request.assetBundle;
Instantiate(ab.LoadAsset<GameObject>("CubeWall"));
}
//第五種加載AB的方式,WWW.LoadFromCacheOrDownload - 從緩存中加載和下載
IEnumerator LoadCacheOrDownloadFromFile(string path)
{
while (Caching.ready == false)
yield return null;
WWW www = WWW.LoadFromCacheOrDownload(path, 1);
yield return www;
if( !string.IsNullOrEmpty(www.error) )
{
Debug.Log(www.error);
yield break;
}
AssetBundle ab = www.assetBundle;
GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
}
//第六種加載AB的方式,UnityWebRequest,網頁請求進行加載【第一種方法】
IEnumerator UseUnityWebRequest(string path)
{
//1、使用UnityWebRequest.GetAssetBundle(路徑)【服務器 / 本地都可以】 去獲取到網頁請求
UnityWebRequest request = UnityWebRequest.GetAssetBundle(path);
//2、等待這個請求進行發送完
yield return request.SendWebRequest();
//3、發送完請求之后,就要從DownloadHandlerAssetBundle進行獲取一個request,得到出來的是一個AssetBundle類對象
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//4、用獲取到的AssetBundle對象去加載資源泛型是GameObject
GameObject obj = ab.LoadAsset<GameObject>("Wall");
//5、實例化出這個GameObject對象
Instantiate(obj);
}
//第六種加載AB的方式,UnityWebRequest,網頁請求進行加載【第二種方法】
IEnumerator UseUnityWebRequest2(string path)
{
UnityWebRequest request = UnityWebRequest.GetAssetBundle(path);
yield return request.SendWebRequest();
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
GameObject obj = ab.LoadAsset<GameObject>("Wall");
Instantiate(obj);
}
讀取Manifest文件,獲取它們的依賴關系並且加載出來
AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] strArr = manifest.GetAllDependencies("scene/cubewall.ab");
foreach (var item in strArr)
{
AssetBundle.LoadFromFile("AssetBundles/" + item);
}
【待繼續往后寫】