问题描述
我试图实现 OpenSimplex Noise https://gist.github.com/digitalshadow/134a3a02b67cecd72181 在我的游戏中,但我收到错误'MethodImplOptions' 不包含'AggressiveInlining' 的定义".
I was trying to implement OpenSimplex Noise https://gist.github.com/digitalshadow/134a3a02b67cecd72181 in my game, but I'm getting the error "'MethodImplOptions' does not contain a definition for 'AggressiveInlining'".
[ MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int FastFloor(double x)
{
var xi = (int)x;
return x < xi ? xi - 1 : xi;
}
是的,我包含了using System.Runtime.CompilerServices;
.现在,老实说,我不知道内联是如何工作的,所以我什至不知道 Unity 中的程序是否有必要这样做.如果我不需要它,我想知道用什么代替.提前致谢.
Yes, I included using System.Runtime.CompilerServices;
. Now, I'll be perfectly honest, I don't know how inlining works, so I don't even know if it's necessary for a program in Unity. If I don't need it, I would like to know what to use instead. Thanks in advance.
推荐答案
MethodImplOptions.AggressiveInlining
枚举不存在,因为此功能是在 .NET 4.5 中添加的,而 Unity仍在使用 .NET 2.0/3.5.
The MethodImplOptions.AggressiveInlining
enum does not exist because this feature was added in .NET 4.5 while Unity is still using .NET 2.0/3.5.
我什至不知道 Unity 中的程序是否有必要
这不是必需的,但它似乎加快了函数调用的速度.您可以在此处查看速度测试.
It's not necessary but it seems to speed up function calling. You can see a speed test here.
我想知道用什么代替
您可以使用 [MethodImpl(256)]
代替 MethodImplOptions.AggressiveInlining
.这也应该有效.
You can use [MethodImpl(256)]
instead of MethodImplOptions.AggressiveInlining
. That should work too.
查看下面 MethodImplOptions
枚举的源代码:
Look at the source code for the MethodImplOptions
enum below:
public enum MethodImplOptions {
Unmanaged = 4,
ForwardRef = 16,
InternalCall = 4096,
Synchronized = 32,
NoInlining = 8,
PreserveSig = 128,
NoOptimization = 64,
#if NET_4_5
AggressiveInlining = 256,
#endif
} // MethodImplOptions
如您所见,AggressiveInlining
的值为 256
.当您使用 MethodImplAttribute
时 构造重载带有 int
参数,您基本上可以使用任何不存在的 enum
值.在任何 .NET 版本上传递它 256
应该会给你 AggressiveInlining
.新的枚举值只是为了更容易记住/使用.
As you can see, AggressiveInlining
has a value of 256
. When you use the MethodImplAttribute
construction overload with the int
parameter, you can essentially use any enum
value that does not exist. Passing it 256
should give you AggressiveInlining
on any .NET version. The new enum value is simply there to make it easier to remember/use.
另一个来源来自Unity工程师:
Another source is from Unity Engineer:
.NET 小技巧:使用 [MethodImpl(256)] 而不是MethodImplOptions.AggressiveInlining 在任何 .NET 版本上编译并避免 ifdefs
这篇关于AggressiveInlining 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!