【UE4 C++ 基础知识】<1> UPROPERTY宏、属性说明符、元数据说明符
属性声明
- 属性使用标准的C++变量语法声明,前面用UPROPERTY宏来定义属性元数据和变量说明符。
UPROPERTY([specifier, specifier, ...], [meta(key=value, key=value, ...)])
Type VariableName;
-
示例:位掩码
UENUM(BlueprintType)
enum class EColorBits1 :uint8
{
ECB_Red,
ECB_Green,
ECB_Blue,
};
//使用"bitflags"元标记来创建UENUM,实际使用似乎没差别
UENUM(BlueprintType, Meta = (Bitflags))
enum class EColorBits2 :uint8
{
ECB_Red,
ECB_Green,
ECB_Blue,
};
UPROPERTY(EditAnywhere, Meta = (Bitmask))
int32 BasicBits;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
EColorBits1 ColorBits1;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
EColorBits2 ColorBits2;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Meta = (Bitmask, BitmaskEnum = "EColorBits1"))
int32 ColorFlags3;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Meta = (Bitmask, BitmaskEnum = "EColorBits2"))
int32 ColorFlags4;
在上述示例中, ECB_Red 值为0,表示它被选中时将激活位0(将ColorFlags增加1)。ECB_Green对应于位1(将ColorFlags增加2),ECB_Blue 对应于位2(将ColorFlags增加4)。
属性说明符
作用
- 声明属性时,属性说明符 可被添加到声明,以控制属性与引擎和编辑器诸多方面的相处方式。
常用说明符
VisibleDefaultsOnly
// 仅在 Class Defaults 可见
UPROPERTY(VisibleDefaultsOnly)
int32 VisibleDefaultsOnlyInt;
VisibleInstanceOnly
// 仅在实例化 Detail 可见
UPROPERTY(VisibleInstanceOnly)
FVector VisibleInstanceOnlyVector;
VisibleAnywhere
//Class Defaults 和 实例化 Detail 可见
UPROPERTY(VisibleAnywhere)
FString VisibleAnywhereString;
EditDefaultsOnly
//仅在 Class Defaults 可编辑
UPROPERTY(EditDefaultsOnly)
int32 EditDefaultsOnlyInt;
EditInstanceOnly
//仅在实例化 Detail 可编辑
UPROPERTY(EditInstanceOnly)
FVector EditInstanceOnlyVector;
EditAnywhere
//Class Defaults 和 实例化 Detail 可编辑
UPROPERTY(EditAnywhere)
FString EditAnywhereString;
BlueprintReadOnly
//Variables 只读
UPROPERTY(BlueprintReadOnly)
int32 intValue_BlueprintReadOnly;
BlueprintReadWrite
//Variables 可读写
UPROPERTY(BlueprintReadWrite)
int32 intValue_BlueprintReadWrite;
Category 类别
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="TopCategory")
int32 intValue1;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TopCategory|SubCategory")
int32 intValue2;
BlueprintAssignable
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FNotifyPawnChange, float, PawnHpPercent, float, PawnPhysicalShieldPercent, float, PawnMageShieldPercent);
//蓝图中可绑定委托
UPROPERTY(BlueprintAssignable)
FNotifyPawnChange NotifyPawnChange;
元数据说明符 meta
作用
声明类、接口、结构体、列举、列举值、函数,或属性时,可添加 元数据说明符 来控制其与引擎和编辑器各方面的相处方式。每一种类型的数据结构或成员都有自己的元数据说明符列表
常见说明符
DisplayName 别名
- 可以便于蓝图变量搜索,如果变量名不好记的话
EditCondition 条件可编辑
- 支持bool、比较等条件判断
UPROPERTY(EditAnywhere,BlueprintReadWrite, meta = (DisplayName="UseOffset1、2 标志位") )
bool bUseOffset;
UPROPERTY(EditAnywhere, meta = (DisplayName = "ContitionValue 控制 offset3"))
int32 ContitionValue;
UPROPERTY(EditAnywhere, meta = (DisplayName = "ContitionColorBits 控制 offset4"))
EColorBits1 ContitionColorBits;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition = "bUseOffset"))
float Offset1;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition = "!bUseOffset"))
float Offset2;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition = "ContitionValue>0"))
float Offset3;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition = "ContitionColorBits==EColorBits1::ECB_Green"))
float Offset4;
BindWidget 和 UMG 同名同类型控件绑定
UPROPERTY(EditAnywhere, meta = (BindWidget))
UImage* TCanvas;
UPROPERTY(EditAnywhere, BlueprintReadWrite , meta = (BindWidget))
UButton* Btn_Mass;
UMETA 扩展宏
- cpp meta里的中文在蓝图经常会乱码
- 解决方法一:高级保存选择 Unicode 65001
- 解决办法二:使用UMETA中的DisplayName
- 可用于Enum的元素别名
- 可用于结构体的元素别名
- 可用于Datatable的别名
UPROPERTY(EditAnywhere)
FName ChineseName UMETA(DisplayName="中文名");
UPROPERTY(EditAnywhere)
float Weight UMETA(DisplayName = "体重") = 65.0f;
UPROPERTY(EditAnywhere)
EColorBits1 FavoriteColorBits UMETA(DisplayName = "最喜欢的颜色")=EColorBits1::ECB_Blue;
参考
作者:砥才人
出处:https://www.cnblogs.com/shiroe
本系列文章为笔者整理原创,只发表在博客园上,欢迎分享本文链接,如需转载,请注明出处!