UE4 C++ 创建一个接口

8 篇文章 1 订阅

接口的创建不需要通过编辑器,可以直接使用IDE将接口的.h和.cpp创建到项目目录下的Source文件夹下的工程名文件夹里。Source->项目名文件夹。

这里创建一个简单的空的接口MyInterface
MyInterface.h

#pragma once

#include "MyInterface.generated.h"

UINTERFACE()
class MyProjectName_API UMyInterface : public UInterface
{
	GENERATED_BODY()
};

class MyProjectName_API IMyInterface
{
	GENERATED_BODY()
	public:
	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Interactable)
		bool CanInteract();
	virtual bool CanInteract_Implementation();
	
	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Interactable)
		void PerformInteract();
	virtual void PerformInteract_Implementation();
};

UMyInterface并不是我们需要的接口,IMyInterface才是,UMyInterface的存在是因为Unreal Engine 4的反射系统的需要。

the UINTERFACE class is not the actual interface; it is an empty class that exists only for visibility to Unreal Engine’s reflection system. The actual interface that will be inherited by other classes must have the same class name, but with the initial “U” changed to an “I”.
https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/Reference/Interfaces#determiningifagivenclassimplementsyourinterface

标记为BlueprintNativeEvent的函数,既可以在C++中被覆盖,也可以在蓝图中覆盖,如果C++中也覆盖了,蓝图中也覆盖了,以蓝图的为主。
_Implementation()为对应函数的默认实现。

MyInterface.cpp

#include "MyInterface.h"
bool IMyInterface::CanInteract_Implementation()
{
	return true;
}

void IMyInterface::PerformInteract_Implementation()
{

}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值