UE4 C++ 创建一个接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012801153/article/details/86076885

接口的创建不需要通过编辑器,可以直接使用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()
{

}

猜你喜欢

转载自blog.csdn.net/u012801153/article/details/86076885