BuildAssemblieEditor.cs
6.38 KB
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
// 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");
// }
// }
// }