(UE4 4.20)UE4 UCLASS,UENUM, USTRUCT, UPROPERTY 的 常用配置

  • Post author:
  • Post category:其他


总结下我在项目中 “UCLASS,UENUM,  USTRUCT,  UPROPERTY”的常用配置, 持续更新

UCLASS

meta=(BlueprintSpawnableComponent)

让一个ActorComponent变为蓝图可挂载组件

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT2_API UMyActorComponent : public UActorComponent

Blueprintable

让一个UObject可以创建为蓝图BP类

UCLASS(Blueprintable)
class MYPROJECT2_API UTestObject : public UObject
{
	GENERATED_BODY()
	
public:
	UPROPERTY(EditAnywhere)
		float a;
};

UENUM

作为C++枚举的宏

UENUM()
enum class ECharactermMovementStatus : uint8
{
	Run,
	JUMP,
};
UCLASS(Blueprintable)
class MYPROJECT2_API UTestObject : public UObject
{
	GENERATED_BODY()
	
public:
	UPROPERTY(EditAnywhere)
		float a;

	UPROPERTY(EditAnywhere)
		ECharactermMovementStatus CharactermMovementStatus;
};

USTRUCT

C++结构体的宏

正常嵌套在UObject中蓝图化

USTRUCT()
struct FTest 
{
	GENERATED_BODY()

	UPROPERTY(EditAnywhere)
	float a;

	UPROPERTY(EditAnywhere)
	float b;
};
/**
 * 
 */
UCLASS(Blueprintable)
class MYPROJECT2_API UTestObject : public UObject
{
	GENERATED_BODY()
	
public:
	UPROPERTY(EditAnywhere)
		FTest Test;
};


DataTable

做成CSV属性配置表


#include "Engine/DataTable.h"


USTRUCT()
struct FTest : public FTableRowBase
{
	GENERATED_BODY()

	UPROPERTY(EditAnywhere)
	float a;

	UPROPERTY(EditAnywhere)
	float b;
};

UPROPERTY

关于UPROPERTY的枚举都定义在ObjectMacros.h

namespace UP
{
	// valid keywords for the UPROPERTY macro
	enum 
	{
		/// This property is const and should be exported as const.
		Const,

		/// Property should be loaded/saved to ini file as permanent profile.
		Config,

		/// Same as above but load config from base class, not subclass.
		GlobalConfig,

		/// Property should be loaded as localizable text. Implies ReadOnly.
		Localized,

		/// Property is transient: shouldn't be saved, zero-filled at load time.
		Transient,

		/// Property should always be reset to the default value during any type of duplication (copy/paste, binary duplication, etc.)
		DuplicateTransient,

		/// Property should always be reset to the default value unless it's being duplicated for a PIE session - deprecated, use NonPIEDuplicateTransient instead
		NonPIETransient,

		/// Property should always be reset to the default value unless it's being duplicated for a PIE session
		NonPIEDuplicateTransient,

		/// Value is copied out after function call. Only valid on function param declaration.
		Ref,

		/// Object property can be exported with it's owner.
		Export,

		/// Hide clear (and browse) button in the editor.
		NoClear,

		/// Indicates that elements of an array can be modified, but its size cannot be changed.
		EditFixedSize,

		/// Property is relevant to network replication.
		Replicated,

		/// Property is relevant to network replication. Notify actors when a property is replicated (usage: ReplicatedUsing=FunctionName).
		ReplicatedUsing,

		/// Skip replication (only for struct members and parameters in service request functions).
		NotReplicated,

		/// Interpolatable property for use with matinee. Always user-settable in the editor.
		Interp,

		/// Property isn't transacted.
		NonTransactional,

		/// Property is a component reference. Implies EditInline and Export.
		Instanced,

		/// MC Delegates only.  Property should be exposed for assigning in blueprints.
		BlueprintAssignable,

		/// Specifies the category of the property. Usage: Category=CategoryName.
		Category,

		/// Properties appear visible by default in a details panel
		SimpleDisplay,

		/// Properties are in the advanced dropdown in a details panel
		AdvancedDisplay,

		/// Indicates that this property can be edited by property windows in the editor
		EditAnywhere,

		/// Indicates that this property can be edited by property windows, but only on instances, not on archetypes
		EditInstanceOnly,

		/// Indicates that this property can be edited by property windows, but only on archetypes
		EditDefaultsOnly,

		/// Indicates that this property is visible in property windows, but cannot be edited at all
		VisibleAnywhere,
		
		/// Indicates that this property is only visible in property windows for instances, not for archetypes, and cannot be edited
		VisibleInstanceOnly,

		/// Indicates that this property is only visible in property windows for archetypes, and cannot be edited
		VisibleDefaultsOnly,

		/// This property can be read by blueprints, but not modified.
		BlueprintReadOnly,

		/// This property has an accessor to return the value. Implies BlueprintReadOnly if BlueprintSetter or BlueprintReadWrite is not specified. (usage: BlueprintGetter=FunctionName).
		BlueprintGetter,

		/// This property can be read or written from a blueprint.
		BlueprintReadWrite,

		/// This property has an accessor to set the value. Implies BlueprintReadWrite. (usage: BlueprintSetter=FunctionName).
		BlueprintSetter,

		/// The AssetRegistrySearchable keyword indicates that this property and it's 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.
		AssetRegistrySearchable,

		/// Property should be serialized for save game.
		SaveGame,

		/// MC Delegates only.  Property should be exposed for calling in blueprint code
		BlueprintCallable,

		/// MC Delegates only. This delegate accepts (only in blueprint) only events with BlueprintAuthorityOnly.
		BlueprintAuthorityOnly,

		/// Property shouldn't be exported to text format (e.g. copy/paste)
		TextExportTransient,

		/// Property shouldn't be serialized, can still be exported to text
		SkipSerialization,
	};
}

这里我主要讲有关属性反射到编辑器的,关于RPC在UPROPERTY的就不想讲了, 那篇有关RPC的博客就说过了。

EditAnywhere 与 Category

“EditAnywhere”可以任意在编辑器编辑值,Category 分类标签


UPROPERTY(EditAnywhere, Category = "abc")
	float a;

VisibleAnywhere

对于一般的 int, float, FString 等属性在编辑器可见,但不能编辑


UPROPERTY(VisibleAnywhere, Category = "abc")
	float a;

上面说“一般的” 是这样,但是我发现在Actor构造函数中创建的ActorComponent组件的属性想要编辑, 最好用VisibleAnywhere, 直接用EditAnywhere不是我们要的效果。

UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* StaitcMeshComponent;

AMyActor::AMyActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	StaitcMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaitcMeshComponent"));
	RootComponent = StaitcMeshComponent;

}

meta = (UIMin = “xxx1”, UIMax = “xxx2”)

实现把一个在编辑器的变量可编辑值从xxx1限制到xxx2, 如下所示:

	UPROPERTY(EditAnywhere, Category = "abc", meta = (UIMin = "0.0", UIMax = "1.0"))
		float a;

BlueprintReadOnly

变量在蓝图只读

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "abc", meta = (UIMin = "0.0", UIMax = "1.0"))
	float abc;

BlueprintReadWrite

变量在蓝图中能读又能写

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "abc", meta = (UIMin = "0.0", UIMax = "1.0"))
		float abc;

参考资料

[1]

https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Reference/Properties/Specifiers

[2]

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Classes/Specifiers/Abstract/index.html

[3]

https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Reference/Metadata



版权声明:本文为qq_29523119原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。