一、创建接口
二、创建C++类
三、实现接口
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "ReactToTriggerInterface.generated.h"
/*
声明接口类与声明普通的类相似,但是任有两个主要区别。
1、接口类使用UINTERFACE()而不是UCLASS()。
2、直接从UInterface而不是UObject继承。
接口分为四种:
无返回值 无参数 被蓝图当做事件 不能被实现
无返回值 带参数 被蓝图当做事件 不能被实现
有返回值 无参数 不能被当做事件 可以被实现
有返回值 带参数 不能备当做事件 可以被实现
说明符:
BlueprintCallable 可以在蓝图中调用
BlueprintImplementableEvent 在C++可以声明函数(不能定义,蓝图重载),在C++里调用该函数,蓝图重载实现该函数
BlueprintNativeEvent 在C++可以声明和定义函数,在C++里调用该函数,蓝图重载实现该函数(蓝图可以重载或不重载C++父类函数)
*/
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UReactToTriggerInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class INTERFACE_API IReactToTriggerInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
//不能当做事件接口定义
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
bool ReactToHighNoon();
//不能当做事件接口定义
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category = "LVCate")
bool ReactToMidnight();
//可以当做事件接口实现
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category = "LVCate")
void FuncAsImplementableEvent();
//可以当做事件接口实现
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsNativeEvent();
//可以当做事件接口实现
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsNativeEventWithParam(int value);
};
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ReactToTriggerInterface.h"
#include "AReactToTriggerCPPImp.generated.h"
UCLASS()
class INTERFACE_API AAReactToTriggerCPPImp : public AActor, public IReactToTriggerInterface
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AAReactToTriggerCPPImp();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//实现接口
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
bool ReactToHighNoon();
virtual bool ReactToHighNoon_Implementation() override; //必须定义和实现这个函数
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsNativeEvent();
virtual void FuncAsNativeEvent_Implementation() override;
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsNativeEventWithParam(int value);
virtual void FuncAsNativeEventWithParam_Implementation(int value) override;
//自己定义的函数
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category = "LVCate")
void FuncAsImplementableEventCPPImp();
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsNativeEventCPPImp();
void FuncAsNativeEventCPPImp_Implementation();
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsNativeEventWithParamCPPImp(int value);
void FuncAsNativeEventWithParamCPPImp_Implementation(int value);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "AReactToTriggerCPPImp.h"
// Sets default values
AAReactToTriggerCPPImp::AAReactToTriggerCPPImp()
{
// 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;
}
// Called when the game starts or when spawned
void AAReactToTriggerCPPImp::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AAReactToTriggerCPPImp::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
bool AAReactToTriggerCPPImp::ReactToHighNoon_Implementation()
{
this->FuncAsNativeEventCPPImp();
return true;
}
void AAReactToTriggerCPPImp::FuncAsNativeEvent_Implementation()
{
}
void AAReactToTriggerCPPImp::FuncAsNativeEventWithParam_Implementation(int value)
{
}
void AAReactToTriggerCPPImp::FuncAsNativeEventCPPImp_Implementation()
{
}
void AAReactToTriggerCPPImp::FuncAsNativeEventWithParamCPPImp_Implementation(int value)
{
}
四、在蓝图中调用接口
将新建的C++类拖到场景当中,选中添加蓝图,命名为AReactToTriggerCPPImp1_Blueprint。
打开AReactToTriggerCPPImp1_Blueprint,可以看到无返回值函数作为事件,不能被实现。
可以看到不能被当做事件,但是可以被实现的函数。
添加事件
实现ReactToMidnight、ReactToHighNoon
新建一个新的蓝图命名为NewBlueprint,拖入场景中,并打开蓝图。
新建一个变量React
点击场景中的NewBlueprint
返回蓝图中
运行可以看到屏幕上的打印
运行结果
在C++类中调用接口
在AAReactToTriggerCPPImp.cpp中添加代码
// Fill out your copyright notice in the Description page of Project Settings.
#include "AReactToTriggerCPPImp.h"
#include "Misc\AssertionMacros.h"
#include <Engine.h>
DEFINE_LOG_CATEGORY_STATIC(LV, Log, All);
// Sets default values
AAReactToTriggerCPPImp::AAReactToTriggerCPPImp()
{
// 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;
}
// Called when the game starts or when spawned
void AAReactToTriggerCPPImp::BeginPlay()
{
Super::BeginPlay();
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "BeginPlay");
this->Execute_ReactToHighNoon(this);//运行蓝图中的ReactToHighNoon函数
}
// Called every frame
void AAReactToTriggerCPPImp::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
bool AAReactToTriggerCPPImp::ReactToHighNoon_Implementation()
{
UE_LOG(LV, Warning, TEXT("ReactToHighNoon_Implementation"));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "ReactToHighNoon_Implementation");
this->Execute_ReactToMidnight(this);//运行蓝图中的ReactToMidnight函数
//this->FuncAsNativeEventCPPImp();
return true;
}
void AAReactToTriggerCPPImp::FuncAsNativeEvent_Implementation()
{
}
void AAReactToTriggerCPPImp::FuncAsNativeEventWithParam_Implementation(int value)
{
}
void AAReactToTriggerCPPImp::FuncAsNativeEventCPPImp_Implementation()
{
}
void AAReactToTriggerCPPImp::FuncAsNativeEventWithParamCPPImp_Implementation(int value)
{
}
编译完后运行结果如下
创建C++类,命名MyActor。
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AReactToTriggerCPPImp.h"
#include "MyActor.generated.h"
UCLASS()
class INTERFACE_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void IteratorCall(); //迭代调用
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
#include <Engine.h>
#include <EngineUtils.h>
// Sets default values
AMyActor::AMyActor()
{
// 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;
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
IteratorCall();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMyActor::IteratorCall()
{
TActorIterator<AAReactToTriggerCPPImp> actorIt = TActorIterator<AAReactToTriggerCPPImp>(GetWorld()); //获得迭代器
//遍历迭代器
for (actorIt; actorIt; ++actorIt)
{
if (!actorIt)
{
continue;
}
AAReactToTriggerCPPImp* actor = *actorIt;
bool isImplementation = actor->GetClass()->ImplementsInterface(UReactToTriggerInterface::StaticClass());
if (isImplementation)
{
IReactToTriggerInterface* inter = Cast<IReactToTriggerInterface>(actor);
inter->Execute_ReactToHighNoon(actor);
}
}
}
将MyActor拖入场景中并运行,结果如下。