虚幻引擎(UE4) 枚举(ENUM)的定义方法和需要注意的坑。

在C++中定义枚举:需要注意的是c++ 11增加了enum class类型,这和之前的enum类型有点差别。

  • 只供C++使用:
  • UENUM()
    enum Type
    {
    	Top_Left,
    	Top_Center,
    	Top_Right,
    	Center_Left,
    	Center_Center,
    	Center_Right,
    	Bottom_Left,
    	Bottom_Center,
    	Bottom_Right,
    	Custom
    };
UENUM()
namespace ESpritePivotMode
{
	enum Type
	{
		Top_Left,
		Top_Center,
		Top_Right,
		Center_Left,
		Center_Center,
		Center_Right,
		Bottom_Left,
		Bottom_Center,
		Bottom_Right,
		Custom
	};
}
  •  如果要在蓝图中使用枚举:
#pragma once

UENUM(BlueprintType)
enum class EUI_Zorder : uint8
{
	None = 0,
	Base = 10 UMETA(DisplayName = "Base"),
	UI = 20 UMETA(DisplayName = "UI"),
	Pop = 30 UMETA(DisplayName = "Pop"),
	Animation = 40 UMETA(DisplayName = "Animation"),
	System = 50 UMETA(DisplayName = "System"),
};

UMETA关键字用于修饰枚举值(Display 表示显示描述)

必须要:

  • 加上UENUM(BlueprintType)。

  • 继承自uint8,目前UE4.25只支持uint8(unsigned char),这意味着枚举的值不能超过uint8的范围及(0-255),个人认为这可能是处于对引擎的优化考虑。

  • 必须要有一个枚举值为0。

蓝图中调用:需要将返回值转换为相应的类型如int

猜你喜欢

转载自blog.csdn.net/zhang1461376499/article/details/113790119
今日推荐