项目开始使用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. linux下gimp的使用

    参考资料: http://wenku.baidu.com/view/345c525f804d2b160b4ec070.html 没有视频, 只靠自己摸索使用... 参考文章: http://www.3 ...

  2. 关于JS的几点TIPS

    作为前端基本工作每天都会用到JS...但是我们对JS真的都了解吗,或者说有什么tips是我们不知道的呢.. So..此文关于JS的几点tips..... 一:定时器(可传多个参数) 首先是一个一般的定 ...

  3. 大熊君大话NodeJS之------FS文件模块

    一,开篇分析 文件系统模块是一个简单包装的标准 POSIX 文件 I/O 操作方法集.可以通过调用 require("fs") 来获取该模块.文件系统模块中的所有方法均有异步和同步 ...

  4. sql中的常见的全局变量

    select APP_NAME ( ) as w --当前会话的应用程序 select @@IDENTITY --返回最后插入的标识值 select USER_NAME() --返回用户数据库用户名 ...

  5. 移除wordpress留言中自动链接功能

    默认情况下,在 WordPress 博客的留言中含有 URL,会自动变成可点击的,虽然这样的链接是 nofollow 的,但是还是成为 SPAM 利用的对象,所以可以移除这个自动链接功能. 我们可以在 ...

  6. Javascript高级程序设计——引用类型

    对象在javascript中被称为引用类型的值,而且有一些内置的引用类型可以创建特定的对象: 引用类型与传统面向对象中的程序设计的类相似,但实现不同: Object是一个基础类型,其他所有类型都从Ob ...

  7. js 模块化编程

    Javascript模块化编程(一):模块的写法   作者: 阮一峰 日期: 2012年10月26日 随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞 ...

  8. jQuery Colorbox插件

    http://www.open-open.com/lib/view/open1338084606042.html jQuery Colorbox是一款非常好的内容播放插件.它集弹出层.幻灯片播放功能于 ...

  9. CSU 1116 Kingdoms(枚举最小生成树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...

  10. BZOJ3282——Tree

    1.题目大意:动态树问题,点修改,链查询xor和 裸题一道.. #include <stack> #include <cstdio> #include <cstdlib& ...