BundleBuilderZ.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using Pack;
  9. public class BundleBuilderZ : EditorWindow
  10. {
  11. class MD5
  12. {
  13. public string Md5;
  14. public long Length;
  15. public MD5(string m, long length)
  16. {
  17. Md5 = m;
  18. Length = length;
  19. }
  20. }
  21. protected static BuildTarget target = BuildTarget.Android;
  22. static string StreamingAssetsPath = "Assets/StreamingAssets";
  23. static Dictionary<string, MD5> DicFileMD5 = null;
  24. static string VersionInfo = null;
  25. [MenuItem("AssetBundle/MD5Creator")]
  26. public static void MD5Creator()
  27. {
  28. DicFileMD5 = new Dictionary<string, MD5>();
  29. string[] files = Directory.GetFiles(FileSystem.PackagePath(), "*", SearchOption.AllDirectories);
  30. MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();
  31. int i = 0;
  32. StringBuilder sb = new StringBuilder(1024);
  33. foreach (string filePath in files)
  34. {
  35. if (filePath.Contains(".svn") || filePath.Contains(".meta") || filePath.Contains("DS_Store"))
  36. continue;
  37. EditorUtility.DisplayProgressBar(string.Format("生成MD5码-{0}", Path.GetFileName(filePath)), Path.GetFileName(filePath), (float)(++i) / files.Length);
  38. FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  39. byte[] hash = md5Generator.ComputeHash(file);
  40. sb.Clear();
  41. for (int j = 0; j < hash.Length; ++j)
  42. {
  43. sb.Append(hash[j].ToString("X2"));
  44. }
  45. string strMD5 = sb.ToString().ToLower();
  46. string key = Path.GetFileName(filePath);
  47. if (!DicFileMD5.ContainsKey(key))
  48. DicFileMD5.Add(key, new MD5(strMD5, file.Length));
  49. else
  50. Debug.LogWarning("<Two File has the same name> name = " + filePath);
  51. file.Close();
  52. file.Dispose();
  53. }
  54. EditorUtility.ClearProgressBar();
  55. string savePath = string.Format("{0}mainfest", FileSystem.PackagePath());
  56. // 删除前一版的old数据
  57. if (File.Exists(savePath))
  58. {
  59. System.IO.File.Delete(savePath);
  60. }
  61. //PackPlatforms cfg = PackRun.GetConfigs();
  62. int size = 0;
  63. sb.Clear();
  64. if (string.IsNullOrEmpty(VersionInfo))
  65. {
  66. VersionInfo = "0.1.0.0";
  67. }
  68. //sb.Append("FileName,MD5,Length\r\n");
  69. sb.Append("ID,FullName,MD5,Size\r\n");
  70. sb.Append("int,string,string,ulong\r\n");
  71. sb.Append($"{++size},Version,{VersionInfo},0\r\n");
  72. foreach (KeyValuePair<string, MD5> pair in DicFileMD5)
  73. {
  74. if (pair.Key == "Version" || pair.Key == "mainfest")
  75. {
  76. continue;
  77. }
  78. sb.Append(++size);
  79. sb.Append(",");
  80. sb.Append(pair.Key);
  81. sb.Append(",");
  82. sb.Append(pair.Value.Md5);
  83. sb.Append(",");
  84. sb.Append(pair.Value.Length);
  85. sb.Append("\r\n");
  86. }
  87. FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate);
  88. if (fs == null)
  89. return;
  90. UTF8Encoding encoding = new UTF8Encoding(false, false);
  91. StreamWriter textWriter = new StreamWriter(fs, encoding);
  92. if (textWriter == null)
  93. return;
  94. textWriter.Write(sb.ToString());
  95. textWriter.Flush();
  96. textWriter.Close();
  97. DicFileMD5.Clear();
  98. VersionInfo = null;
  99. Debug.Log("<color=red>================ MD5Creator Complete ================</color>");
  100. }
  101. public static void CreateMD5File(string version)
  102. {
  103. VersionInfo = version;
  104. MD5Creator();
  105. FileHelper.WirteStringToFile($"{FileSystem.PackagePath()}Version",version);
  106. }
  107. #region BundleBuidlerDeleteAssetsData;
  108. static void BundleBuidlerDeleteAssetsData()
  109. {
  110. Caching.ClearCache();
  111. if (Directory.Exists(StreamingAssetsPath))
  112. {
  113. string srcPath = GetAssetbundlesPath();
  114. string path = Application.streamingAssetsPath + srcPath.Replace(Path.GetDirectoryName(Application.dataPath), "");
  115. if (Directory.Exists(path))
  116. {
  117. Directory.Delete(path, true);
  118. }
  119. //Log.E("DeleteAndCreateDir.Delete (path); {0}", StreamingAssetsPath);
  120. }
  121. AssetDatabase.Refresh();
  122. DebugHelper.Log("<color=red>================ DeleteStreamingAssets Complete ================</color>");
  123. }
  124. #endregion BundleBuidlerDeleteAssetsData
  125. #region BundleBuidlerCopyToStreamingAssets;
  126. public static void BundleBuidlerCopyToStreamingAssets()
  127. {
  128. Caching.ClearCache();
  129. BundleBuidlerDeleteAssetsData();
  130. string path = Application.dataPath;
  131. string srcPath = GetAssetbundlesPath();
  132. path = Application.streamingAssetsPath + srcPath.Replace(Path.GetDirectoryName(Application.dataPath), "");
  133. DeleteAndCreateDir(path);
  134. CopyFolder(srcPath, path);
  135. DebugHelper.Log("<color=red>================ CopyToStreamingAssets Complete ================</color>");
  136. AssetDatabase.Refresh();
  137. }
  138. #endregion BundleBuidlerCopyToStreamingAssets
  139. public static void BuildApp(bool debug = false)
  140. {
  141. target = EditorUserBuildSettings.activeBuildTarget;// BuildTarget.Android;
  142. BuildOptions buildOption = BuildOptions.None;
  143. System.DateTime time = System.DateTime.Now;
  144. string DateTime = time.Month.ToString("D2") + time.Day.ToString("D2") +
  145. "_" + time.Hour.ToString("D2") + time.Minute.ToString("D2");
  146. PlayerSettings.applicationIdentifier = AssetBundleConstant.PgName;
  147. if (EditorUserBuildSettings.development)
  148. {
  149. buildOption |= BuildOptions.Development;
  150. buildOption |= BuildOptions.AllowDebugging;
  151. buildOption |= BuildOptions.ConnectWithProfiler;
  152. }
  153. else
  154. {
  155. buildOption |= BuildOptions.None;
  156. }
  157. string locationPathName;
  158. if (BuildTarget.iOS == target)
  159. {
  160. locationPathName = Application.dataPath;
  161. locationPathName = locationPathName.Replace("Assets", "/Xcode");
  162. }
  163. else if (BuildTarget.Android == target)
  164. {
  165. string apkName = AssetBundleConstant.ApkName;
  166. if (!debug)
  167. {
  168. ChangeAndroidVersionCode();
  169. }
  170. else
  171. {
  172. apkName = apkName + "_debug";
  173. }
  174. locationPathName = Application.dataPath;
  175. locationPathName = locationPathName.Replace("Assets", "/BatunityApk/");
  176. EditorUserBuildSettings.androidBuildSubtarget = MobileTextureSubtarget.ETC;
  177. PlayerSettings.Android.keystoreName = Application.dataPath.Replace("Assets", "/Cert/wenting.keystore");
  178. PlayerSettings.Android.keystorePass = "n9P5j2b7blMm";
  179. PlayerSettings.Android.keyaliasName = "game";
  180. PlayerSettings.Android.keyaliasPass = "KlYX666Ro";
  181. locationPathName += apkName + ".apk";
  182. }
  183. else
  184. {
  185. target = BuildTarget.StandaloneWindows64;
  186. locationPathName = Application.dataPath;
  187. locationPathName = locationPathName.Replace("Assets", "/RO");
  188. locationPathName += DateTime + "/" + DateTime + ".exe";
  189. PlayerSettings.SetArchitecture(BuildTargetGroup.Standalone, 0);
  190. }
  191. var buildResult = BuildPipeline.BuildPlayer(ms_scenes, locationPathName, target, buildOption);
  192. }
  193. private static void ChangeAndroidVersionCode()
  194. {
  195. int oldVersionCode = PlayerSettings.Android.bundleVersionCode;
  196. oldVersionCode = oldVersionCode % 100;
  197. oldVersionCode += 1;
  198. int bigVersionCode = 0;
  199. var bigVersionStrs = AssetBundleConstant.GameVersion.Split('.');
  200. if (bigVersionStrs.Length >= 1)
  201. {
  202. bigVersionCode += (int.Parse(bigVersionStrs[0]) * 1000000);
  203. }
  204. if (bigVersionStrs.Length >= 2)
  205. {
  206. bigVersionCode += (int.Parse(bigVersionStrs[1]) * 10000);
  207. }
  208. if (bigVersionStrs.Length >= 3)
  209. {
  210. bigVersionCode += (int.Parse(bigVersionStrs[2]) * 100);
  211. }
  212. PlayerSettings.Android.bundleVersionCode = (bigVersionCode + oldVersionCode);
  213. PlayerSettings.bundleVersion = AssetBundleConstant.GameVersion + oldVersionCode;
  214. }
  215. private static string[] ms_scenes =
  216. {
  217. "Assets/game.unity",
  218. "Assets/relogin.unity",
  219. };
  220. #region CommonMethod;
  221. static void DeleteAndCreateDir(string targetPath)
  222. {
  223. if (Directory.Exists(targetPath))
  224. {
  225. Directory.Delete(targetPath, true);
  226. DebugHelper.LogWarning("DeleteAndCreateDir.Delete (path); {0}", targetPath);
  227. }
  228. if (!Directory.Exists(targetPath))
  229. Directory.CreateDirectory(targetPath);
  230. }
  231. static void DeleteFile(string targetPath)
  232. {
  233. if (File.Exists(targetPath))
  234. {
  235. File.Delete(targetPath);
  236. }
  237. }
  238. private static void CopyFolder(string from, string to)
  239. {
  240. string[] dirs = Directory.GetDirectories(from);
  241. FileSystem.CreateDirIfNotExist(to);
  242. foreach (string sub in dirs)
  243. {
  244. string path = to + Path.GetFileName(sub);
  245. if (!Directory.Exists(path))
  246. Directory.CreateDirectory(to + Path.GetFileName(sub));
  247. CopyFolder(sub + "\\", path + "\\");
  248. }
  249. string[] files = Directory.GetFiles(from);
  250. foreach (string file in files)
  251. File.Copy(file, to + Path.GetFileName(file), true);
  252. }
  253. public static string GetAssetbundlesPath()
  254. {
  255. string path = Application.dataPath;
  256. path = Path.GetDirectoryName(path);
  257. BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
  258. if (buildTarget == BuildTarget.iOS)
  259. {
  260. path = path + "/ios/";
  261. }
  262. else if (buildTarget == BuildTarget.Android)
  263. {
  264. path = path + "/AssetsAndroid/";
  265. }
  266. else
  267. {
  268. path = path + "/AssetsPC/";
  269. }
  270. return path;
  271. }
  272. public static string GetWwisePath()
  273. {
  274. string path = Application.dataPath;
  275. path = Path.GetDirectoryName(path);
  276. BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
  277. if (buildTarget == BuildTarget.iOS)
  278. {
  279. path = path + "/client_WwiseProject/GeneratedSoundBanks/Windows/";
  280. }
  281. else if (buildTarget == BuildTarget.Android)
  282. {
  283. path = path + "/client_WwiseProject/GeneratedSoundBanks/Windows/";
  284. }
  285. else
  286. {
  287. path = path + "/client_WwiseProject/GeneratedSoundBanks/Windows/";
  288. }
  289. return path;
  290. }
  291. #endregion CommonMethod;
  292. [MenuItem("RO_Tool/Scene/EnableScene All")]
  293. public static void EnableScene()
  294. {
  295. SetScenes(true);
  296. }
  297. [MenuItem("RO_Tool/Scene/DisableScene All")]
  298. public static void DisableScene()
  299. {
  300. SetScenes(false);
  301. }
  302. static void SetScenes(bool enable)
  303. {
  304. EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
  305. foreach (EditorBuildSettingsScene scene in scenes)
  306. {
  307. if (scene.path.Contains("game") || scene.path.Contains("Loading") || scene.path.Contains("relogin"))
  308. continue;
  309. scene.enabled = enable;
  310. }
  311. EditorBuildSettings.scenes = scenes;
  312. AssetDatabase.Refresh();
  313. }
  314. [MenuItem("[ 快捷启动 ]/[ Run Game ]", false, 1)]
  315. static void RunGame()
  316. {
  317. Time.timeScale = 1.0f;
  318. UnityEditor.SceneManagement.EditorSceneManager.OpenScene(@"Assets/game.unity");
  319. EditorApplication.isPlaying = true;
  320. }
  321. [MenuItem("RO_Tool/生成公私钥")]
  322. public static void GenKey()
  323. {
  324. string publicKey, privateKey, privateKeyPk8;
  325. RsaKeyHelper.GenKey(out publicKey, out privateKey, out privateKeyPk8);
  326. Debug.LogError(string.Format("{0}\n\n\n{1}", publicKey, privateKeyPk8));
  327. }
  328. }