UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符

文章目录

  • 枚举(ENUM)
    • 第一种方式
    • 第二种方式
  • 结构体(USTRUCT)
  • 补充属性说明符(ExposeOnSoawn)
  • 结构体创建数据表格

枚举(ENUM)

第一种方式

定义枚举

UENUM(BlueprintType)
namespace MyEnumType
{
	enum MyCustomType
	{
		Type1, // 或者使用带 DisplayName别名 ==> Enum1 UMETA(DisplayName = "Type1"),
		Type2,
		Type3,
		Type4,
	};
}

调用枚举,使用TEnumAsByte模板
UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第1张图片

//在蓝图中声明
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyEnumType")
TEnumAsByte<MyEnumType::MyCustomType> MyEnumType;

UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第2张图片

第二种方式

UENUM(BlueprintType)
enum class MyEnumType2 : uint8
{
	Enum1 UMETA(DisplayName = "Type1"),
	Enum2 UMETA(DisplayName = "Type2"),
	Enum3 UMETA(DisplayName = "Type3"),
	Enum4 UMETA(DisplayName = "Type4"),
};
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyEnumType2")
TEnumAsByte<MyEnumType2> MyEnumType2;

UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第3张图片
UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第4张图片

结构体(USTRUCT)

定义结构体
注意,定义结构体名称前要加F前缀,不然编译不通过。

USTRUCT(BlueprintType)
struct FMyCustomStruct
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyStructType")
	FString ID;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyStructType")
	FString Name;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyStructType")
	int32 Age;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyStructType")
	float Height;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyStructType")
	bool IsMan;
};

声明结构体

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyCustomStruct")
	FMyCustomStruct MyCustomStruct;

编译后在蓝图中可以看到
UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第5张图片

也可以使用它
UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第6张图片

补充属性说明符(ExposeOnSoawn)

蓝图生成时暴露变量,方便赋予变量的初始值

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyExposeOnSpawn", meta = (ExposeOnSpawn = "ExposeOnSpawnValue"))
	float ExposeOnSpawnValue;

编译后,打开关卡蓝图

UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第7张图片

结构体创建数据表格

先创建一个结构体数据类

#include "Engine/DataTable.h"

USTRUCT(BlueprintType)
struct FDateTableStruct:public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DateTableStruct")
	FString ID;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DateTableStruct")
	FString Name;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DateTableStruct")
	int32 Age;
};

编译之后,创建一个关联的信息表格,保存为.csv格式
UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第8张图片
然后拖到项目文件目录中
UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第9张图片
UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第10张图片
UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符_第11张图片

你可能感兴趣的:(UE5,C++,入门开发,ue5,c++)