项目开始使用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. Python开发【第十七篇】:MySQL(一)

    一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...

  2. javascript取一周的日期

    上代码: <script> var today = new Date(); for (var i = 0; i < 7; i++) { today.setDate(today.get ...

  3. plsql常用函数汇总

    在SQLPLUS下,实现中-英字符集转换alter session set nls_language='AMERICAN';alter session set nls_language='SIMPLI ...

  4. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  5. Ubuntu 16.10 虚拟机安装记录

    一定要选自定义. 这里一定要选 稍后安装操作系统  都是坑! 启动时出现'SMBus Host Controller not enabled'错误提示,进不到图形界面. 解决办法:1.在启动Ubunt ...

  6. Eclipse中集成Ant配置 (转)

    目前的Eclipse都集成了ant,本文图示如何在eclipse下使用ant. 1.新建Java Project-新建Java文件HelloWorld.java HelloWorld.java pac ...

  7. 安卓自动化测试(2)Robotium环境搭建与新手入门教程

    Robotium环境搭建与新手入门教程 准备工具:Robotium资料下载 知识准备: java基础知识,如基本的数据结构.语法结构.类.继承等 对Android系统较为熟悉,了解四大组件,会编写简单 ...

  8. Knockout.Js案例二Working With Lists And Collections

    案例一:Foreach绑定 通常,您要生成重复的UI元素,特别是当显示列表,用户可以添加和删除元素.KO.JS让你轻松,使用的数组和foreach绑定. 在接下来的几分钟,您将构建一个动态UI保留席位 ...

  9. 关于CSS中对IE条件注释的问题

    一.通用区分方式:IE6.IE7能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important:IE7能识别*,也能识别 !important:IE8能识别\0,不能识别* ...

  10. 基于SSL协议的双向认证 - 双向认证 [3]

    1      SSL双向认证的实现 这里是基于SSL和Tomcat配置实现的,配置方法如下: 1.1    生成CA数字证书 首先需要配置OPENSSL环境变量. 我的OPENSSL配置文件路径是“D ...