1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| using System.IO; using System.Net; using System.Security.Cryptography; using System.Text; 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://192.168.1.101";
[MenuItem("AB包工具/打开工具窗口")] private static void OpenWindow() { ABTools window = EditorWindow.GetWindowWithRect<ABTools>(new Rect(0, 0, 350, 220)); window.Show(); }
private void OnGUI() { GUI.Label(new Rect(10, 10, 150, 15), "平台选择"); nowSelIndex = GUI.Toolbar(new Rect(10, 30, 330, 20), nowSelIndex, targetStrings); GUI.Label(new Rect(10, 60, 150, 15), "资源服务器地址"); serverIP = GUI.TextField(new Rect(10, 80, 330, 20), serverIP); if (GUI.Button(new Rect(10, 110, 100, 40), "创建对比文件")) CreateABCompareFile(); if (GUI.Button(new Rect(115, 110, 225, 40), "保存默认资源到StreamingAssets")) MoveABToStreamingAssets(); if (GUI.Button(new Rect(10, 160, 330, 40), "上传AB包和对比文件")) UploadABFile(); }
private void UploadABFile() { DirectoryInfo directory = new DirectoryInfo(Application.dataPath + $"/ArtRes/AB/{targetStrings[nowSelIndex]}/"); FileInfo[] fileInfos = directory.GetFiles();
foreach (FileInfo info in fileInfos) { if (info.Extension == "" || info.Extension == ".txt") { FtpUploadFile(info.FullName, info.Name); } } }
private async void FtpUploadFile(string filePath, string fileName) { await Task.Run(() => { try { FtpWebRequest request = FtpWebRequest.Create(new System.Uri($"{serverIP}/AB/{targetStrings[nowSelIndex]}/" + fileName)) as FtpWebRequest; NetworkCredential credential = new NetworkCredential("MrTang", "MrTang123"); request.Credentials = credential; request.Proxy = null; request.KeepAlive = false; request.Method = WebRequestMethods.Ftp.UploadFile; request.UseBinary = true; Stream upLoadStream = request.GetRequestStream(); using (FileStream file = File.OpenRead(filePath)) { byte[] bytes = new byte[2048]; int contentLength = file.Read(bytes, 0, bytes.Length); while (contentLength > 0) { upLoadStream.Write(bytes, 0, contentLength); contentLength = file.Read(bytes, 0, bytes.Length); } file.Close(); upLoadStream.Close(); } Debug.Log($"{fileName}上传成功"); } catch (System.Exception e) { Debug.Log($"{fileName}上传失败:{e.Message}"); } }); }
private void MoveABToStreamingAssets() { Object[] selectedAsset = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets); if (selectedAsset.Length == 0) return; string abCompareInfo = ""; foreach (Object asset in selectedAsset) { string assetPath = AssetDatabase.GetAssetPath(asset); string fileName = assetPath.Substring(assetPath.LastIndexOf('/')); if (fileName.IndexOf('.') != -1) continue; AssetDatabase.CopyAsset(assetPath, "Assets/StreamingAssets" + fileName); FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + fileName); abCompareInfo += fileInfo.Name + " " + fileInfo.Length + " " + CreateABCompare.GetMD5(fileInfo.FullName); abCompareInfo += "|"; } abCompareInfo = abCompareInfo.Substring(0, abCompareInfo.Length - 1); File.WriteAllText(Application.streamingAssetsPath + "/ABCompareInfo.txt", abCompareInfo); AssetDatabase.Refresh(); }
private void CreateABCompareFile() { DirectoryInfo directory = new DirectoryInfo(Application.dataPath + $"/ArtRes/AB/{targetStrings[nowSelIndex]}"); FileInfo[] fileInfos = directory.GetFiles(); string abCompareInfo = ""; foreach (FileInfo info in fileInfos) { if (info.Extension == "") { abCompareInfo += info.Name + " " + info.Length + " " + GetMD5(info.FullName); abCompareInfo += "|"; } } abCompareInfo = abCompareInfo.Substring(0, abCompareInfo.Length - 1); File.WriteAllText(Application.dataPath + $"/ArtRes/AB/{targetStrings[nowSelIndex]}/ABCompareInfo.txt", abCompareInfo); Debug.Log("AB包对比文件生成完毕!"); AssetDatabase.Refresh(); }
private string GetMD5(string filePath) { using (FileStream file = new FileStream(filePath, FileMode.Open)) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] md5Info = md5.ComputeHash(file); StringBuilder sb = new StringBuilder(); file.Close(); for (int i = 0; i < md5Info.Length; i++) sb.Append(md5Info[i].ToString("x2")); return sb.ToString(); } } }
|