「Unity3D」使用Il2CppSetOption优化IL2CPP生成C++代码的性能

使用IL2CPP生成C++代码,有三个检查项可选,即:Null checks(空值检查)、Array bounds checks(数组越界检查)、Divide by zero checks(除数为0检查)——其中,前两个是默认开启的,后一个是默认关闭的。

显然,运行时可以全部给关闭了,不需要每帧各个对象检查空值与越界。方法是,需要一个Il2CppSetOption.cs文件——在Unity的安装目录下官方提供:

  • Windows在 Data\il2cpp
  • OS X在Contents/Frameworks/il2cpp
using System;

namespace Unity.IL2CPP.CompilerServices
{
    /// 
    /// The code generation options available for IL to C++ conversion.
    /// Enable or disabled these with caution.
    /// 
    public enum Option
    {
        /// 
        /// Enable or disable code generation for null checks.
        ///
        /// Global null check support is enabled by default when il2cpp.exe
        /// is launched from the Unity editor.
        ///
        /// Disabling this will prevent NullReferenceException exceptions from
        /// being thrown in generated code. In *most* cases, code that dereferences
        /// a null pointer will crash then. Sometimes the point where the crash
        /// happens is later than the location where the null reference check would
        /// have been emitted though.
        /// 
        NullChecks = 1,
        /// 
        /// Enable or disable code generation for array bounds checks.
        ///
        /// Global array bounds check support is enabled by default when il2cpp.exe
        /// is launched from the Unity editor.
        ///
        /// Disabling this will prevent IndexOutOfRangeException exceptions from
        /// being thrown in generated code. This will allow reading and writing to
        /// memory outside of the bounds of an array without any runtime checks.
        /// Disable this check with extreme caution.
        /// 
        ArrayBoundsChecks = 2,
        /// 
        /// Enable or disable code generation for divide by zero checks.
        ///
        /// Global divide by zero check support is disabled by default when il2cpp.exe
        /// is launched from the Unity editor.
        ///
        /// Enabling this will cause DivideByZeroException exceptions to be
        /// thrown in generated code. Most code doesn't need to handle this
        /// exception, so it is probably safe to leave it disabled.
        /// 
        DivideByZeroChecks = 3,
    }

    /// 
    /// Use this attribute on an assembly, struct, class, method, or property to inform the IL2CPP code conversion utility to override the
    /// global setting for one of a few different runtime checks.
    ///
    /// Example:
    ///
    ///     [Il2CppSetOption(Option.NullChecks, false)]
    ///     public static string MethodWithNullChecksDisabled()
    ///     {
    ///         var tmp = new Object();
    ///         return tmp.ToString();
    ///     }
    /// 
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Delegate, Inherited = false, AllowMultiple = true)]
    public class Il2CppSetOptionAttribute : Attribute
    {
        public Option Option { get; private set; }
        public object Value { get; private set; }

        public Il2CppSetOptionAttribute(Option option, object value)
        {
            Option = option;
            Value = value;
        }
    }
}

这东西,可以用在AssemblyStructClassMethod 、​​​​​​​PropertyDelegate等各处——官方有示例代码:Unity - Manual: IL2CPP Overview——但没有给出Assembly的例子,而这个可以应用全局,所以这里贴出这个设置,放到AssemblyInfo.cs文件中。

using Unity.IL2CPP.CompilerServices;

// Disable the global code generation for null checks.
[assembly: Il2CppSetOption(Option.NullChecks, false)]

// Disable the global code generation for array bounds checks.
[assembly: Il2CppSetOption(Option.ArrayBoundsChecks, false)]

你可能感兴趣的:(Unity3D,Unity,Il2CppSetOption,IL2CPP,IL2CPP优化,Unity优化C++)