一.创建接口
声明Attack() 和 Calculatehealth()虚函数
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.头文件设置 添加
#include "MyInterface.h"
class PRACTICEC_API AMyCharacter : public ACharacter,public IMyInterface
//重写接口函数virtual void Attack() override;virtual void CalculateHealth() override;
2.在Character.cpp简单实现, 并在 BeginPlay里调用这两个函数
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.效果如下