UE5 C++中的单播、多播、动态多播及的创建使用方法附带(%s/%d/%f)

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyDelegateActor.generated.h"

//单播代理(委托),绑定一个函数
DECLARE_DELEGATE(NoParamDelegate);
DECLARE_DELEGATE_OneParam(OneParamDelegate,FString);
DECLARE_DELEGATE_TwoParams(TwoParamDelegate,FString,int32);
DECLARE_DELEGATE_ThreeParams(ThreeParamDelegate,FString,int32,float);
DECLARE_DELEGATE_RetVal(FString,RevalDelegate);
//多播代理(委托),绑定多个函数
DECLARE_MULTICAST_DELEGATE_OneParam(OneParamMultiDelegate,FString);
//动态多播代理(委托),可以暴露给蓝图,在蓝图中进行事件绑定
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDynamicMulticast,FString,Param);

UCLASS()
class BUTTONCTEST_API AMyDelegateActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyDelegateActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	//单播代理(委托)
	NoParamDelegate NoParamDelegate;
	OneParamDelegate OneParamDelegate;
	TwoParamDelegate TwoParamDelegate;
	ThreeParamDelegate ThreeParamDelegate;
	RevalDelegate RevalDelegate;

	void NoParamFunction();
	void OneParamFunction(FString str);
	void TwoParamFunction(FString str,int32 Value);
	void ThreeParamFunction(FString str,int32 Value,float Valuel);
	FString RevalParamFunction();

	//多播代理(委托),绑定多个函数
	OneParamMultiDelegate OneParamMultiDelegate;

	void MultiDelegateFunction01(FString str);
	void MultiDelegateFunction02(FString str);
	void MultiDelegateFunction03(FString str);

	//动态多播代理(委托),可以暴露给蓝图,在蓝图中进行事件绑定
	UPROPERTY(BlueprintAssignable)
	FDynamicMulticast DynamicMulticast;
	
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyDelegateActor.h"

// Sets default values
AMyDelegateActor::AMyDelegateActor()
{
 	// 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;
	//单播代理绑定
	NoParamDelegate.BindUObject(this,&AMyDelegateActor::NoParamFunction);
	OneParamDelegate.BindUObject(this,&AMyDelegateActor::OneParamFunction);
	TwoParamDelegate.BindUObject(this,&AMyDelegateActor::TwoParamFunction);
	ThreeParamDelegate.BindUObject(this,&AMyDelegateActor::ThreeParamFunction);
	RevalDelegate.BindUObject(this,&AMyDelegateActor::RevalParamFunction);

	//多播代理绑定
	OneParamMultiDelegate.AddUObject(this,&AMyDelegateActor::MultiDelegateFunction01);
	OneParamMultiDelegate.AddUObject(this,&AMyDelegateActor::MultiDelegateFunction02);
	OneParamMultiDelegate.AddUObject(this,&AMyDelegateActor::MultiDelegateFunction03);

	
}

// Called when the game starts or when spawned
void AMyDelegateActor::BeginPlay()
{
	Super::BeginPlay();

	//单播代理执行
	NoParamDelegate.ExecuteIfBound();
	OneParamDelegate.ExecuteIfBound("This One");
	TwoParamDelegate.ExecuteIfBound("This Two",2);
	ThreeParamDelegate.ExecuteIfBound("This Three",3,3.0);
	FString strValue = RevalDelegate.Execute();

	//多播代理执行
	OneParamMultiDelegate.Broadcast("OneParamMuliiDelegate");

	//执行动态多播代理,绑定在蓝图中进行
	DynamicMulticast.Broadcast("DynamicMulticast");
}

// Called every frame
void AMyDelegateActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void AMyDelegateActor::NoParamFunction()
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Blue,TEXT("NoParamDelegate"));
}

void AMyDelegateActor::OneParamFunction(FString str)
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Blue,FString::Printf(TEXT("%s"),*str));
}

void AMyDelegateActor::TwoParamFunction(FString str, int32 Value)
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Blue,FString::Printf(TEXT("%s %d"),*str,Value));
}

void AMyDelegateActor::ThreeParamFunction(FString str, int32 Value, float Valuel)
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Blue,FString::Printf(TEXT("%s %d %f"),*str,Value,Valuel));
}

FString AMyDelegateActor::RevalParamFunction()
{
	FString str = FString::Printf(TEXT("RevalParamDelegate"));
	return str;
}

void AMyDelegateActor::MultiDelegateFunction01(FString str)
{
	FString TmpStr = str.Append("1");
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::Printf(TEXT("%s"),*TmpStr));
}

void AMyDelegateActor::MultiDelegateFunction02(FString str)
{
	FString TmpStr = str.Append("2");
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::Printf(TEXT("%s"),*TmpStr));
}

void AMyDelegateActor::MultiDelegateFunction03(FString str)
{
	FString TmpStr = str.Append("3");
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::Printf(TEXT("%s"),*TmpStr));
}

UE5 C++中的单播、多播、动态多播及的创建使用方法附带(%s/%d/%f)_第1张图片 

 

你可能感兴趣的:(ue5,c++,java)