属性声明
属性使用标准的C++变量语法声明,前面用UPROPERTY宏来定义属性元数据和变量说明符。
UPROPERTY([specifier, specifier, ...], [meta(key=value, key=value, ...)])
Type VariableName;
核心数据类型
整数
整数数据类型转换是"int"或"uint"后跟位大小。
变量类型 | 说明 |
---|---|
uint8 | 8位无符号 |
uint16 | 16位无符号 |
uint32 | 32位无符号 |
uint64 | 64位无符号 |
int8 | 8位有符号 |
int16 | 16位有符号 |
int32 | 32位有符号 |
int64 | 64位有符号 |
作为位掩码
整数属性现在可以位掩码形式公开给编辑器。要将整数属性标记为位掩码,只需在meta分段中添加"bitmask"即可,如下所示:
/*~ BasicBits appears as a list of generic flags in the editor, instead of an integer field. */
UPROPERTY(EditAnywhere, Meta = (Bitmask))
int32 BasicBits;
添加此元标记将使整数作为下拉列表形式可供编辑,它们使用笼统命名标记("Flag 1"、"Flag 2"、"Flag 3"等等),可以 单独打开或关闭。

你也可以让蓝图可调用函数的整型参数表现为位掩码,方法是在参数的 UPARAM
指定器上添加 Bitmask
元标签(不需要值)。
/*~ You can set MyFunction using a generic list of flags instead of typing in an integer value. */
UFUNCTION(BlueprintCallable)
void MyFunction(UPARAM(meta=(Bitmask)) int32 BasicBitsParam)
为了自定义位标记名称,首先必须使用"bitflags"元标记来创建UENUM:
UENUM(Meta = (Bitflags))
enum class EColorBits
{
ECB_Red,
ECB_Green,
ECB_Blue
};
比特掩码枚举类型的范围是0到31,包括0和31。其对应于32位整型变量的位数(从第0位开始)。在上面的例子中,第0位是 ECB_Red
,第1位是 ECB_Green
,第2位是 ECB_Blue
。
作为另一种声明方式,你可以使用 ENUM_CLASS_FLAGS
在定义完枚举类型后,将其变成一个位掩码。为了在编辑器中使用标志选择器(flag selector),我们还必须添加元字段 UseEnumValuesAsMaskValuesInEditor
并将其设置为 true
。关键的区别在于,这个方法直接使用掩码值,而不是比特数。使用此方法制作的等效枚举类型看起来像这样:
UENUM(Meta = (Bitflags, UseEnumValuesAsMaskValuesInEditor = "true"))
enum class EColorBits
{
ECB_Red = 0x01,
ECB_Green = 0x02,
ECB_Blue = 0x04
};
ENUM_CLASS_FLAGS(EColorBits);
创建该UENUM后,可以使用"BitmaskEnum"元标记来引用它,如:
/*~ This property lists flags matching the names of values from EColorBits. */
UPROPERTY(EditAnywhere, Meta = (Bitmask, BitmaskEnum = "EColorBits"))
int32 ColorFlags;
完成这个更改后,下拉框中列出的位标记将使用列举类条目的名称和值。在上述示例中, ECB_Red 值为0,表示它被选中时将激活位0(将ColorFlags增加1)。ECB_Green对应于位1(将ColorFlags增加2),ECB_Blue 对应于位2(将ColorFlags增加4)。

