1. 程式人生 > >[Unity]AssetBundle資源更新以及多執行緒下載

[Unity]AssetBundle資源更新以及多執行緒下載

這裡寫圖片描述

前言

此文章適合不太瞭解資源載入的萌新,有了入門基礎之後再去github上搜大牛寫的專業的資源載入方案才能得心應手,不然的話會看的很吃力或者說一臉懵逼。Unity裡面關於資源載入我們都知道是下載更新AssetBundle,關於AssetBundle我之前的文章已經詳細介紹過,沒看過的朋友可以在看一下。下面介紹的資源載入的Demo有以下幾點:
1.WWW下載圖片資源
2.HTTP下載apk檔案,並且支援斷點續傳,並且顯示載入進度條
3.HTTP多執行緒下載檔案

部分核心程式碼和講解

WWW下載

思路:

WWW是Unity給我們封裝的一個基於HTTP的簡單類庫,如果我們做很簡單的下載,或者網路請求可以用這個類庫,個人覺得這個封裝的並不是很好,所以一般商業專案開發都不會使用這個,寧可自己去封裝一個HTTP請求和下載的類庫,可控性更好。僅僅是個人觀點,不喜勿噴。

程式碼:

using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class WWWLoad
{
    private WWW www = null;
    static System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
    /// <summary>
    /// 下載檔案
    /// </summary>
    public IEnumerator DownFile
(string url, string savePath, Action<WWW> process) { FileInfo file = new FileInfo(savePath); stopWatch.Start(); UnityEngine.Debug.Log("Start:" + Time.realtimeSinceStartup); www = new WWW(url); while (!www.isDone) { yield return 0; if
(process != null) process(www); } yield return www; if (www.isDone) { byte[] bytes = www.bytes; CreatFile(savePath, bytes); } } /// <summary> /// 建立檔案 /// </summary> /// <param name="bytes"></param> public void CreatFile(string savePath, byte[] bytes) { FileStream fs = new FileStream(savePath, FileMode.Append); BinaryWriter bw = new BinaryWriter(fs); fs.Write(bytes, 0, bytes.Length); fs.Flush(); //流會緩衝,此行程式碼指示流不要緩衝資料,立即寫入到檔案。 fs.Close(); //關閉流並釋放所有資源,同時將緩衝區的沒有寫入的資料,寫入然後再關閉。 fs.Dispose(); //釋放流 www.Dispose(); stopWatch.Stop(); Debug.Log("下載完成,耗時:" + stopWatch.ElapsedMilliseconds); UnityEngine.Debug.Log("End:" + Time.realtimeSinceStartup); } }

HTTP下載並載入AB資源

思路:

主要用的核心類是HttpWebRequest,用這個類建立的物件可以申請下載的檔案的大小以及下載的進度。移動上可讀寫的目錄是PersidentDataPath,並且各個移動裝置的路徑不同,這點要注意,所以我們下載的AB資源就會下載到這個目錄。

效果圖:

這裡寫圖片描述

核心程式碼:

using UnityEngine;
using System.Collections;
using System.Threading;
using System.IO;
using System.Net;
using System;

/// <summary>
/// 通過http下載資源
/// </summary>
public class HttpDownLoad {
    //下載進度
    public float progress{get; private set;}
    //涉及子執行緒要注意,Unity關閉的時候子執行緒不會關閉,所以要有一個標識
    private bool isStop;
    //子執行緒負責下載,否則會阻塞主執行緒,Unity介面會卡主
    private Thread thread;
    //表示下載是否完成
    public bool isDone{get; private set;}
    const int ReadWriteTimeOut = 2 * 1000;//超時等待時間
    const int TimeOutWait = 5 * 1000;//超時等待時間


    /// <summary>
    /// 下載方法(斷點續傳)
    /// </summary>
    /// <param name="url">URL下載地址</param>
    /// <param name="savePath">Save path儲存路徑</param>
    /// <param name="callBack">Call back回撥函式</param>
    public void DownLoad(string url, string savePath,string fileName, Action callBack, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal)
    {
        isStop = false;
        System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
        //開啟子執行緒下載,使用匿名方法
        thread = new Thread(delegate() {
            stopWatch.Start();
            //判斷儲存路徑是否存在
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            //這是要下載的檔名,比如從伺服器下載a.zip到D盤,儲存的檔名是test
            string filePath = savePath + "/"+ fileName;

            //使用流操作檔案
            FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            //獲取檔案現在的長度
            long fileLength = fs.Length;
            //獲取下載檔案的總長度
            UnityEngine.Debug.Log(url+" "+fileName);
            long totalLength = GetLength(url);
            Debug.LogFormat("<color=red>檔案:{0} 已下載{1}M,剩餘{2}M</color>",fileName,fileLength/1024/1024,(totalLength- fileLength)/ 1024/1024);         

            //如果沒下載完
            if(fileLength < totalLength)
            {

                //斷點續傳核心,設定本地檔案流的起始位置
                fs.Seek(fileLength, SeekOrigin.Begin);

                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;

                request.ReadWriteTimeout = ReadWriteTimeOut;
                request.Timeout = TimeOutWait;

                //斷點續傳核心,設定遠端訪問檔案流的起始位置
                request.AddRange((int)fileLength);

                Stream  stream = request.GetResponse().GetResponseStream();
                byte[] buffer = new byte[1024];
                //使用流讀取內容到buffer中
                //注意方法返回值代表讀取的實際長度,並不是buffer有多大,stream就會讀進去多少
                int length = stream.Read(buffer, 0, buffer.Length);
                //Debug.LogFormat("<color=red>length:{0}</color>" + length);
                while (length > 0)
                {
                    //如果Unity客戶端關閉,停止下載
                    if(isStop) break;
                    //將內容再寫入本地檔案中
                    fs.Write(buffer, 0, length);
                    //計算進度
                    fileLength += length;
                    progress = (float)fileLength / (float)totalLength;
                    //UnityEngine.Debug.Log(progress);
                    //類似尾遞迴
                    length = stream.Read(buffer, 0, buffer.Length);

                }
                stream.Close();
                stream.Dispose();

            }
            else
            {
                progress = 1;
            }
            stopWatch.Stop();
            Debug.Log("耗時: " + stopWatch.ElapsedMilliseconds);
            fs.Close();
            fs.Dispose();
            //如果下載完畢,執行回撥
            if(progress == 1)
            {
                isDone = true;
                if (callBack != null) callBack();
                thread.Abort();
            }
            UnityEngine.Debug.Log ("download finished");    
        });
        //開啟子執行緒
        thread.IsBackground = true;
        thread.Priority = threadPriority;
        thread.Start();
    }


    /// <summary>
    /// 獲取下載檔案的大小
    /// </summary>
    /// <returns>The length.</returns>
    /// <param name="url">URL.</param>
    long GetLength(string url)
    {
        UnityEngine.Debug.Log(url);

        HttpWebRequest requet = HttpWebRequest.Create(url) as HttpWebRequest;
        requet.Method = "HEAD";
        HttpWebResponse response = requet.GetResponse() as HttpWebResponse;
        return response.ContentLength;
    }

    public void Close()
    {
        isStop = true;
    }

}

多執行緒下載檔案

思路:

多執行緒下載思路是計算一個檔案包大小,然後建立幾個執行緒,計算每一個執行緒下載的始末下載的位置,最後是合併成一個整體的檔案包寫入到本地。

效果圖:

這裡寫圖片描述

核心程式碼:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEngine;
using System.Threading;

public class MultiHttpDownLoad : MonoBehaviour
{
    string savePath = string.Empty;
    string FileName = "ClickEffect.apk";
    //string resourceURL = @"http://www.aladdingame.online/wuzhang/Resources/ClickEffect.apk";// @"http://www.dingxiaowei.cn/birdlogo.png";
    string resourceURL = @"http://www.dingxiaowei.cn/ClickEffect.apk";
    string saveFile = string.Empty;
    public int ThreadNum { get; set; }
    public bool[] ThreadStatus { get; set; }
    public string[] FileNames { get; set; }
    public int[] StartPos { get; set; }
    public int[] FileSize { get; set; }
    public string Url { get; set; }
    public bool IsMerge { get; set; }
    private int buffSize = 1024;
    DateTime beginTime;

    void Start()
    {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        savePath = Application.streamingAssetsPath;
#elif UNITY_ANDROID
          savePath = Application.persistentDataPath;;
#endif
        saveFile = Path.Combine(savePath, FileName);

        DownDoad();
    }

    void Init(long fileSize)
    {
        if (ThreadNum == 0)
            ThreadNum = 5;

        ThreadStatus = new bool[ThreadNum];
        FileNames = new string[ThreadNum];
        StartPos = new int[ThreadNum];//下載位元組起始點
        FileSize = new int[ThreadNum];//該程序檔案大小
        int fileThread = (int)fileSize / ThreadNum;//單程序檔案大小
        int fileThreade = fileThread + (int)fileSize % ThreadNum;//最後一個程序的資源大小
        for (int i = 0; i < ThreadNum; i++)
        {
            ThreadStatus[i] = false;
            FileNames[i] = i.ToString() + ".dat";
            if (i < ThreadNum - 1)
            {
                StartPos[i] = fileThread * i;
                FileSize[i] = fileThread;
            }
            else
            {
                StartPos[i] = fileThread * i;
                FileSize[i] = fileThreade;
            }
        }
    }

    void DownDoad()
    {
        UnityEngine.Debug.Log("開始下載 時間:" + System.DateTime.Now.ToString());
        beginTime = System.DateTime.Now;
        Url = resourceURL;
        long fileSizeAll = 0;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        fileSizeAll = request.GetResponse().ContentLength;
        Init(fileSizeAll);

        System.Threading.Thread[] threads = new System.Threading.Thread[ThreadNum];
        HttpMultiThreadDownload[] httpDownloads = new HttpMultiThreadDownload[ThreadNum];
        for (int i = 0; i < ThreadNum; i++)
        {
            httpDownloads[i] = new HttpMultiThreadDownload(request, this, i);
            threads[i] = new System.Threading.Thread(new System.Threading.ThreadStart(httpDownloads[i].Receive));
            threads[i].Name = string.Format("執行緒{0}:", i);
            threads[i].Start();
        }
        StartCoroutine(MergeFile());
    }

    IEnumerator MergeFile()
    {
        while (true)
        {
            IsMerge = true;
            for (int i = 0; i < ThreadNum; i++)
            {
                if (ThreadStatus[i] == false)
                {
                    IsMerge = false;
                    yield return 0;
                    System.Threading.Thread.Sleep(100);
                    break;
                }
            }
            if (IsMerge)
                break;
        }

        int bufferSize = 512;
        string downFileNamePath = saveFile;
        byte[] bytes = new byte[bufferSize];
        FileStream fs = new FileStream(downFileNamePath, FileMode.Create);
        FileStream fsTemp = null;

        for (int i = 0; i < ThreadNum; i++)
        {
            fsTemp = new FileStream(FileNames[i], FileMode.Open);
            while (true)
            {
                yield return 0;
                buffSize = fsTemp.Read(bytes, 0, bufferSize);
                if (buffSize > 0)
                    fs.Write(bytes, 0, buffSize);
                else
                    break;
            }
            fsTemp.Close();
        }
        fs.Close();
        Debug.Log("接受完畢!!!結束時間:" + System.DateTime.Now.ToString());
        Debug.LogError("下載耗時:" + (System.DateTime.Now - beginTime).TotalSeconds.ToString());
        yield return null;
        DeleteCacheFiles();
    }

    private void DeleteCacheFiles()
    {
        for (int i = 0; i < ThreadNum; i++)
        {
            FileInfo info = new FileInfo(FileNames[i]);
            Debug.LogFormat("Delete File {0} OK!", FileNames[i]);
            info.Delete();
        }
    }
}

public class HttpMultiThreadDownload
{
    private int threadId;
    private string url;
    MultiHttpDownLoad downLoadObj;
    private const int buffSize = 1024;
    HttpWebRequest request;

    public HttpMultiThreadDownload(HttpWebRequest request, MultiHttpDownLoad downLoadObj, int threadId)
    {
        this.request = request;
        this.threadId = threadId;
        this.url = downLoadObj.Url;
        this.downLoadObj = downLoadObj;
    }

    public void Receive()
    {
        string fileName = downLoadObj.FileNames[threadId];
        var buffer = new byte[buffSize];
        int readSize = 0;
        FileStream fs = new FileStream(fileName, System.IO.FileMode.Create);
        Stream ns = null;

        try
        {
            request.AddRange(downLoadObj.StartPos[threadId], downLoadObj.StartPos[threadId] + downLoadObj.FileSize[threadId]);
            ns = request.GetResponse().GetResponseStream();
            readSize = ns.Read(buffer, 0, buffSize);
            showLog("執行緒[" + threadId.ToString() + "] 正在接收 " + readSize);
            while (readSize > 0)
            {
                fs.Write(buffer, 0, readSize);
                readSize = ns.Read(buffer, 0, buffSize);
                showLog("執行緒[" + threadId.ToString() + "] 正在接收 " + readSize);
            }
            fs.Close();
            ns.Close();
        }
        catch (Exception er)
        {
            Debug.LogError(er.Message);
            fs.Close();
        }
        showLog("執行緒[" + threadId.ToString() + "] 結束!");
        downLoadObj.ThreadStatus[threadId] = true;
    }

    private void showLog(string processing)
    {
        Debug.Log(processing);
    }
}

執行緒下載速度跟執行緒的關係呈鐘罩式關係,也就是說適量的執行緒數量會提高下載速度,但並不是說執行緒數越多就越好,因為執行緒的切換和資源的整合也是需要時間的。下面就列舉下載單個檔案,建立的執行緒數和對應的下載時間:

  • 單執行緒
    這裡寫圖片描述
  • 5個執行緒
    這裡寫圖片描述
  • 15個執行緒
    這裡寫圖片描述

這裡我是1M的頻寬,下載的是一個300KB左右的資源,一般不會做多執行緒下載單一資源,多執行緒下載一般用於下載多個資源,除非單一資源真的很大才有多執行緒下載,然後做合包操作。

Demo下載

開發交流

1群
QQ群
unity3d unity 遊戲開發

1群如果已經滿員,請加2群
159875734

後續計劃

寫一個實際商業專案中用到的資源更新案例。