F5.UPROPERTY
大约 4 分钟
UPROPERTY定义
//声明一个变量
int32 MyInt;
//定义一个变量
int32 MyInt = 0;
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyTest.generated.h"
UCLASS()
class EXORCIST_API AMyTest : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(VisibleAnywhere,BlueprintReadOnly)
int32 VisibleAnywhereNumber;
UPROPERTY(VisibleDefaultsOnly,BlueprintReadOnly)
int32 VisibleDefaultsOnly;
UPROPERTY(VisibleInstanceOnly,BlueprintReadOnly)
int32 VisibleInstanceOnly;
UPROPERTY(EditAnywhere,BlueprintReadWrite)
int32 EditAnywhereNumber;
UPROPERTY(EditDefaultsOnly)
int32 EditDefaultsOnlyNumber;
};
属性说明符

VisibleAnywhere
&BlueprintReadOnly
该属性在所有属性窗口中可见,但不可编辑,需配合BlueprintReadOnly。
UPROPERTY(VisibleAnywhere,BlueprintReadOnly)
int32 VisibleAnywhereNumber;
VisibleDefaultsOnly
& BlueprintReadOnly
该属性在所有属性窗口中可见,但只能在类的默认属性面板中编辑。在蓝图中是只读的。
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly)
int32 VisibleDefaultsOnlyNumber;
VisibleInstanceOnly
& BlueprintReadOnly
该属性在所有属性窗口中可见,但只能在每个实例的属性面板中编辑。在蓝图中是只读的。
UPROPERTY(VisibleInstanceOnly, BlueprintReadOnly)
int32 VisibleInstanceOnlyNumber;
EditAnywhere
&BlueprintReadWrite
该属性在所有属性窗口中可见且可编辑。在蓝图中可以读写。
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 EditAnywhereNumber;
EditDefaultsOnly
该属性在所有属性窗口中可见,但只能在类的默认属性面板中编辑。在蓝图中可以读写。
UPROPERTY(EditDefaultsOnly)
int32 EditDefaultsOnlyNumber;
DeterminesOutputType
动态输出类型

UFUNCTION(BlueprintCallable, Category="Actor", meta=(WorldContext="WorldContextObject", DeterminesOutputType="ActorClass"))
static ENGINE_API class AActor* GetActorOfClass(const UObject* WorldContextObject, TSubclassOf<AActor> ActorClass);
AActor* UGameplayStatics::GetActorOfClass(const UObject* WorldContextObject, TSubclassOf<AActor> ActorClass)
{
QUICK_SCOPE_CYCLE_COUNTER(UGameplayStatics_GetActorOfClass);
// We do nothing if no is class provided
if (ActorClass)
{
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
{
for (TActorIterator<AActor> It(World, ActorClass); It; ++It)
{
AActor* Actor = *It;
return Actor;
}
}
}
return nullptr;
}
UDataStrategyBase* UUserDataSubsystem::FindDataStrategy(FName StrategyName)
{
UDataStrategyBase* StrategyInstance = RegisteredStrategies.FindRef(StrategyName);
return StrategyInstance;
}
首先当然是C++的T模板
template <typename T>
T* UUserDataSubsystem::FindDataStrategyAs(FName StrategyName)
{
UDataStrategyBase* BaseInstance = FindDataStrategy(StrategyName);
return Cast<T>(BaseInstance); // 直接通过Cast返回派生类指针
}
其次是蓝图节点的改造,通过
DeterminesOutputType
public:
UFUNCTION(BlueprintCallable, Category="DataStrategy", meta = (DeterminesOutputType = "StrategyClass"))
UDataStrategyBase* FindDataStrategyByClass(FName StrategyName, TSubclassOf<UDataStrategyBase> StrategyClass)
{
UDataStrategyBase* StrategyInstance = FindDataStrategy(StrategyName);
if (StrategyInstance && StrategyInstance->IsA(StrategyClass))
{
return StrategyInstance;
}
return nullptr;
}
//将UDataStrategyBase* FindDataStrategy(FName StrategyName);移到私有
private:
UDataStrategyBase* FindDataStrategy(FName StrategyName);
Category
Category|分组
UPROPERTY(EditAnywhere, Category="分组")
int32 Category;
UPROPERTY(EditAnywhere, Category="分组|子标签")
int32 Child;
ShowOnlyInnerProperties
内联显示属性
USTRUCT()
struct FCat
{
GENERATED_BODY()
UPROPERTY(EditDefaultsOnly)
FString Name;
UPROPERTY(EditDefaultsOnly)
int32 Age;
UPROPERTY(EditDefaultsOnly)
FLinearColor Color;
};
//ShowOnlyInnerProperties
UPROPERTY(EditAnywhere, Category="ShowOnlyInnerProperties")
FCat NormalProperties;//正常显示
UPROPERTY(EditAnywhere, Category="ShowOnlyInnerProperties", meta=(ShowOnlyInnerProperties))
FCat ShowOnlyInnerProperties;//内联显示

Tooltip
提示文本
UPROPERTY(EditAnywhere, meta=(ToolTip="提示文本"))
int32 Tooltip;
DisplayName
显示名称
UPROPERTY(EditAnywhere, meta=(DisplayName="Display Font"))
FSoftObjectPath DisplayName;
AdvancedDisplay
高级显示,属性隐藏在下拉菜单中
UPROPERTY(EditAnywhere, Category="AdvancedDisplay")
bool NormalDisplay1;
UPROPERTY(EditAnywhere, Category="AdvancedDisplay")
bool NormalDisplay2;
UPROPERTY(EditAnywhere, Category="AdvancedDisplay", AdvancedDisplay)
bool AdvancedDisplay;
EditCondition
属性条件编辑
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PackageName")
bool bAsPackageName = false;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PackageName",meta=(EditCondition="bAsPackageName")
FString PackageNamePrefix;

EditConditionHides
隐藏属性
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PackageName")
bool bAsPackageName = false;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PackageName",meta=(EditCondition="bAsPackageName",EditConditionHides))
FString PackageNamePrefix;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "PackageName",meta=(EditCondition="bAsPackageName",EditConditionHides))
FString PackageNameSuffix;
UPROPERTY(BlueprintReadOnly,VisibleAnywhere,Category = "PackageName",meta=(EditCondition="bAsPackageName",EditConditionHides))
FString PackageNamePreview;
InlineEditConditionToggle
内联属性
UPROPERTY(EditAnywhere, meta=(InlineEditConditionToggle))
bool bCanFly;
UPROPERTY(EditAnywhere, meta=(EditCondition="bCanFly", Units="s"))
float InlineEditConditionToggle;