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,測試結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM