• Unity3D 降低IL2CPP编译可执行文件大小


    项目开始使用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,测试结果

  • 相关阅读:
    卡巴斯基呼吁通过国际立法打击网络犯罪 狼人:
    服务流量论Google的那些服务
    字符数组hdu 4552
    方法说明JAVA复习笔记前言:第一节:从注释开始
    新特性版本Impala各版本新特性
    通知准时为什么讲座时间在通知中提前了半个小时
    集合objectjava_collection
    android对象巧用Android网络通信技术,在网络上直接传输对象
    ejb对象2013年 最新面试题
    提示系统启动关于误更改/var下诺干的权限问题,导致系统启动提示The System is running in lowgraphics mode问题解决 By ACReaper
  • 原文地址:https://www.cnblogs.com/mrblue/p/4979004.html
Copyright © 2020-2023  润新知