蓝图接口和C++、Java中的接口类似,都是用来被继承的,并且可以实现相应的功能。
- 使用UE新建一个第一人称游戏来实现接口实例:TestInterface
2. 新建一个C++的接口类:HittedInterface
- 打开VS2019编辑C++代码
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "HittedInterface.generated.h"
// This class does not need to be modified.
/**
class UHittedInterface 这个类是为了在UE中使用反射系统,不需要处理
*/
UINTERFACE(MinimalAPI)
class UHittedInterface : public UInterface {
GENERATED_BODY()
};
/**
*
*/
class TESTINTERFACE_API IHittedInterface {
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
/**
BlueprintNativeEvent除了实现C++调用蓝图外,同样会调用一个本地方法,本地方法的实现为 声明的函数名+_Implementation
在实现中应该是:
void hitByProjectile_Implementation()override;
这样就可以实现一次调用,两个实现(c++实现,蓝图实现)
*/
UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
void hitByProjectile();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "HittedInterface.h"
// Add default functionality here for any IHittedInterface functions that are not pure virtual.
- 新建一个Actord类HittedActor继承自接口IHittedInterface
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "HittedInterface.h"
#include "GameFramework/Actor.h"
#include "HittedActor.generated.h"
UCLASS()
class TESTINTERFACE_API AHittedActor : public AActor, public IHittedInterface {
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AHittedActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
public:
void hitByProjectile_Implementation()override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "HittedActor.h"
#include "Kismet/KismetSystemLibrary.h"
// Sets default values
AHittedActor::AHittedActor() {
// 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 AHittedActor::BeginPlay() {
Super::BeginPlay();
}
// Called every frame
void AHittedActor::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
}
void AHittedActor::hitByProjectile_Implementation() {
//在HUD界面显示内容
GEngine->AddOnScreenDebugMessage(-1, 2.0, FColor::Red, UKismetSystemLibrary::GetDisplayName(this) + TEXT(" hitting..."));
}
- 在子弹击中后调用这个函数,在类ATestInterfaceProjectile中实现
void ATestInterfaceProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) {
//Actor被子弹击中后,调用接口
IHittedInterface* hitedActor = Cast<IHittedInterface>(OtherActor);
if (hitedActor) {
hitedActor->Execute_hitByProjectile(OtherActor);
}
// Only add impulse and destroy projectile if we hit a physics
if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) && OtherComp->IsSimulatingPhysics()) {
OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());
Destroy();
}
}
- 在UEEditor 中创建AHittedActor的蓝图类BP_HittedActor
在蓝图中添加一个立方体
保存以后,把BP_HittedActor拖动到场景中
可以在跑【类设置】中查看接口继承情况
- 运行程序
aaa