UE5 C++ Interface接口

一.创建接口

声明Attack() 和 Calculatehealth()虚函数

cpp 复制代码
UINTERFACE(MinimalAPI)
class UMyInterface : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class PRACTICEC_API IMyInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	virtual void Attack() {};
	virtual void CalculateHealth() {};
};

二.角色的C++类里继承接口

1.头文件设置 添加

cpp 复制代码
#include "MyInterface.h"  
cpp 复制代码
class PRACTICEC_API AMyCharacter : public ACharacter,public IMyInterface
cpp 复制代码
	//重写接口函数
	virtual void Attack() override;
	virtual void CalculateHealth() override;

2.在Character.cpp简单实现, 并在 BeginPlay里调用这两个函数

cpp 复制代码
void AMyCharacter::Attack()
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("Attack"));
}

void AMyCharacter::CalculateHealth()
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("Calculate "));
}

3.效果如下

相关推荐
四代水门6 天前
UE5实现客户端与服务器时间同步
ue5
智海深蓝7 天前
海上平行战场:态势模拟三维可视化平台
3d·ue5
_守一8 天前
UE5 ListView记录
ue5
成都渲染101云渲染66669 天前
CR15新功能介绍以及CR15云渲染流程
ue5·图形渲染·blender·maya·corona
一锅炖出任易仙10 天前
创梦汤锅学习日记day30
学习·ai·ue5·游戏引擎
OSwich10 天前
【UE5学习笔记】UMG中控件命名规范
笔记·学习·ue5
一锅炖出任易仙11 天前
创梦汤锅学习日记day29
学习·ai·ue5·游戏引擎
吴梓穆11 天前
UE5 C++ 注册 开始重叠和结束重叠事件
开发语言·c++·ue5
妙为11 天前
unreal engine5(UE5)中使用Rider
ue5·游戏引擎·虚幻·rider
归真仙人13 天前
【UE】LineTraceByProfile
ue5·游戏引擎·ue4·unreal engine