这个教程是从UE4 Wiki上整理而来.
在C++中直接使用Interface大家应该很熟悉。只是简单先定义一个个有虚函数的基类,然后在子类中实现相应的虚函数。像这样的虚函数的基类一般概念上叫接口。那接下来看看UE4中怎样在C++中定义接口的。
.h
#pragma once#include"TargetInterface.generated.h"UINTERFACE(MinimalAPI)classUTargetInterface :publicUInterface
{
GENERATED_UINTERFACE_BODY()
};classITargetInterface{
GENERATED_IINTERFACE_BODY()public:
UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "On Interact"))void OnInteract(bool bNewOpen);virtual floatGetHealth();
};
.cpp
#include "YourProject.h"#include"TargetInterface.h"UTargetInterface::UTargetInterface(const FObjectInitializer&ObjectInitializer)
:Super(ObjectInitializer)
{
}//Give GetHealth a default implementation
floatITargetInterface::GetHealth(){return 0.0f;
}
首先定