Skip to content

strangechu's Blog

  • About me
  • GitHub
  • LinkedIn

strangechu's Blog

Work & Game

[UE4] 快速繪製2D/3D Debug圖形 (C++)

3 月 12, 2021 by strangechu

UE4的藍圖內有各種方便的Debug繪製功能,在此整理在C++中使用的快速繪製Debug圖形方法。

2D繪製:使用UDebugDrawService

此處以某攝影機的Actor為例子。 在Class內追加FDelegateHandleCameraDebugDrawHandle。 之後將它登錄到UDebugDrawService。登錄後每個Frame會呼叫此登錄的函數。 在函數傳入值的Canvas上直接繪製需要的圖形。

(TestCamera.h)
class ATestCamera : public AActor
{
	GENERATED_BODY()

public:
	ASideScrollTestCamera();

	void DebugDraw(UCanvas* InCanvas, APlayerController* InPC);

protected:
	FDelegateHandle CameraDebugDrawHandle;
};
(TestCamera.cpp)
#include "TestCamera.h"
#include "Debug/DebugDrawService.h"

ASideScrollTestCamera::ATestCamera()
{
	FDebugDrawDelegate DrawDebugDelegate = FDebugDrawDelegate::CreateUObject(this, &TestCamera::HandleDebugDraw);

	CameraDebugDrawHandle = UDebugDrawService::Register(TEXT("GameplayDebug"), DrawDebugDelegate);
}

void ANxPlayerCameraManager::BeginDestroy()
{
	Super::BeginDestroy();
	// 物件刪除時解除登錄
	UDebugDrawService::Unregister(this->DrawDebugDelegateHandle);
}

void ASideScrollTestCamera::HandleDebugDraw(UCanvas* InCanvas, APlayerController* InPC)
{
	// 在此繪製Debug圖形
	FCanvasLineItem item(FVector2D(100.f, 100.0f), FVector2D(200.f, 200.0f));
	item.LineThickness = 10.f;
	item.SetColor(FLinearColor::Red);
	InCanvas->DrawItem(item);
}

3D繪製:使用DrawDebugHelpers

請參照引擎原始碼的DrawDebugHelpers.h的DrawDebugLine等,include後直接使用即可。

/** Draw a debug line */
ENGINE_API void DrawDebugLine(const UWorld* InWorld, FVector const& LineStart, FVector const& LineEnd, FColor const& Color, bool bPersistentLines = false, float LifeTime=-1.f, uint8 DepthPriority = 0, float Thickness = 0.f);
/** Draw a debug point */
ENGINE_API void DrawDebugPoint(const UWorld* InWorld, FVector const& Position, float Size, FColor const& PointColor, bool bPersistentLines = false, float LifeTime=-1.f, uint8 DepthPriority = 0);
/** Draw directional arrow **/
ENGINE_API void DrawDebugDirectionalArrow(const UWorld* InWorld, FVector const& LineStart, FVector const& LineEnd, float ArrowSize, FColor const& Color, bool bPersistentLines = false, float LifeTime=-1.f, uint8 DepthPriority = 0, float Thickness = 0.f);

其實這篇主要是想提2D繪製。之前還要特別製作Widget來繪製,之後才發現有這個方便的功能,讓繪製的流程快速多了。

Post navigation

Previous Post:

個人遊戲月報 2020年11月

Next Post:

[UE4] 真正的全文搜尋: Asset Search Plugin

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

近期文章

  • 從《33遠征隊》看日本遊戲業的未來——JRPG的蛻變與停滯
  • 機戰傭兵6 玩後感
  • 沉沒意志 玩後感
  • Fallout影集觀後感
  • [CEDEC2024]薩爾達的餘料建造開發減法

近期留言

  • 「strangechu」於〈個人遊戲月報 2020年8月〉發佈留言
  • 「扇」於〈個人遊戲月報 2020年8月〉發佈留言

彙整

  • 2025 年 5 月
  • 2025 年 1 月
  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2024 年 3 月
  • 2024 年 1 月
  • 2023 年 12 月
  • 2023 年 11 月
  • 2023 年 10 月
  • 2023 年 8 月
  • 2023 年 6 月
  • 2023 年 5 月
  • 2023 年 4 月
  • 2023 年 3 月
  • 2023 年 1 月
  • 2022 年 12 月
  • 2022 年 10 月
  • 2022 年 9 月
  • 2022 年 5 月
  • 2022 年 3 月
  • 2022 年 2 月
  • 2022 年 1 月
  • 2021 年 12 月
  • 2021 年 10 月
  • 2021 年 3 月
  • 2020 年 12 月
  • 2020 年 11 月
  • 2020 年 10 月
  • 2020 年 8 月
  • 2020 年 7 月
  • 2020 年 3 月
  • 2020 年 2 月
  • 2019 年 7 月
  • 2019 年 4 月

分類

  • Career
  • Game
  • Game Review
  • Monthly Report
  • UE4
  • UE5
  • Uncategorized
  • 技術
  • 演講心得

其他操作

  • 登入
  • 訂閱網站內容 RSS 資訊提供
  • 訂閱網站留言 RSS 資訊提供
  • WordPress 台灣繁體中文
© 2025 strangechu's Blog | WordPress Theme by Superbthemes