1. 原生枚举(Native Enums)
定义方法: 原生枚举是 C++ 中的基本枚举,不包含虚幻引擎的反射信息。它们仅在 C++ 代码中可用,不会自动暴露给蓝图。
#pragma once
#include "CoreMinimal.h"
enum class ENativeEnum : uint8
{
FirstValue,
SecondValue,
ThirdValue
};
2. 反射枚举(Reflected Enums)
定义方法: 反射枚举通过虚幻引擎的 UENUM
宏定义,支持蓝图中的使用和编辑。它们允许在编辑器中进行更强大的集成和自定义。
#pragma once
#include "CoreMinimal.h"
#include "ReflectedEnum.generated.h"
UENUM(BlueprintType) // 使枚举在蓝图中可用
enum class EReflectedEnum : uint8
{
ValueOne UMETA(DisplayName = "Value One"),
ValueTwo UMETA(DisplayName = "Value Two"),
ValueThree UMETA(DisplayName = "Value Three")
};
UENUM
宏搭配BlueprintType
可以将枚举暴露给蓝图
UENUM
UENUM将枚举类型进行标记,方便UHT为其生成相应的类型文件,生成反射系统需要的代码。BlueprintType说明符令该枚举可以在蓝图中自如使用。
UMETA
使用UMETA宏来指定每个枚举值指定一个显示名,即在蓝图使用中和UI中显示的名称。
TEnumAsByte
虚幻引擎还提供了一种特殊类型的枚举,称为TEnumAsByte,用于加强类型安全并且解决一些常见的错误,主要是一些将枚举类型强行用作其基础类型的应用场合,例如意外使用枚举作为数组的索引。
枚举的基础类型
-
uint8
: 默认和最常用的基础类型,范围从 0 到 255。// MyEnum_Uint8.h #pragma once #include "CoreMinimal.h" #include "MyEnum_Uint8.generated.h" UENUM(BlueprintType) enum class EMyEnum_Uint8 : uint8 { OptionA UMETA(DisplayName = "Option A"), OptionB UMETA(DisplayName = "Option B"), OptionC UMETA(DisplayName = "Option C") };
-
int32
: 适用于需要更大范围的枚举,范围从 -2,147,483,648 到 2,147,483,647。// MyEnum_Int32.h #pragma once #include "CoreMinimal.h" #include "MyEnum_Int32.generated.h" UENUM(BlueprintType) enum class EMyEnum_Int32 : int32 { ValueOne UMETA(DisplayName = "Value One"), ValueTwo UMETA(DisplayName = "Value Two"), ValueThree UMETA(DisplayName = "Value Three") };
-
uint32
: 无符号整型基础类型,范围从 0 到 4,294,967,295。// MyEnum_Uint32.h #pragma once #include "CoreMinimal.h" #include "MyEnum_Uint32.generated.h" UENUM(BlueprintType) enum class EMyEnum_Uint32 : uint32 { Alpha UMETA(DisplayName = "Alpha"), Beta UMETA(DisplayName = "Beta"), Gamma UMETA(DisplayName = "Gamma") };
-
int64
: 用于需要更大范围的整数,范围从 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807。 -
uint64
: 无符号的 64 位整数,范围从 0 到 18,446,744,073,709,551,615。// MyEnum_Int64.h #pragma once #include "CoreMinimal.h" #include "MyEnum_Int64.generated.h" UENUM(BlueprintType) enum class EMyEnum_Int64 : int64 { HighValue UMETA(DisplayName = "High Value"), LowValue UMETA(DisplayName = "Low Value") };
一些常见的数据类型
int8_t | typedef signed char; |
uint8_t | typedef unsigned char; |
int16_t | typedef signed short ; |
uint16_t | typedef unsigned short ; |
int32_t | typedef signed int; |
uint32_t | typedef unsigned int; |
int64_t | typedef signed long long; |
uint64_t | typedef unsigned long long; |