0%

UE(4):UPROPERTY宏、UFUNCTION宏

UPROPERTY宏、属性说明符和元数据说明符

UPROPERTY宏

UPROPERTY宏:把C++里的变量暴露给蓝图,实现在蓝图中访问C++变量,达到C++和蓝图通信的效果

属性说明符

1
2
3
4
5
6
7
8
9
// 仅在类默认设置可见
UPROPERTY(VisibleDefaultsOnly) // 虚幻C++生成反射数据,在蓝图中可以找到相应的变量
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;
// BlueprintReadOnlyInt只能获取,BlueprintReadWriteInt既可获取又能设置

image-20240820173600330

1
2
3
4
5
6
// Category属性目录
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyIntValue")
int32 Value1;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyIntValue|MySubIntValue")
int32 Value2;
// 为变量创建目录

image-20240820174959387

image-20240820175106272

元数据说明符

1
2
3
4
5
// meta元数据说明符
// DisPlayName别名:将变量用另一个别名替换
// MyValue3被MyValue3DisplayName替换
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisPlayName = "MyValue3DisplayName"))
int32 MyValue3;

image-20240820175451727

1
2
3
4
5
6
// EditCondition条件控制编辑
// 只有当Controller为真时,Value3才可编辑
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisPlayName = "Controller"))
bool isController;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition = "isController"))
float Value3;

image-20240820180119115

image-20240820180123296

1
2
3
// Tooltip解释说明我们的变量
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (ToolTip = "isControllerTrue"))
bool isTrue;

image-20240820180451588

UFUNCTION宏、属性说明符和元数据说明符

UFUNCTION宏:生成函数的反射数据,将函数暴露给蓝图,实现函数和蓝图之间的通信

属性说明符

BlueprintCallable暴露我们的函数在蓝图中可调用

BlueprintPure纯虚函数的定义

1
2
3
4
5
6
// 暴露我们的函数在蓝图中可调用
UFUNCTION(BlueprintCallable, Category = "My Function")
void PrintF1();
// 纯虚函数的定义BlueprintPure
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "My Function")
bool PrintF2();

有返回值vs无返回值

  • 有返回值的是一个函数,无返回值的是一个事件

image-20240820205201302image-20240820205206787

BlueprintImplementableEvent在C++中声明,不能定义,蓝图可重载

1
2
3
4
5
6
7
8
9
// BlueprintImplementableEvent:在C++中声明,不能定义,蓝图可重载
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
// BlueprintNativeEvent:在C++中声明和实现,蓝图可重载或者不重载
UFUNCTION(BlueprintNativeEvent)
void TestA();
UFUNCTION(BlueprintNativeEvent)
int TestB();
UFUNCTION(BlueprintNativeEvent)
void TestC(const FString &mystring);
UFUNCTION(BlueprintNativeEvent)
int TestD(const FString& mystring);
// 实现时函数名=原函数名+_Implementation
void AMyPawn::TestA_Implementation()
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("TestA"));
}

示例:在 BeginPlay 中调用 TestA, 在蓝图中重载 TestA

image-20240821124658647

运行结果:

image-20240821124837582

元数据说明符

DisplayName:为函数起别名

1
2
3
// 元数据说明符meta
UFUNCTION(BlueprintCallable, Category = "My Function", meta = (DisplayName = "MyPrintTest"))
void PrintTest();

image-20240821125656504