项目开始使用IL2CPP编译,后果是可执行文件急剧增加。

google后发现国外一大神写的方法,原帖在这http://forum.unity3d.com/threads/suggestion-for-reducing-the-size-of-il2cpp-generated-executable.338986/

懒得愿意看原帖的直接看我的方法吧

1、新建一个Mono工程,命名成UnusedByteCodeStripper2,贴如下面代码,编译出 UnusedByteCodeStripper2.exe

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,测试结果

Unity3D 降低IL2CPP编译可执行文件大小的更多相关文章

  1. Unity3D之如何将包大小减少到极致

    http://www.luzexi.com Unity3D之如何将包大小减少到极致,图片是游戏app里最最占空间的资源,所以请各位还没有理解u3d对图片文件存储方式理解的请看<unity3d-t ...

  2. Unity3D如何减少安装包大小

    译官方文档:http://docs.unity3d.com/Manual/ReducingFilesize.html PDF文档:http://www.rukawa.cn/Uploads/Attach ...

  3. Unity3d+Jenkins 自动编译iOS、Android版本

    1.在Unity3d中, 创建导出 iOS.Android 项目脚本 PerformBuild.cs ,放在Editor目录下(必须),如下: using UnityEditor; using Sys ...

  4. 【转】NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。

    原文网址:http://blog.csdn.net/hxdanya/article/details/39371759 由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题. ...

  5. Keil C减小代码编译量大小的方法(gai)

    keil-C减小代码编译大小的方法整理 方法一:(通过优化代码减小) 1.1少做乘除运算,使用左/右移位来实现乘除 Eg ,普通:a = 0x80*4: 优化:a = 0x80<<2: 1 ...

  6. python3.6调用c语言动态编译文件 c语言编译可执行文件和动态编译等

    1.c的代码 dfunc.c #include<stdio.h> int dgfunc(int n) { ){ ; }else{ )+dgfunc(n-); } } 2.动态编译 cmd ...

  7. NDK编译可执行文件在Android 中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。

    由于使用了NDK编译的可执行文件在应用中调用,在Android 7.0上的运行情况发现,当运行该可执行文件时,报如下错误: error: only position independent execu ...

  8. [Unity3D]降低向Shader中传值的开销

    Unity3D中提供了很多API用于向shader传值,这篇文章对比测试了两类不同的使用方法的性能. 正文 Unity3D中,通过C#代码向shader传值有两种方式. 一种是面向具体的materia ...

  9. 008_STM32之_keil编译内存大小解析

    Program Size: Code=28784 RO-data=6480 RW-data=60 ZI-data=3900   的含义 1. Code: 程序所占用的FLASH大小,存储在FLASH. ...

随机推荐

  1. chrome调试文章

    http://blog.csdn.net/a6225301/article/details/20207191#t1 http://www.360doc.com/content/13/1220/11/8 ...

  2. SQLSERVER2008R2正确使用索引

    T1表 10000000万条数据,(插入时间36分钟,count(*)查询19秒,空间占用670M左右) 1.真正充分的利用索引比如like '张%' 就是符合SARG(符合扫描参数)标准而like ...

  3. fedora各个版本的下载地址archive

    archive: ['a:kaiv] she went to the city archive this morning. ==================== === fedora 的下载地址是 ...

  4. linux下如何安装charles

    charles是很好的抓包工具. 如何安装呢? 可以去官网下载:http://www.charlesproxy.com/download/ 如果是Debian系列的,可以直接用apt-get 安装 h ...

  5. Java字节流:ByteArrayInputStream ByteArrayOutputStream

    ----------------------------------------------------------------------------------- ByteArrayInputSt ...

  6. 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.ServiceModel.Activation.HttpModule”。

    ********************************* 思路壹(也是网络上铺天盖地的通俗解决方案) 原因: 这是因为先安装了 .NET Framework , 随后启用了 .NET Fra ...

  7. 快速切换IP的批处理!

    内容如下: @echo off color 1A Title [SMART专用 IP设置V1.0] cls echo. echo SMART专用 IP设置V1.0 %date%%time% echo. ...

  8. 计划安装SQL Server2012需求详细

    1.查看 SQL Server2012 安装的安装要求.系统配置检查和安全注意事项. 1.1 硬件要求 [参考资料http://msdn.microsoft.com/zh-cn/library/ms1 ...

  9. Spark之命令

    Spark之命令 1.spark运行模式有4种: a.local 多有用测试, b. standalone:spark 集群模式,使用spark自己的调度方式. c. Yarn: 对Mapreduce ...

  10. gcc 4.8.3 install centos

    http://blog.csdn.net/xlx921027/article/details/17382643