UPROPERTY宏、属性说明符和元数据说明符
UPROPERTY宏
UPROPERTY
宏:把C++里的变量暴露给蓝图,实现在蓝图中访问C++变量,达到C++和蓝图通信的效果
属性说明符
1 2 3 4 5 6 7 8 9
| UPROPERTY(VisibleDefaultsOnly) int32 VisibleDefaultsOnlyInt;
UPROPERTY(VisibleInstanceOnly) FString VisibleInstanceOnlyString;
UPROPERTY(VisibleAnywhere) FVector VisibleAnywhereVector;
|
1 2 3 4 5 6 7 8 9
| UPROPERTY(EditDefaultsOnly) int32 EditDefaultsOnly;
UPROPERTY(EditInstanceOnly) FString EditInstanceOnlyString;
UPROPERTY(EditAnywhere) FVector EditAnywhereVector;
|
1 2 3 4 5 6 7
| UPROPERTY(EditAnywhere,BlueprintReadOnly) int32 BlueprintReadOnlyInt;
UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 BlueprintReadWriteInt;
|

1 2 3 4 5 6
| UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyIntValue") int32 Value1; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyIntValue|MySubIntValue") int32 Value2;
|


元数据说明符
1 2 3 4 5
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisPlayName = "MyValue3DisplayName")) int32 MyValue3;
|

1 2 3 4 5 6
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisPlayName = "Controller")) bool isController; UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition = "isController")) float Value3;
|


1 2 3
| UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (ToolTip = "isControllerTrue")) bool isTrue;
|

UFUNCTION宏、属性说明符和元数据说明符
UFUNCTION
宏:生成函数的反射数据,将函数暴露给蓝图,实现函数和蓝图之间的通信
属性说明符
BlueprintCallable
:暴露我们的函数在蓝图中可调用
BlueprintPure
:纯虚函数的定义
1 2 3 4 5 6
| UFUNCTION(BlueprintCallable, Category = "My Function") void PrintF1();
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "My Function") bool PrintF2();
|
有返回值vs无返回值


BlueprintImplementableEvent
:在C++中声明,不能定义,蓝图可重载
1 2 3 4 5 6 7 8 9
| UFUNCTION(BlueprintImplementableEvent) void Test1(); UFUNCTION(BlueprintImplementableEvent) int Test2(); UFUNCTION(BlueprintImplementableEvent) void Test3(const FString &MyString); UFUNCTION(BlueprintImplementableEvent) int Test4(const FString& MyString);
|
BlueprintNativeEvent
:在C++中声明和实现,蓝图可重载或者不重载
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| UFUNCTION(BlueprintNativeEvent) void TestA(); UFUNCTION(BlueprintNativeEvent) int TestB(); UFUNCTION(BlueprintNativeEvent) void TestC(const FString &mystring); UFUNCTION(BlueprintNativeEvent) int TestD(const FString& mystring);
void AMyPawn::TestA_Implementation() { GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("TestA")); }
|
示例:在 BeginPlay
中调用 TestA
, 在蓝图中重载 TestA

运行结果:

元数据说明符
DisplayName
:为函数起别名
1 2 3
| UFUNCTION(BlueprintCallable, Category = "My Function", meta = (DisplayName = "MyPrintTest")) void PrintTest();
|