同样,你可以在 UPARAM
标签的meta部分添加 BitmaskEnum
和对应的枚举类型名称来定制它。
/*~ MyOtherFunction shows flags named after the values from EColorBits. */
UFUNCTION(BlueprintCallable)
void MyOtherFunction(UPARAM(meta=(Bitmask, BitmaskEnum = "EColorBits")) int32 ColorFlagsParam)
虽然列举类型包含超过32个条目,但在属性编辑器UI中,位掩码关联中只会看到前32个值。同样,虽然可接受显式值条目,但显式值介于0-31的条目不会包含在下拉列表中。
浮点类型
虚幻使用标准C++浮点类型、浮点和双精度。
布尔类型
布尔类型可以使用C++ ool关键字表示或表示为位域。
uint32 bIsHungry : 1;
bool bIsThirsty;
字符串
虚幻引擎4支持三种核心类型的字符串。
- FString是典型的"动态字符数组"字符串类型。
- FName是对全局字符串表中不可变且不区分大小写的字符串的引用。相较于FString,它的大小更小,更能高效的传递,但更难以操控。
- FText是指定用于处理本地化的更可靠的字符串表示。
对于大多数情况下,虚幻依靠TCHAR类型来表示字符。TEXT()宏可用于表示TCHAR文字。
MyDogPtr->DogName = FName(TEXT("Samson Aloysius"));
有关这三种字符串类型、何时使用哪个类型以及如何使用它们的更多信息,请参阅字符串处理文档。
属性说明符
When declaring properties, Property Specifiers can be added to the declaration to control how the property behaves with various aspects of the Engine and Editor.
Property Tag | Effect |
---|---|
AdvancedDisplay |
The property will be placed in the advanced (dropdown) section of any panel where it appears. |
AssetRegistrySearchable |
The AssetRegistrySearchable Specifier indicates that this property and its value will be automatically added to the Asset Registry for any Asset class instances containing this as a member variable. It is not legal to use on struct properties or parameters. |
BlueprintAssignable |
Usable with Multicast Delegates only. Exposes the property for assigning in Blueprints. |
BlueprintAuthorityOnly |
This property must be a Multicast Delegate. In Blueprints, it will only accept events tagged with BlueprintAuthorityOnly . |
BlueprintCallable |
Multicast Delegates only. Property should be exposed for calling in Blueprint code. |
BlueprintGetter=GetterFunctionName |
This property specifies a custom accessor function. If this property isn't also tagged with BlueprintSetter or BlueprintReadWrite , then it is implicitly BlueprintReadOnly . |
BlueprintReadOnly |
This property can be read by Blueprints, but not modified. This Specifier is incompatible with the BlueprintReadWrite Specifier. |
BlueprintReadWrite |
This property can be read or written from a Blueprint. This Specifier is incompatible with the BlueprintReadOnly Specifier. |
BlueprintSetter=SetterFunctionName |
This property has a custom mutator function, and is implicitly tagged with BlueprintReadWrite . Note that the mutator function must be named and part of the same class. |
Category="TopCategory\|SubCategory\|..." |
Specifies the category of the property when displayed in Blueprint editing tools. Define nested categories using the | operator. |
Config |
This property will be made configurable. The current value can be saved to the .ini file associated with the class and will be loaded when created. Cannot be given a value in default properties. Implies BlueprintReadOnly . |
DuplicateTransient |
Indicates that the property's value should be reset to the class default value during any type of duplication (copy/paste, binary duplication, etc.). |
EditAnywhere |
Indicates that this property can be edited by property windows, on archetypes and instances. This Specifier is incompatible with any of the the "Visible" Specifiers. |
EditDefaultsOnly |
Indicates that this property can be edited by property windows, but only on archetypes. This Specifier is incompatible with any of the "Visible" Specifiers. |
EditFixedSize |
Only useful for dynamic arrays. This will prevent the user from changing the length of an array via the Unreal Editor property window. |
EditInline |
Allows the user to edit the properties of the Object referenced by this property within Unreal Editor's property inspector (only useful for Object references, including arrays of Object reference). |
EditInstanceOnly |
Indicates that this property can be edited by property windows, but only on instances, not on archetypes. This Specifier is incompatible with any of the "Visible" Specifiers. |
Export |
Only useful for Object properties (or arrays of Objects). Indicates that the Object assigned to this property should be exported in its entirety as a subobject block when the Object is copied (such as for copy/paste operations), as opposed to just outputting the Object reference itself. |
GlobalConfig |
Works just like Config except that you cannot override it in a subclass. Cannot be given a value in default properties. Implies BlueprintReadOnly . |
Instanced |
Object (UCLASS ) properties only. When an instance of this class is created, it will be given a unique copy of the Object assigned to this property in defaults. Used for instancing subobjects defined in class default properties. Implies EditInline and Export . |
Interp |
Indicates that the value can be driven over time by a Track in Sequencer. |
Localized |
The value of this property will have a localized value defined. Mostly used for strings. Implies ReadOnly . |
Native |
Property is native: C++ code is responsible for serializing it and exposing to Garbage Collection. |
NoClear |
Prevents this Object reference from being set to none from the editor. Hides clear (and browse) button in the editor. |
NoExport |
Only useful for native classes. This property should not be included in the auto-generated class declaration. |
NonPIEDuplicateTransient |
The property will be reset to the default value during duplication, except if it's being duplicated for a Play In Editor (PIE) session. |
NonTransactional |
Indicates that changes to this property's value will not be included in the editor's undo/redo history. |
NotReplicated |
Skip replication. This only applies to struct members and parameters in service request functions. |
Replicated |
The property should be replicated over the network. |
ReplicatedUsing=FunctionName |
The ReplicatedUsing Specifier specifies a callback function which is executed when the property is updated over the network. |
RepRetry |
Only useful for struct properties. Retry replication of this property if it fails to be fully sent (for example, Object references not yet available to serialize over the network). For simple references, this is the default, but for structs, this is often undesirable due to the bandwidth cost, so it is disabled unless this flag is specified. |
SaveGame |
This Specifier is a simple way to include fields explicitly for a checkpoint/save system at the property level. The flag should be set on all fields that are intended to be part of a saved game, and then a proxy archiver can be used to read/write it. |
SerializeText |
Native property should be serialized as text (ImportText , ExportText ). |
SkipSerialization |
This property will not be serialized, but can still be exported to a text format (such as for copy/paste operations). |
SimpleDisplay |
Visible or editable properties appear in the Details panel and are visible without opening the "Advanced" section. |
TextExportTransient |
This property will not be exported to a text format (so it cannot, for example, be used in copy/paste operations). |
Transient |
Property is transient, meaning it will not be saved or loaded. Properties tagged this way will be zero-filled at load time. |
VisibleAnywhere |
Indicates that this property is visible in all property windows, but cannot be edited. This Specifier is incompatible with the "Edit" Specifiers. |
VisibleDefaultsOnly |
Indicates that this property is only visible in property windows for archetypes, and cannot be edited. This Specifier is incompatible with any of the "Edit" Specifiers. |
VisibleInstanceOnly |
Indicates that this property is only visible in property windows for instances, not for archetypes, and cannot be edited. This Specifier is incompatible with any of the "Edit" Specifiers. |
Metadata Specifiers
声明类、接口、结构体、列举、列举值、函数,或属性时,可添加 元数据说明符 来控制其与引擎和编辑器各方面的相处方式。每一种类型的数据结构或成员都有自己的元数据说明符列表。
Metadata只存在于编辑器中。请不要编写能够访问到Metadata的游戏逻辑。
属性元标签 | 效果 |
---|---|
AllowAbstract="true/false" |
用于 Subclass 和 SoftClass 属性。说明抽象类属性是否应显示在类选取器中。 |
AllowedClasses="Class1, Class2, .." |
用于 FSoftObjectPath 属性。逗号分隔的列表,表明要显示在资源选取器中的资源类类型。 |
AllowPreserveRatio |
用于 Fvector 属性。在细节面板中显示此属性时将添加一个比率锁。 |
ArrayClamp="ArrayProperty" |
用于整数属性。将可在UI中输入的有效值锁定在0和命名数组属性的长度之间。 |
AssetBundles |
用于 SoftObjectPtr 或 SoftObjectPath 属性。主数据资源中使用的束列表命名,指定此引用属于哪个束的一部分。 |
BlueprintBaseOnly |
用于 Subclass 和 SoftClass 属性。说明蓝图类是否应显示在类选取器中。 |
BlueprintCompilerGeneratedDefaults |
属性默认项由蓝图编译器生成,CopyPropertiesForUnrelatedObjects 在编译后调用时将不会被复制。 |
ClampMin="N" |
用于浮点和整数属性。指定可在属性中输入的最小值 N 。 |
ClampMax="N" |
用于浮点和整数属性。指定可在属性中输入的最大值 N 。 |
ConfigHierarchyEditable |
此属性被序列化为一个配置(.ini )文件,可在配置层级中的任意处进行设置。 |
ContentDir |
由 FDirectoryPath 属性使用。说明将使用 Content 文件夹中的Slate风格目录选取器来选取路径。 |
DisplayAfter="PropertyName" |
在蓝图编辑器中,名为 PropertyName 的属性后即刻显示此属性。前提是两个属性属于同一类别,则忽略其在源代码中的顺序进行显示。如多个属性有相同的 DisplayAfter 值和相同的 DisplayPriority 值,将在指定属性之后,按照自身在标头文件中声明的顺序显示。 |
DisplayName="Property Name" |
此属性显示的命名,不显示代码生成的命名。 |
DisplayPriority="N" |
如两个属性有相同的 DisplayAfter 值,或属于同一类别且无 DisplayAfter 元标签,则此属性将决定其顺序。最高优先级值为1,表示 DisplayPriority 值为1的属性将在 DisplayProirity 值为2的属性之上显示。如多个属性有相同的 DisplayAfter 值,其将按照在标头文件中声明的顺序显示。 |
DisplayThumbnail="true" |
说明属性是一个资源类型,其应显示选中资源的缩略图。 |
EditCondition="BooleanPropertyName" |
对一个布尔属性进行命名,此属性用于说明此属性的编辑是否被禁用。将"!"放置在属性命名前可颠倒测试。 |
EditFixedOrder |
使排列的元素无法通过拖拽来重新排序。 |
ExactClass="true" |
结合 AllowedClasses 用于 FSoftObjectPath 属性。说明是否只能使用 AllowedClasses 中指定的准确类,或子类是否同样有效。 |
ExposeFunctionCategories="Category1, Category2, .." |
在蓝图编辑器中编译一个函数列表时,指定其函数应被公开的类目的列表。 |
ExposeOnSpawn="true" |
指定此属性是否应在此类类型的一个Spawn Actor节点上公开。 |
FilePathFilter="FileType" |
由 FFilePath 属性使用。说明在文件选取器中显示的路径过滤器。常规值包括"uasset"和"umap",但这些并非唯一可能的值。 |
GetByRef |
使该属性的"Get"蓝图节点返回对属性的常量引用,而不是其值的副本。只对稀疏类数据生效,只能在不存在 NoGetter 时使用。 |
HideAlphaChannel |
用于 Fcolor 和 FLinearColor 属性。说明详细显示属性控件时 Alpha 属性应为隐藏状态。 |
HideViewOptions |
用于 Subclass 和 SoftClass 属性。隐藏在类选取器中修改显示选项的功能。 |
InlineEditConditionToggle |
表示出布尔属性只内联显示为其他属性中的一个编辑条件切换,不应显示在其自身的行上。 |
LongPackageName |
由 FDirectoryPath 属性使用。将路径转换为一个长的包命名。 |
MakeEditWidget |
用于变换或旋转体属性,或变换/旋转体的排列。说明属性应在视口中公开为一个可移动控件。 |
NoGetter |
防止蓝图为该属性生成一个"get"节点。只对稀疏类数据生效。 |