项目开始使用IL2CPP编译,后果是可执行文件急剧增加。
google后发现国外一大神写的方法,原帖在这http://forum.unity3d.com/threads/suggestion-for-reducing-the-size-of-il2cpp-generated-executable.338986/
懒得愿意看原帖的直接看我的方法吧
1、新建一个Mono工程,命名成UnusedByteCodeStripper2,贴如下面代码,编译出 UnusedByteCodeStripper2.exe
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;
using
System.Collections.Generic;
using
System.Diagnostics;
using
System.IO;
using
System.Linq;
using
System.Reflection;
using
System.Text;
using
Mono.Cecil;
using
Mono.Collections.Generic;
namespace
RemoveAttributesTool
{
internal
class
Program
{
private
static
readonly
string
[] RemoveAttributesNames =
{
// Just information
"System.Runtime.CompilerServices.CompilerGeneratedAttribute"
,
"System.Runtime.CompilerServices.ExtensionAttribute"
,
"System.ParamArrayAttribute"
,
"System.Reflection.DefaultMemberAttribute"
,
"System.Diagnostics.DebuggerStepThroughAttribute"
,
"System.Diagnostics.DebuggerHiddenAttribute"
,
"System.Diagnostics.DebuggerDisplayAttribute"
,
"System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"
,
"System.ObsoleteAttribute"
,
"System.AttributeUsageAttribute"
,
"System.MonoTODOAttribute"
,
// Not relative
"System.CLSCompliantAttribute"
,
"System.Runtime.InteropServices.ComVisibleAttribute"
,
"System.Runtime.ConstrainedExecution.ReliabilityContractAttribute"
,
// Editor only
"UnityEngine.AddComponentMenu"
,
"UnityEditor.MenuItem"
,
"UnityEngine.ContextMenu"
,
"UnityEngine.ExecuteInEditMode"
,
"UnityEngine.HideInInspector"
,
"UnityEngine.TooltipAttribute"
,
"UnityEngine.DisallowMultipleComponent"
,
"UnityEngine.Internal.ExcludeFromDocsAttribute"
,
};
private
static
readonly
string
[] AdditionalDllFileNames =
{
"UnityEngine.dll"
,
"mscorlib.dll"
,
"System.dll"
,
"System.Core.dll"
,
"System.Xml.dll"
,
"Mono.Security.dll"
,
};
private
static
void
Main(
string
[] args)
{
// Process
for
(
var
i = 0; i < args.Length; i++)
{
switch
(args[i])
{
case
"-a"
:
ProcessDll(args[i + 1]);
break
;
}
}
foreach
(
var
fileName
in
AdditionalDllFileNames)
{
if
(File.Exists(fileName))
ProcessDll(fileName);
}
// Run original executables
var
monoCfgDir = Environment.GetEnvironmentVariable(
"MONO_CFG_DIR"
);
var
monoPath = monoCfgDir.Substring(0, monoCfgDir.Length - 3) +
"bin/mono"
;
var
currentModulePath = Assembly.GetExecutingAssembly().Location;
var
orgModulePath = currentModulePath.Substring(0, currentModulePath.Length - 3) +
"org.exe"
;
var
orgArgs =
'"'
+ orgModulePath +
'"'
+
' '
+
string
.Join(
" "
, args.Select(a =>
'"'
+ a +
'"'
));
var
handle = Process.Start(monoPath, orgArgs);
handle.WaitForExit();
}
private
static
void
ProcessDll(
string
dllPath)
{
AssemblyDefinition assemblyDef;
using
(
var
assemblyStream =
new
MemoryStream(File.ReadAllBytes(dllPath)))
{
assemblyDef = AssemblyDefinition.ReadAssembly(assemblyStream);
}
ProcessAssembly(
new
[] {assemblyDef});
using
(
var
assemblyStream = File.Create(dllPath))
{
assemblyDef.Write(assemblyStream);
}
}
private
static
void
ProcessAssembly(AssemblyDefinition[] assemblyDefs)
{
foreach
(
var
assemblyDef
in
assemblyDefs)
{
foreach
(
var
moduleDef
in
assemblyDef.Modules)
{
foreach
(
var
type
in
moduleDef.Types)
RemoveAttributes(type);
}
}
}
private
static
void
RemoveAttributes(TypeDefinition typeDef)
{
RemoveAttributes(typeDef.FullName, typeDef.CustomAttributes);
foreach
(
var
field
in
typeDef.Fields)
RemoveAttributes(field.Name, field.CustomAttributes);
foreach
(
var
property
in
typeDef.Properties)
RemoveAttributes(property.Name, property.CustomAttributes);
foreach
(
var
method
in
typeDef.Methods)
RemoveAttributes(method.Name, method.CustomAttributes);
foreach
(
var
type
in
typeDef.NestedTypes)
RemoveAttributes(type);
}
private
static
void
RemoveAttributes(
string
ownerName, Collection<CustomAttribute> customAttributes)
{
foreach
(
var
attrName
in
RemoveAttributesNames)
{
var
index = -1;
for
(
var
i = 0; i < customAttributes.Count; i++)
{
var
attr = customAttributes[i];
if
(attr.Constructor !=
null
&& attr.Constructor.DeclaringType.FullName == attrName)
{
index = i;
break
;
}
}
if
(index != -1)
customAttributes.RemoveAt(index);
}
}
}
}
|
2、进入Unity目录
Unity/Contents/Frameworks/Tools/UnusedByteCodeStripper2
3、把原来的UnusedByteCodeStripper2.exe命名成 UnusedByteCodeStripper2.org.exe
4、把刚生成 UnusedByteCodeStripper2.org.exe 贴进入。
5、用Unity生成xcode,测试结果