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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
| using System; using System.Collections; using System.Collections.Generic; 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://127.0.0.1";
[MenuItem("AB包工具/打开工具窗口")] private static void OpenWindow() { ABTools windown = EditorWindow.GetWindowWithRect(typeof(ABTools), new Rect(0, 0, 350, 220)) 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); 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, 100, 40), "创建对比文件")) CreateABCompareFile(); if (GUI.Button(new Rect(115, 110, 225, 40), "保存默认资源到StreamingAssets")) MoveABToStreamingAssets(); if (GUI.Button(new Rect(10, 160, 330, 40), "上传AB包和对比文件")) UploadAllABFile(); }
private void CreateABCompareFile() { DirectoryInfo directory = Directory.CreateDirectory(Application.dataPath + "/ArtRes/AB/" + targetStrings[nowSelIndex]); FileInfo[] fileInfos = directory.GetFiles();
if (fileInfos.Length == 0) { Debug.Log(directory.FullName+" 没有文件需要生成对比文件"); return; }
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); AssetDatabase.Refresh();
Debug.Log("AB包对比文件生成成功"); } private string GetMD5(string filePath) { using (FileStream file = new FileStream(filePath, FileMode.Open)) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] md5Info = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder(); for (int i = 0; i < md5Info.Length; i++) sb.Append(md5Info[i].ToString("x2"));
return sb.ToString(); } }
private void MoveABToStreamingAssets() { UnityEngine.Object[] selectedAsset = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets); if (selectedAsset.Length == 0) return; string abCompareInfo = ""; foreach (UnityEngine.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 UploadAllABFile() { DirectoryInfo directory = Directory.CreateDirectory(Application.dataPath + "/ArtRes/AB/" + targetStrings[nowSelIndex] + "/"); FileInfo[] fileInfos = directory.GetFiles();
if (fileInfos.Length == 0) { Debug.Log(directory.FullName+" 没有文件需要上传"); return; }
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 req = FtpWebRequest.Create(new Uri( serverIP + "/AB/" + targetStrings[nowSelIndex] + "/" + fileName)) as FtpWebRequest; req.Credentials = new NetworkCredential("chenlf", "123456"); req.Proxy = null; req.KeepAlive = false; req.Method = WebRequestMethods.Ftp.UploadFile; req.UseBinary = true; Stream upLoadStream = req.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 (Exception ex) { Debug.Log(fileName + "上传失败" + ex.Message); } });
} }
|