BuildAssemblieEditor.cs 6.38 KB
// using System;
// using System.Collections.Generic;
// using System.IO;
// using System.Linq;
// using System.Threading;
// using UnityEngine;
// using UnityEditor;
// using UnityEditor.Compilation;
// using Random = UnityEngine.Random;
//
// namespace ZhaoluGame
// {
//     public static class BuildAssemblieEditor
//     {
//         [MenuItem("Tools/Build/EnableAutoBuildCodeDebug _F1")]
//         public static void SetAutoBuildCode()
//         {
//             PlayerPrefs.SetInt("AutoBuild", 1);
//             EditorNotificationHelper.ShowNotification("AutoBuildCode Enabled");
//         }
//
//         [MenuItem("Tools/Build/DisableAutoBuildCodeDebug _F2")]
//         public static void CancelAutoBuildCode()
//         {
//             PlayerPrefs.DeleteKey("AutoBuild");
//             EditorNotificationHelper.ShowNotification("AutoBuildCode Disabled");
//         }
//
//         [MenuItem("Tools/Build/BuildCodeRelease _F6")]
//         public static void BuildCodeRelease()
//         {
//             BuildAssemblieEditor.BuildMuteAssembly("HotfixCode", new[]
//             {
//                 "HotfixCode/",
//             }, Array.Empty<string>(), CodeOptimization.Release);
//
//             AfterCompiling();
//
//             AssetDatabase.Refresh();
//         }
//
//         private static void BuildMuteAssembly(string assemblyName, string[] CodeDirectorys,
//             string[] additionalReferences, CodeOptimization codeOptimization)
//         {
//             List<string> scripts = new List<string>();
//             for (int i = 0; i < CodeDirectorys.Length; i++)
//             {
//                 DirectoryInfo dti = new DirectoryInfo(CodeDirectorys[i]);
//                 FileInfo[] fileInfos = dti.GetFiles("*.cs", System.IO.SearchOption.AllDirectories);
//                 for (int j = 0; j < fileInfos.Length; j++)
//                 {
//                     scripts.Add(fileInfos[j].FullName);
//                 }
//             }
//
//             string dllPath = Path.Combine(Define.BuildOutputDir, $"{assemblyName}.dll");
//             string pdbPath = Path.Combine(Define.BuildOutputDir, $"{assemblyName}.pdb");
//             File.Delete(dllPath);
//             File.Delete(pdbPath);
//
//             Directory.CreateDirectory(Define.BuildOutputDir);
//
//             AssemblyBuilder assemblyBuilder = new AssemblyBuilder(dllPath, scripts.ToArray());
//
//             //启用UnSafe
//             //assemblyBuilder.compilerOptions.AllowUnsafeCode = true;
//
//             BuildTargetGroup buildTargetGroup =
//                 BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
//
//             assemblyBuilder.compilerOptions.CodeOptimization = codeOptimization;
//             assemblyBuilder.compilerOptions.ApiCompatibilityLevel =
//                 PlayerSettings.GetApiCompatibilityLevel(buildTargetGroup);
//             // assemblyBuilder.compilerOptions.ApiCompatibilityLevel = ApiCompatibilityLevel.NET_4_6;
//
//             assemblyBuilder.additionalReferences = additionalReferences;
//
//             assemblyBuilder.flags = AssemblyBuilderFlags.None;
//             //AssemblyBuilderFlags.None                 正常发布
//             //AssemblyBuilderFlags.DevelopmentBuild     开发模式打包
//             //AssemblyBuilderFlags.EditorAssembly       编辑器状态
//             assemblyBuilder.referencesOptions = ReferencesOptions.UseEngineModules;
//
//             assemblyBuilder.buildTarget = EditorUserBuildSettings.activeBuildTarget;
//
//             assemblyBuilder.buildTargetGroup = buildTargetGroup;
//
//             assemblyBuilder.buildStarted += delegate(string assemblyPath)
//             {
//                 Debug.LogFormat("build start:" + assemblyPath);
//             };
//
//             assemblyBuilder.buildFinished += delegate(string assemblyPath, CompilerMessage[] compilerMessages)
//             {
//                 int errorCount = compilerMessages.Count(m => m.type == CompilerMessageType.Error);
//                 int warningCount = compilerMessages.Count(m => m.type == CompilerMessageType.Warning);
//
//                 Debug.LogFormat("Warnings: {0} - Errors: {1}", warningCount, errorCount);
//
//                 if (warningCount > 0)
//                 {
//                     Debug.LogFormat("有{0}个Warning!!!", warningCount);
//                 }
//
//                 if (errorCount > 0)
//                 {
//                     for (int i = 0; i < compilerMessages.Length; i++)
//                     {
//                         if (compilerMessages[i].type == CompilerMessageType.Error)
//                         {
//                             Debug.LogError(compilerMessages[i].message);
//                         }
//                     }
//                 }
//             };
//
//             //开始构建
//             if (!assemblyBuilder.Build())
//             {
//                 Debug.LogErrorFormat("build fail:" + assemblyBuilder.assemblyPath);
//                 return;
//             }
//         }
//
//         private static void AfterCompiling()
//         {
//             while (EditorApplication.isCompiling)
//             {
//                 Debug.Log("Compiling wait1");
//                 // 主线程sleep并不影响编译线程
//                 Thread.Sleep(1000);
//                 Debug.Log("Compiling wait2");
//             }
//
//             Debug.Log("Compiling finish");
//
//             Directory.CreateDirectory(Define.HotfixCodeDir);
//             File.Copy(Path.Combine(Define.BuildOutputDir, "HotfixCode.dll"),
//                 Path.Combine(Define.HotfixCodeDir, "HotfixCode.dll.bytes"), true);
//             File.Copy(Path.Combine(Define.BuildOutputDir, "HotfixCode.pdb"),
//                 Path.Combine(Define.HotfixCodeDir, "HotfixCode.pdb.bytes"), true);
//
//             // File.Copy(Path.Combine(Define.BuildOutputDir, "HotfixCode.dll"), Path.Combine(Define.HotfixCodeEditorDir, "HotfixCode.dll"), true);
//             // File.Copy(Path.Combine(Define.BuildOutputDir, "HotfixCode.pdb"), Path.Combine(Define.HotfixCodeEditorDir, "HotfixCode.pdb"), true);
//
//             AssetDatabase.Refresh();
//             Debug.Log("copy Code.dll to Bundles/Code success!");
//
//             Debug.Log("build success!");
//             //反射获取当前Game视图,提示编译完成
//             EditorNotificationHelper.ShowNotification("Build Code Success");
//         }
//     }
// }