EditorGuiLayout.MaskField 大量枚举的问题

EditorGuiLayout.MaskField issue with large enums

我正在开发一个输入系统,该系统允许用户转换不同输入设备和操作系统之间的输入映射,并可能定义他们自己的输入映射。

我正在尝试为编辑器 window 创建一个 MaskField,用户可以在其中 select 来自 RuntimePlatforms 的列表,但是 selecting 单个值会导致多个值被select编辑。

主要是为了调试,我将它设置为生成一个等效的枚举 RuntimePlatformFlags,它使用它而不是 RuntimePlatform:

[System.Flags]
public enum RuntimePlatformFlags: long
{
    OSXEditor=(0<<0),
    OSXPlayer=(0<<1),
    WindowsPlayer=(0<<2),
    OSXWebPlayer=(0<<3),
    OSXDashboardPlayer=(0<<4),
    WindowsWebPlayer=(0<<5),
    WindowsEditor=(0<<6),
    IPhonePlayer=(0<<7),
    PS3=(0<<8),
    XBOX360=(0<<9),
    Android=(0<<10),
    NaCl=(0<<11),
    LinuxPlayer=(0<<12),
    FlashPlayer=(0<<13),
    LinuxEditor=(0<<14),
    WebGLPlayer=(0<<15),
    WSAPlayerX86=(0<<16),
    MetroPlayerX86=(0<<17),
    MetroPlayerX64=(0<<18),
    WSAPlayerX64=(0<<19),
    MetroPlayerARM=(0<<20),
    WSAPlayerARM=(0<<21),
    WP8Player=(0<<22),
    BB10Player=(0<<23),
    BlackBerryPlayer=(0<<24),
    TizenPlayer=(0<<25),
    PSP2=(0<<26),
    PS4=(0<<27),
    PSM=(0<<28),
    XboxOne=(0<<29),
    SamsungTVPlayer=(0<<30),
    WiiU=(0<<31),
    tvOS=(0<<32),
    Switch=(0<<33),
    Lumin=(0<<34),
    BJM=(0<<35),
}

In this linked screenshot, only the first 4 options were selected. "Platforms: " 旁边的整数是掩码本身。

我在很大程度上不是按位向导,但我的假设是发生这种情况是因为 EditorGUILayout.MaskField returns 一个 32 位 int 值,并且有超过 32 个枚举选项。是否有任何解决方法或其他原因导致此问题?

我注意到的第一件事是 Enum 中的所有值都是相同的,因为您向左移动了 0 位。您可以通过使用此脚本记录您的值来观察这一点。

// Shifts 0 bits to the left, printing "0" 36 times.
for(int i = 0; i < 36; i++){
    Debug.Log(System.Convert.ToString((0 << i), 2));
}

// Shifts 1 bits to the left, printing values up to 2^35.
for(int i = 0; i < 36; i++){
    Debug.Log(System.Convert.ToString((1 << i), 2));
}

long 继承的原因不能单独工作,是因为移位。查看 this example 我发现的问题:

UInt32 x = ....;
UInt32 y = ....;
UInt64 result = (x << 32) + y;

The programmer intended to form a 64-bit value from two 32-bit ones by shifting 'x' by 32 bits and adding the most significant and the least significant parts. However, as 'x' is a 32-bit value at the moment when the shift operation is performed, shifting by 32 bits will be equivalent to shifting by 0 bits, which will lead to an incorrect result.


所以你也应该转换移位位。像这样:

public enum RuntimePlatformFlags : long {
    OSXEditor = (1 << 0),
    OSXPlayer = (1 << 1),
    WindowsPlayer = (1 << 2),
    OSXWebPlayer = (1 << 3),

    // With literals.
    tvOS = (1L << 32),
    Switch = (1L << 33),

    // Or with casts.
    Lumin = ((long)1 << 34),
    BJM = ((long)1 << 35),
}