概述
参考:
https://docs.unrealengine.com/zh-CN/Programming/UnrealArchitecture/Reference/Interfaces/index.html
接口的模式比较固定,下面就上我定义的接口
// 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"
// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UReactToTriggerInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class PLUGINS_ME_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, BlueprintNativeEvent, Category = "LVCate")
void FuncAsEventNativeEvent();
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category = "LVCate")
void FuncAsEventImplementableEvent();
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsEventNativeEventWithParam(int value);
};
解释一下概率:
BlueprintNativeEvent :这个关键字,说明这个函数既可以蓝图重写,也可以C++重新
BlueprintImplementableEvent:这个关键字,说明这个函数只能蓝图重新
可以写一个C++类继承接口:
// 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 "ReactToTriggerCppImp.generated.h"
UCLASS()
class PLUGINS_ME_API AReactToTriggerCppImp : public AActor,public IReactToTriggerInterface
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AReactToTriggerCppImp();
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 FuncAsEventNativeEvent();
virtual void FuncAsEventNativeEvent_Implementation() override;
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsEventNativeEventWithParam(int value);
virtual void FuncAsEventNativeEventWithParam_Implementation(int value) override;
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category = "LVCate")
void FuncAsEventCPPImp();
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsEventNativeEventCPPImp();
void FuncAsEventNativeEventCPPImp_Implementation();
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "LVCate")
void FuncAsEventNativeEventWithParamCPPImp(int value);
void FuncAsEventNativeEventWithParamCPPImp_Implementation(int value);
};
然后在蓝图里面实现一个actor 继承之新写的C++实体类
蓝图实现一个函数可以用如下的脚本:
再看下面的概念:这里的的函数,是需要在蓝图进行重载的函数
另外,蓝图调用函数有两种方法,一种是事件方式,一种是函数方式
*****************************************20200821*****************************
今天使用发现了一个大问题:BlueprintImplementableEvent
这个声明的函数不能包含参数
上面这种形式是不对的,会导致编译出错。
但是如果写成下面的形式,却是OK 的
UFUNCTION(BlueprintImplementableEvent, Category = "MqttFun")
void MessageArrivedCallBack(const FString &topic,const FString &message);
最后
以上就是沉默自行车为你收集整理的UE4之接口的全部内容,希望文章能够帮你解决UE4之接口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复