我的Unity学习笔记之——Unity中从网站下载ab资源+下载存储一条龙

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");
        string[] names = manifest.GetAllAssetBundles();
        for (int i = 0; i < names.Length; i++)
        {
            Debug.Log(allAssetBundleURL + names[i]);
            StartCoroutine(DownLoadSingleAssetBundel(allAssetBundleURL + names[i]));
        }
    }
    /// 
    /// 下载单个AB文件
    /// 
    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(tempName);
            GameObject.Instantiate(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");
        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]));
        }
    }
    /// 
    /// 下载单个AB文件不保存
    /// 
    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(tempName);
            GameObject.Instantiate(gameObject);

        }


    }
    /// 
    /// 下载AB文件并保存到本地
    /// 
    /// 
    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);
            
        }
    }
    /// 
    /// 存储AB文件到本地
    /// 
    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 + "下载并存储完成");
    }

}

 

你可能感兴趣的:(我的Unity学习笔记之——Unity中从网站下载ab资源+下载存储一条龙)