Unity ResUpdater研究使用1

本文档介绍了如何使用Unity ResUpdater,包括从git获取的项目地址、基本使用流程及其实现步骤。根据作者的描述,主要涉及实现ResUpdater接口、创建Mono测试以及生成资源的.md5文件进行验证。目前,文件生成类和.version文件的处理仍在研究中。
摘要由CSDN通过智能技术生成

git地址:https://github.com/stallboy/unityresupdater
基本流程作者在git上都写了,但是写的有点简洁也没有个使用的例子然后自己根据作者的描述画了一下流程图:
这里写图片描述
使用:
1.需要实现一下ResUpdater这个接口
2.创建一个Mono测试
3.编写res.md5生成器来生成.md5文件来验证资源

//Reporter接口实现类
using System;
using System.Collections.Generic;
using System.Diagnostics;
using ResUpdater;
using Debug = UnityEngine.Debug;

namespace Assets
{
    public class ReporterImpl:Reporter 
    {
        public void DownloadLatestVersionErr(Exception err)
        {
            Debug.Log($"err :{err}");
            /*
                $"{}"需要unity2017版本,5.x,4.x不支持
                还需要在unity2017中把Scripting Backend修改为.Net4.6
            */
        }

        public void ReadVersionErr(Loc loc, string wwwErr, Exception parseErr)
        {
            Debug.Log($" wwwErr{wwwErr}, parseErr{parseErr}");
        }

        public void CheckVersionDone(State nextState, int localVersion, int latestVersion)
        {
            Debug.Log($"nextState :{nextState}, localVersion{localVersion}, latestVersion{latestVersion}");
        }

        public void DownloadLatestMd5Err(Exception err)
        {
            Debug.Log($"err :{err}");
        }

        public void ReadMd5Err(Loc loc, string wwwwErr, Exception parseErr)
        {
            Debug.Log($"loc :{loc}, wwwErr{wwwwErr}, parseErr{parseErr}");
        }

        public void CheckMd5Done(State nextState, Dictionary<string, CheckMd5State.Info> downloadList)
        {
            Debug.Log($" nextStatenextState{nextState}, downloadList{downloadList}");
        }

        public void DownloadOneResComplete(Exception err, string fn, CheckMd5State.Info info)
        {
            Debug.Log($"err :{err}, fn{fn}, info{info}");
        }

        public void DownloadResDone(State nextState, int errCount)
        {
           Debug.Log($"nextState :{nextState}, errCount{errCount}");
        }
    }
}
//测试类

using System.Collections;
using Assets;
using ResUpdater;
using UnityEngine;

public class Test : MonoBehaviour
{
    public string[] HostStrings;
    public int te;
    private Reporter _reporter = new ReporterImpl();


    // Use this for initialization
    void Start()
    {
        var re = new ResUpdater.ResUpdater(HostStrings, te, _reporter, cor);
        re.Start();
    }

    private Coroutine cor(IEnumerator routine)
    {
       Debug.Log("test");
        return null;
    }
    // Update is called once per frame
    void Update () {

    }
}

上面2个有了也就可以测试了,但是还缺少了一个文件生成类.

//文件生成类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using ResUpdater;
using UnityEditor;
using UnityEngine;


namespace Res
{
    class ResCreate : EditorWindow
    {
        [MenuItem("生成/Res.md5")]
        ///生成res.md5文件
        static void Create()
        {
            isCreate = false;
            ShowWindow();
        }
#region window窗口
        public static void ShowWindow()
        {
            EditorWindow.GetWindow(typeof(ResCreate));
        }
        public static void CloseWindow()
        {
            EditorWindow.GetWindow(typeof(ResCreate)).Close();
        }
#endregion


        static bool isCreate = false;
#region GUI绘制
        void OnGUI()
        {

            if (GUI.Button(new Rect(new Vector2(2, 10), new Vector2(70, 20)), "选择需要生成的文件夹"))
            {
                _folderPath = EditorUtility.OpenFolderPanel("选择一个根目录", null, null);
            }
            GUI.Label(new Rect(new Vector2(75, 10), new Vector2(600, 30)), _folderPath);
            GUI.Label(new Rect(new Vector2(2, 30), new Vector2(100, 30)), "输入根目录:");
            _root = GUI.TextArea(new Rect(new Vector2(70, 30), new Vector2(100, 30)), _root);
            if (GUI.Button(new Rect(new Vector2(20, 100), new Vector2(70, 20)), "生成"))
            {
                CreateStart();
                Save();
            }
            if (isCreate)
            {
                GUI.Label(new Rect(new Vector2(2, 130), new Vector2(150, 30)), "生成成功,路径在控制台");
            }
        }
#endregion
#region 执行开始
        void CreateStart()
        {
            _resSb.Remove(0,_resSb.Length);
            GetFileInfo();
            ListFiles(_fileInfo);
            _resSb.Remove(_resSb.Length - 1, 1);
            Debug.Log(_resSb.ToString());
        }
#endregion
        string _root = "Asses";
        string _folderPath;
        DirectoryInfo _fileInfo;
        void GetFileInfo()
        {
            _fileInfo = new DirectoryInfo(_folderPath);
        }

        StringBuilder _resSb = new StringBuilder();
#region 遍历所选文件夹内的所有文件
        void ListFiles(FileSystemInfo info)
        {
            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录 
            if (dir == null) return;
            FileSystemInfo[] files = dir.GetFileSystemInfos();
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;
                //是文件 
                if (file != null)
                {
                    Debug.Log($"文件名:{file.FullName} 文件大小:{file.Length},文件路径:{GetFile(file.ToString())}");
                    MD5Calculate(file);
                }
                else
                    //对于子目录,进行递归调用 
                    ListFiles(files[i]);
            }
        } 
#endregion

#region 文件信息生成
        void MD5Calculate(FileInfo file)
        {
            _resSb.Append(GetFile(file.ToString())+" ");
            _resSb.Append(Util.getMD5(file.ToString())+" ");
            _resSb.Append(file.Length);
            _resSb.Append("\n");
        }
#endregion
#region 路径切割
        string GetFile(string path)
        {
            if (path.Contains(_root))
            {
    //                Debug.Log(path.Length);
                path = path.Substring(
                    path.IndexOf(_root, StringComparison.Ordinal) + _root.Length+1, 
                    path.Length - (path.IndexOf(_root, StringComparison.Ordinal) + _root.Length+1)
                    );
    //                Debug.Log (path+""+ _root.Length);
            }
            return path;
        }
#endregion
#region 保存文件
        void Save()
        {
            string path;
            try
            {
                path = EditorUtility.SaveFilePanel("保存生成的文件",null, "res","md5");
                Debug.Log($"保存路径:{path}");
                Md5Write(path);
            }
            catch (System.ArgumentException e)
            {
                Debug.Log("取消保存:" + e);
            }
            catch (System.Exception e) {
                Debug.Log("错误:" + e);
            }
        }
        void Md5Write(string jsonPath)
        {
            StreamWriter st = new StreamWriter(jsonPath);
            string json = _resSb.ToString();
            st.Write(json);
            st.Close();
            isCreate = true;
        }
#endregion
    }    
}
//Util类
public static string getMD5(string filePath)
        {
            try
            {
                FileStream file = new FileStream(filePath, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                int len = (int)file.Length;
                byte[] data = new byte[len];
                file.Read(data, 0, len);
                file.Close();
                byte[] result = md5.ComputeHash(data);  
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < result.Length; i++)
                {
                    sb.Append(Convert.ToString(result[i],16));
                }
                return sb.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

然后貌似还有一个.version文件没弄,还在研究

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值