Unity AB包编辑器

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

public class ABTools : EditorWindow
{
    private int nowSelIndex = 0;
    private string[] targetStrings = new string[] { "PC", "IOS", "Android" };
    private string serverIP = "ftp://127.0.0.1";
    [MenuItem("AB包工具/打开工具窗口")]
    private static void OpenWindow()
    {
        //获取一个ABTool编辑器窗口对象
        ABTools windown = EditorWindow.GetWindowWithRect(typeof(ABTools), new Rect(0, 0, 320, 190)) as ABTools;
        windown.Show();
    }
    private void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 150, 15), "平台选择");
        //页签显示 平台选择
        nowSelIndex = GUI.Toolbar(new Rect(10, 30, 250, 20), nowSelIndex, targetStrings);
        //资源服务器IP地址
        GUI.Label(new Rect(10, 60, 150, 15), "资源服务器地址");
        serverIP = GUI.TextField(new Rect(10, 80, 150, 20), serverIP);
        //创建对比文件按钮
        if (GUI.Button(new Rect(10, 110, 90, 30), "创建对比文件"))
        {
            CreateABFile();
        }
        //保存默认资源到StreamingAssets按钮
        if (GUI.Button(new Rect(110, 110, 200, 30), "保存选中资源到StreamingAssets"))
        {
            MoveABToStreamingAssets();
        }
        //上传AB包和对比文件按钮
        if (GUI.Button(new Rect(10, 150, 300, 30), "上传AB包和对比文件按钮"))
        {
            UploadAllABFile();
        }
    }
    /// <summary>
    /// 生成AB包对比文件
    /// </summary>
    private void CreateABFile()
    {
        //获取文件夹信息
        DirectoryInfo directory = Directory.CreateDirectory(Application.dataPath + "/AB/" + targetStrings[nowSelIndex]);
        //获取该目录下所有文件信息
        FileInfo[] fileInfos = directory.GetFiles();
        //用于存储信息的字符串
        string abCompaerInfo = "";
        foreach (FileInfo info in fileInfos)
        {
            if (info.Extension == "")//文件后缀,没有后缀为AB包
            {
                //拼接一个AB包信息
                abCompaerInfo += info.Name + " " + info.Length + " " + Get_md5(info.FullName);
                abCompaerInfo += "|";//分割
            }
        }
        //去除最后一个“|”
        abCompaerInfo = abCompaerInfo.Substring(0, abCompaerInfo.Length - 1);
        //存储拼接好的AB包资源信息
        File.WriteAllText(Application.dataPath + "/AB/" + targetStrings[nowSelIndex] + "/ABCompareInfo.txt", abCompaerInfo);
        AssetDatabase.Refresh();//刷新
        Debug.Log("AB包对比文件生成成功");
    }
    /// <summary>
    /// 根据AB包生成MD5码
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    private string Get_md5(string filePath)
    {
        //将文件以流的形式打开
        using (FileStream file = new FileStream(filePath, FileMode.Open))
        {
            //声名一个MD5对象 用于生成MD5码
            MD5 md5 = new MD5CryptoServiceProvider();
            //利用API得到数据的MD5码 16个字节数组
            byte[] md5Info = md5.ComputeHash(file);
            //关闭文件流
            file.Close();
            //把16个字节转化为16进制 减小MD5码长度
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < md5Info.Length; i++)
            {
                sb.Append(md5Info[i].ToString("x2"));
            }
            return sb.ToString();
        }
    }
    /// <summary>
    /// 保存默认资源到StreamingAssets
    /// </summary>
    private void MoveABToStreamingAssets()
    {
        //获取Project窗口中选中的资源
        Object[] selectAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        if (selectAssets.Length == 0)
            return;
        //用于拼接本地默认AB包资源信息字符串
        string abCompareInfo = "";
        //遍历选中的资源对象
        foreach (Object asset in selectAssets)
        {
            //获取资源路径
            string assetPath = AssetDatabase.GetAssetPath(asset);
            //截取字符串最后一个斜杆后内容作为文件名(包含斜杆,不包含加1)
            string fileName = assetPath.Substring(assetPath.LastIndexOf('/'));
            //排除有后缀文件
            if (fileName.IndexOf('.') != -1)
                continue;
            //复制文件到StreamingAssets         
            AssetDatabase.CopyAsset(assetPath, "Assets/StreamingAssets" + fileName);
            //获取拷贝文件信息
            FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + fileName);
            //拼接字符串
            abCompareInfo += fileInfo.Name + " " + fileInfo.Length + " " + Get_md5(fileInfo.FullName);
            abCompareInfo += "|";
        }
        abCompareInfo = abCompareInfo.Substring(0, abCompareInfo.Length - 1);
        //将本地默认资源对此文件存入
        File.WriteAllText(Application.streamingAssetsPath + "/ABCompareInfo.txt", abCompareInfo);
        Debug.Log("保存选中默认资源到StreamingAssets");
        //刷新窗口
        AssetDatabase.Refresh();
    }
    /// <summary>
    /// 上传AB包和对比文件
    /// </summary>
    private void UploadAllABFile()
    {
        //获取文件夹信息
        DirectoryInfo directory = Directory.CreateDirectory(Application.dataPath + "/AB/" + targetStrings[nowSelIndex] + "/");
        //获取该目录下所有文件信息
        FileInfo[] fileInfos = directory.GetFiles();
        foreach (FileInfo info in fileInfos)
        {
            //文件后缀,没有后缀为AB包,对比文件为TXT(或ABCompareInfo)
            if (info.Extension == "" || info.Extension == ".txt")
            {
                //上传该文件
                FtpUpload(info.FullName, info.Name);
            }
        }
    }
    //异步函数async await任务类
    private async void FtpUpload(string filePath, string flieName)
    {
        //多线程
        await Task.Run(() =>
        {
            try
            {
                //创建Ftp链接上传
                FtpWebRequest req = FtpWebRequest.Create(new System.Uri(serverIP + "/AB/" + targetStrings[nowSelIndex] + "/" + flieName)) as FtpWebRequest;
                //设置通信凭证上传
                NetworkCredential n = new NetworkCredential("Box", "jj13579");//账号密码
                req.Credentials = n;
                //设置代理为null,请求完毕后关闭控制连接,操作命令-上传,指定传输类型-2进制
                req.Proxy = null;
                req.KeepAlive = false;
                req.Method = WebRequestMethods.Ftp.UploadFile;
                req.UseBinary = true;
                //上传文件 创建流对象
                Stream upLoadStream = req.GetRequestStream();
                //读取文件信息,写入流对象
                using (FileStream file = File.OpenRead(filePath))
                {
                    //2kb上传
                    byte[] bytes = new byte[2048];
                    int contentLength = file.Read(bytes, 0, bytes.Length);//读到内容长度
                                                                          //循环上传文件数据
                    while (contentLength != 0)
                    {
                        //写入到上传流中
                        upLoadStream.Write(bytes, 0, contentLength);
                        //写完再读,为0为止
                        contentLength = file.Read(bytes, 0, bytes.Length);
                    }
                    //循环完毕 上传结束
                    file.Close();
                    upLoadStream.Close();
                }
                Debug.Log(flieName + "文件上传成功");
            }
            catch (System.Exception ex)
            {
                Debug.Log(flieName + "上传失败:" + ex);
            }
        });
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上桑AGO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值