可以通过按下回车左面的 ’ 键来显示GameDebug 然后选中你要调试的actor 就能看到下面的界面
这里的6 MyCharacterDebug 是我自己自定义出来显示人物移动速度的调试信息 默认是没有的
下面说一下大概的步骤和实现方法
1.首先要自定义自己的FGameplayDebuggerCategory类 在里面实现数据的采集和绘制
2.在合适的位置 将Category类注册到IGameplayDebugger中
首先自定义Category类 这里由于代码中使用的是模板类,所有函数的定义不能和.h分离写在.cpp中 所以 以内联函数的方式写在了.inl文件中 具体代码中有注释
MyGameplayCategory.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#pragma once
#include "GameplayDebuggerCategory.h"
template <typename T>
class TESTFORDEBUG_API FMyGameplayCategory : public FGameplayDebuggerCategory
{
public:
FMyGameplayCategory();
static void Register(const FName& Name, int HotKey);
virtual void CollectData(APlayerController* OwnerPC, AActor* DebugActor) override;
virtual void DrawData(APlayerController* OwnerPC, FGameplayDebuggerCanvasContext& CanvasContext)override;
virtual void OnDataPackReplicated(int32 DataPackId)override;
protected:
static TSharedRef<FGameplayDebuggerCategory> MakeInstance();
//定义要显示的数据结构 必须定义Serialize函数
struct FDebugData
{
FString Str;
void Serialize(FArchive& archive);
} DebugData;
};
template<typename T>
inline void FMyGameplayCategory<T>::FDebugData::Serialize(FArchive & archive)
{
archive << Str;
}
#include "MyGameplayCategory.inl" //使用内联文件
MyGameplayCategory.inl
#include "MyGameplayCategory.h"
#include "GameplayDebugger.h"
template<typename T>
inline FMyGameplayCategory<T>::FMyGameplayCategory()
{
SetDataPackReplication(&DebugData);
}
template<typename T>
inline void FMyGameplayCategory<T>::Register(const FName& Name, int HotKey)
{
const auto STATE_FLAGS = EGameplayDebuggerCategoryState::EnabledInGame;
auto& debugger = IGameplayDebugger::Get();
auto creationDelegate = IGameplayDebugger::FOnGetCategory::CreateStatic(&FMyGameplayCategory<T>::MakeInstance); //创建一个FOnGetCategory类型的delegate指针
debugger.RegisterCategory(Name, creationDelegate, STATE_FLAGS, HotKey);
debugger.NotifyCategoriesChanged();
}
template<typename T>
inline void FMyGameplayCategory<T>::CollectData(APlayerController * OwnerPC, AActor * DebugActor)
{
DebugData.Str.Empty();
if (!DebugActor)
return;
for (UActorComponent* Component : DebugActor->GetComponents())
{
T* ComponentT = Cast<T>(Component);
if (ComponentT)
{
DebugData.Str = ComponentT->GetDebugStr();
}
}
}
template<typename T>
inline void FMyGameplayCategory<T>::DrawData(APlayerController * OwnerPC, FGameplayDebuggerCanvasContext & CanvasContext)
{
CanvasContext.Print(DebugData.Str); //输出数据
}
template<typename T>
inline void FMyGameplayCategory<T>::OnDataPackReplicated(int32 DataPackId)
{
MarkRenderStateDirty(); //表示数据同步完毕需要重新渲染
}
template<typename T>
inline TSharedRef<FGameplayDebuggerCategory> FMyGameplayCategory<T>::MakeInstance()
{
return MakeShareable(new FMyGameplayCategory<T>()); //创建实例
}
然后是Category的注册调用 这里是写在了Character构造函数中 并且设置模板为MyCharacterMovementComponent
ATestForDebugCharacter::ATestForDebugCharacter(const FObjectInitializer& ObjectInitializer /* = FObjectInitializer::Get() */)
:Super(ObjectInitializer.SetDefaultSubobjectClass<UMyCharacterMovementComponent>(ACharacter::CharacterMovementComponentName)) //使用我们自定义的CharacterMovement
{
FMyGameplayCategory<UMyCharacterMovementComponent>::Register(FName(TEXT("MyCharacterDebug")), 6); //注册Debug Category
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// set our turn rates for input
BaseTurnRate = 45.f;
BaseLookUpRate = 45.f;
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Configure character movement
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
GetCharacterMovement()->JumpZVelocity = 600.f;
GetCharacterMovement()->AirControl = 0.2f;
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
// Create a follow camera
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
}
在自定义的Movement中 定义debug回调函数 输出当前角色的移动速度
FString UMyCharacterMovementComponent::GetDebugStr()
{
return (Cast<ACharacter>(GetOwner()))->GetVelocity().ToString();
}
具体的代码可以看下载的工程