0%

UE(5):枚举UENUM、结构体USTRUCT

枚举UENUM

UENUM生成枚举的反射数据,通过反射将枚举暴露给蓝图,进行C++和蓝图之间的通信

1
2
3
4
5
6
7
8
9
10
11
12
13
// 生成枚举的反射数据,通过反射将枚举暴露给蓝图,进行C++和蓝图之间的通信
// BlueprintType:在蓝图中创建的变量类型可以选择这个枚举
UENUM(BlueprintType)
namespace MyEnumType {
enum MyCustomEnum {
Type1,
Type2,
Type3
};
}
// 在类中声明变量
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyEnum");
TEnumAsByte<MyEnumType::MyCustomEnum> MyCustomEnum;
1
2
3
4
5
6
7
// 枚举声明的另一种方式
UENUM(BlueprintType)
enum class EMtTestEnum :uint8 {
OneType UMETA(DisplayName = "OneType"),
TwoType UMETA(DisplayName = "TwoType"),
ThreeType UMETA(DisplayName = "ThreeType"),
};

image-20240821130846119+

BlueprintType在蓝图中创建的变量类型可以选择这个枚举

image-20240821130940475

结构体USTRUCT

USTRUCT()生成结构体的反射数据,通过反射将结构体的变量或者方法暴露给蓝图

BlueprintType在蓝图中创建的变量类型可以选择这个结构体

ps:命名必须以F开头

1
2
3
4
5
6
7
8
9
10
11
12
// 生成结构体的反射数据,通过反射将结构体的变量或者方法暴露给蓝图
// BlueprintType:在蓝图中创建的变量类型可以选择这个结构体
USTRUCT(BlueprintType)
struct FMyStruct // 命名必须以F开头
{
GENERATED_USTRUCT_BODY()

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyTestStruct")
int32 Health;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyTestStruct")
FString MyName;
};
1
2
3
// 声明一个结构体的变量
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyCustomStruct")
FMyStruct MyCustomStruct;

使用结构体接收数据表格

创建 Object

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Engine/Classes/Engine/DataTable.h"

USTRUCT(BlueprintType)
struct FMyDataTableStruct: public FTableRowBase
{
GENERATED_USTRUCT_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyTestDataTableStruct")
float Health;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyTestDataTableStruct")
FString Name;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyTestDataTableStruct")
int Level;
};

创建数据表格,保存为 csv 格式

image-20240821145917602

在项目中拖入文件