UE4 Delegate

UE4的委托 可以大致分为3种形式 这里以一个参数的函数指针作为例子

一.正常委托

DECLARE_DELEGATE_OneParam 

绑定 UObject成员函数  NormalDelegate.BindUObject(this, &AMyActor::TestLog);

执行委托  NormalDelegate.ExecuteIfBound

二.动态委托 

相比正常委托 效率会有一些降低

DECLARE_DYNAMIC_DELEGATE_OneParam

绑定 UObject成员函数  NormalDelegate.BindUObject(this, &AMyActor::TestLog_Func);  这里 动态委托只能绑定UFunction的函数

执行委托   DynamicDelegate.ExecuteIfBound

三.多组委托

执行委托    Broadcast函数

优点:可以执行多个绑定函数

缺点:无法绑定带有返回值的函数指针

DECLARE_MULTICAST_DELEGATE_OneParam

绑定UObject成员函数   AddUObject(this, &AMyActor::TestLog);

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam

绑定UObject成员函数  这里稍微有些复杂

第一种是可以通过声明UPROPERTY(BlueprintAssignable)然后在蓝图中进行动态绑定

第二种MultiDynamicDelegate.AddDynamic(this, &AMyActor::TestLog_Func)

第三种通过代码的方式的话是

TScriptDelegate<> temp;
	temp.BindUFunction(this, STATIC_FUNCTION_FNAME(TEXT("AMyActor::TestLog_Func")));
	MultiDynamicDelegate.Add(temp);
发布了144 篇原创文章 · 获赞 15 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/maxiaosheng521/article/details/103185059
UE4