using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using System.IO; public class DownLoadAssetBundle : MonoBehaviour { private string mainAssetBundleURL = @"http://www.XXX.com/AssetBundles/AssetBundles"; private string allAssetBundleURL = @"http://www.XXX.com/AssetBundles/"; void Start () { StartCoroutine("DownLoadMainAssetBundel"); } IEnumerator DownLoadMainAssetBundel() { UnityWebRequest request = UnityWebRequest.GetAssetBundle(mainAssetBundleURL); yield return request.Send(); AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); //Debug.Log("OK"); AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); string[] names = manifest.GetAllAssetBundles(); for (int i = 0; i < names.Length; i++) { Debug.Log(allAssetBundleURL + names[i]); StartCoroutine(DownLoadSingleAssetBundel(allAssetBundleURL + names[i])); } } /// <summary> /// 下載單個AB文件 /// </summary> IEnumerator DownLoadSingleAssetBundel(string url) { UnityWebRequest request = UnityWebRequest.GetAssetBundle(url); yield return request.Send(); AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); //Debug.Log("OK"); string[] names = ab.GetAllAssetNames(); for (int i = 0; i < names.Length; i++) { string tempName = Path.GetFileNameWithoutExtension(names[i]); //Debug.Log(tempName); GameObject gameObject = ab.LoadAsset<GameObject>(tempName); GameObject.Instantiate<GameObject>(gameObject); } } }
下面是下載存儲一條龍
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using System.IO; public class DownLoadAssetBundle : MonoBehaviour { private string mainAssetBundleURL = @"http://www.XXX.com/AssetBundles/AssetBundles"; private string allAssetBundleURL = @"http://www.XXX.com/AssetBundles/"; void Start () { StartCoroutine("DownLoadMainAssetBundel"); } IEnumerator DownLoadMainAssetBundel() { UnityWebRequest request = UnityWebRequest.GetAssetBundle(mainAssetBundleURL); yield return request.Send(); AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); //Debug.Log("OK"); AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); string[] names = manifest.GetAllAssetBundles(); for (int i = 0; i < names.Length; i++) { Debug.Log(allAssetBundleURL + names[i]); //StartCoroutine(DownLoadSingleAssetBundel(allAssetBundleURL + names[i])); StartCoroutine(DownLoadAssetBundelAbdSave(allAssetBundleURL + names[i])); } } /// <summary> /// 下載單個AB文件不保存 /// </summary> IEnumerator DownLoadSingleAssetBundel(string url) { UnityWebRequest request = UnityWebRequest.GetAssetBundle(url); yield return request.Send(); AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); //Debug.Log("OK"); string[] names = ab.GetAllAssetNames(); for (int i = 0; i < names.Length; i++) { string tempName = Path.GetFileNameWithoutExtension(names[i]); //Debug.Log(tempName); GameObject gameObject = ab.LoadAsset<GameObject>(tempName); GameObject.Instantiate<GameObject>(gameObject); } } /// <summary> /// 下載AB文件並保存到本地 /// </summary> /// <returns></returns> IEnumerator DownLoadAssetBundelAbdSave(string url) { WWW www = new WWW(url); yield return www; if(www.isDone) { //表示資源下載完畢使用IO技術把www對象存儲到本地 SaveAssetBundle(Path.GetFileName(url), www.bytes, www.bytes.Length); } } /// <summary> /// 存儲AB文件到本地 /// </summary> private void SaveAssetBundle(string fileName, byte[] bytes, int count) { FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + "//" + fileName); FileStream fs = fileInfo.Create(); fs.Write(bytes, 0, count); fs.Flush(); fs.Close(); fs.Dispose(); Debug.Log(fileName + "下載並存儲完成"); } }
