UE5 C++(六)— 枚举UENUM、结构体USTRUCT和补充属性说明符
2023-12-19 06:00:31
枚举(ENUM)
第一种方式
定义枚举
UENUM(BlueprintType)
namespace MyEnumType
{
enum MyCustomType
{
Type1, // 或者使用带 DisplayName别名 ==> Enum1 UMETA(DisplayName = "Type1"),
Type2,
Type3,
Type4,
};
}
调用枚举,使用TEnumAsByte模板
//在蓝图中声明
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyEnumType")
TEnumAsByte<MyEnumType::MyCustomType> MyEnumType;
第二种方式
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;
结构体(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;
编译后在蓝图中可以看到
也可以使用它
补充属性说明符(ExposeOnSoawn)
蓝图生成时暴露变量,方便赋予变量的初始值
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyExposeOnSpawn", meta = (ExposeOnSpawn = "ExposeOnSpawnValue"))
float ExposeOnSpawnValue;
编译后,打开关卡蓝图
结构体创建数据表格
先创建一个结构体数据类
#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格式
然后拖到项目文件目录中
文章来源:https://blog.csdn.net/qq_40120946/article/details/135056766
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!