资源更新

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

public class Resource_Update : MonoBehaviour
{
    private Thread m_thread;

    private List<string> dn_list_file = new List<string>();
    private List<string> dn_list_url = new List<string>();
    private List<string> dn_list_path = new List<string>();
    private List<long> dn_list_size = new List<long>();
    
    private int download_index;
    private bool download_state;
    private long download_value;
    private long download_progress;
    private string download_url;
    private string download_path;

    private void Start()
    {
        StartCoroutine(DownloadFile());
    }

    #region Ready
    /// <summary>
    /// 从服务器下载所有资源
    /// </summary>
    /// <returns></returns>
    IEnumerator DownloadFile()
    {
        WWW www = new WWW(Application.streamingAssetsPath + "/File.txt");

        yield return www;

        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError(www.error);
        }
        else if (www.isDone)
        {
            if (!string.IsNullOrEmpty(www.text))
            {
                string[] files = www.text.Split('\n');
                for (int i = 0; i < files.Length; i++)
                {
                    dn_list_file.Add(files[i].TrimEnd('\r'));
                }
                Compare();
            }
        }
    }

    /// <summary>
    /// 服务器资源与本地资源进行比较
    /// </summary>
    private void Compare()
    {
        dn_list_url.Clear();
        dn_list_path.Clear();

        for (int i = 0; i < dn_list_file.Count; i++)
        {
            if (!string.IsNullOrEmpty(dn_list_file[i]))
            {
                string[] line = dn_list_file[i].Split('|');
                string[] path = line[0].Split('/');
                string local_file = Application.persistentDataPath + "/Res/" + path[path.Length - 1];

                if (File.Exists(local_file))
                {
                    if (Utils_MD5.MD5_File(local_file) != line[1])
                    {
                        dn_list_url.Add(line[0]);
                        dn_list_path.Add(local_file);
                    }
                }
                else
                {
                    string dir = Path.GetDirectoryName(local_file);
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                    dn_list_url.Add(line[0]);
                    dn_list_path.Add(local_file);
                }
            }
        }

        if (dn_list_url.Count > 0)
        {
            download_value = GetFileSize();

            StartUp();
        }
        else
        {
            Debug.Log("资源已是最新!");
        }
    }

    /// <summary>
    /// 获取更新资源大小
    /// </summary>
    /// <returns>资源大小</returns>
    private long GetFileSize()
    {
        long size_all = 0;

        for (int i = 0; i < dn_list_url.Count; i++)
        {
            long szie = GetFileSize(dn_list_url[i]);
            dn_list_size.Add(szie);
            size_all += szie;
        }

        return size_all;
    }

    private long GetFileSize(string url)
    {
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "HEAD";
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

        return response.ContentLength;
    }
    #endregion

    #region Download
    private void StartUp()
    {
        download_index = 0;

        Next();

        m_thread = new Thread(DownLoad);
        m_thread.Start();
    }

    private void DownLoad()
    {
        while (true)
        {
            if (download_state)
            {
                try
                {
                    using (WebClient cilent = new WebClient())
                    {
                        cilent.DownloadProgressChanged += Progress;
                        cilent.DownloadFileAsync(new Uri(download_url), download_path);
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError(e.Message);
                }
                download_state = false;
            }
            Thread.Sleep(1);
        }
    }

    private void Progress(object sender, DownloadProgressChangedEventArgs e)
    {
        EventMessageArgs arg = new EventMessageArgs();
        arg.AddOrReplaceMessage("progress", download_progress + e.BytesReceived);
        arg.AddOrReplaceMessage("endvalue", download_value);
        EventManager.PostEvent(EventKey.Download_Progress, arg);

        if (e.ProgressPercentage == 100)
        {
            if (dn_list_url.Count > download_index)
            {
                Next();
            }
            else
            {
                Finish();
            }
        }
    }

    private void Next()
    {
        download_url = dn_list_url[download_index];
        download_path = dn_list_path[download_index];

        if (download_index > 0)
            download_progress += dn_list_size[download_index - 1];
        else
            download_progress = 0;

        download_index++;

        download_state = true;
    }

    private void Finish()
    {
        if (m_thread != null)
        {
            m_thread.Abort();
            m_thread = null;
        }
    }
    #endregion

    private void OnApplicationQuit()
    {
        if (m_thread != null)
        {
            m_thread.Abort();
            m_thread = null;
        }
    }

    private void OnDestroy()
    {
        if (m_thread != null)
        {
            m_thread.Abort();
            m_thread = null;
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/Joke-crazy/p/10478544.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值