最简单的 UE 4 C++ 教程 —— 绘制 UE4 辅助调试的形状【六】

举报
ShaderJoy 发表于 2021/11/19 01:08:13 2021/11/19
【摘要】  【原教程是基于 UE 4.18,我是基于 UE 4.25】 英文原地址 接上一节,在本教程中,我们将绘制 DrawDebugHelpers.h 提供的多个调试形状。DrawDebugHelpers.h 包含的相关的所有内容都是由 UE4 提供的。 创建一个新的 C++  Actor 子...

 【原教程是基于 UE 4.18,我是基于 UE 4.25】

英文原地址

接上一节,在本教程中,我们将绘制 DrawDebugHelpers.h 提供的多个调试形状。DrawDebugHelpers.h 包含的相关的所有内容都是由 UE4 提供的。

创建一个新的 C++  Actor 子类并将其命名为 MyDrawDebugHelpers 。在头文件中,我们将在 .h 中声明将在 .cpp 文件中使用的变量,它们也可以在编辑器中进行编辑。我们将创建一个FVectors,一个 FMatrix,一个 FBox,和一个 FTransform

该头文件代码如下:


  
  1. #pragma once
  2. #include "CoreMinimal.h"
  3. #include "GameFramework/Actor.h"
  4. #include "MyDrawDebugHelpers.generated.h"
  5. UCLASS()
  6. class UNREALCPP_API AMyDrawDebugHelpers : public AActor
  7. {
  8. GENERATED_BODY()
  9. public:
  10. // Sets default values for this actor's properties
  11. AMyDrawDebugHelpers();
  12. protected:
  13. // Called when the game starts or when spawned
  14. virtual void BeginPlay() override;
  15. public:
  16. // Called every frame
  17. virtual void Tick(float DeltaTime) override;
  18. // declare location variables
  19. UPROPERTY(EditAnywhere, Category = "Locations")
  20. FVector LocationOne;
  21. UPROPERTY(EditAnywhere, Category = "Locations")
  22. FVector LocationTwo;
  23. UPROPERTY(EditAnywhere, Category = "Locations")
  24. FVector LocationThree;
  25. UPROPERTY(EditAnywhere, Category = "Locations")
  26. FVector LocationFour;
  27. UPROPERTY(EditAnywhere, Category = "Locations")
  28. FVector LocationFive;
  29. UPROPERTY(EditAnywhere, Category = "Locations")
  30. FMatrix CircleMatrix;
  31. UPROPERTY(EditAnywhere, Category = "Locations")
  32. FBox MyBox;
  33. UPROPERTY(EditAnywhere, Category = "Locations")
  34. FTransform MyTransform;
  35. };

同时别忘了还要 include 头文件 DrawDebugHelpers.h 


  
  1. #include "MyDrawDebugHelpers.h"
  2. // include draw debu helpers header file
  3. #include "DrawDebugHelpers.h"

在.cpp文件中,我们将首先为变量指定默认值。下面是我们将为默认值添加的初始化代码。


  
  1. // Sets default values
  2. AMyDrawDebugHelpers::AMyDrawDebugHelpers()
  3. {
  4. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  5. PrimaryActorTick.bCanEverTick = true;
  6. // init variables with values
  7. LocationOne = FVector(0,0,600);
  8. LocationTwo = FVector(0,-600,600);
  9. LocationThree = FVector(0,600,600);
  10. LocationFour = FVector(-300,0,600);
  11. LocationFive = FVector(-400,-600,600);
  12. MyBox = FBox(FVector(0,0,0), FVector(200,200,200));
  13. }

接下来,我们将在 BeginPlay() 函数中绘制许多不同的形状。

下面是我们将添加到 BeginPlay() 来绘制所有形状的函数。


  
  1. // Called when the game starts or when spawned
  2. void AMyDrawDebugHelpers::BeginPlay()
  3. {
  4. Super::BeginPlay();
  5. DrawDebugPoint(GetWorld(), LocationOne, 200, FColor(52,220,239), true);
  6. DrawDebugSphere(GetWorld(), LocationTwo, 200, 26, FColor(181,0,0), true, -1, 0, 2);
  7. DrawDebugCircle(GetWorld(), CircleMatrix, 200, 50, FColor(0,104,167), true, -1, 0, 10);
  8. DrawDebugCircle(GetWorld(), LocationFour, 200, 50, FColor(0,0,0), true, -1, 0, 10);
  9. DrawDebugSolidBox(GetWorld(), MyBox, FColor(20, 100, 240), MyTransform, true);
  10. DrawDebugBox(GetWorld(), LocationFive, FVector(100,100,100), FColor::Purple, true, -1, 0, 10);
  11. DrawDebugLine(GetWorld(), LocationTwo, LocationThree, FColor::Emerald, true, -1, 0, 10);
  12. DrawDebugDirectionalArrow(GetWorld(), FVector(-300, 600, 600), FVector(-300, -600, 600), 120.f, FColor::Magenta, true, -1.f, 0, 5.f);
  13. DrawDebugCrosshairs(GetWorld(), FVector(0,0,1000), FRotator(0,0,0), 500.f, FColor::White, true, -1.f, 0);
  14. }

编译代码。将你的新 Actor 实例对象拖放到场景中,则形状将在游戏中绘制。同时还可以在 Actor 的详细信息面板中编辑那些变量。

下面是最终的 .cpp 代码。


  
  1. #include "MyDrawDebugHelpers.h"
  2. // include draw debu helpers header file
  3. #include "DrawDebugHelpers.h"
  4. // Sets default values
  5. AMyDrawDebugHelpers::AMyDrawDebugHelpers()
  6. {
  7. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  8. PrimaryActorTick.bCanEverTick = true;
  9. // init variables with values
  10. LocationOne = FVector(0,0,600);
  11. LocationTwo = FVector(0,-600,600);
  12. LocationThree = FVector(0,600,600);
  13. LocationFour = FVector(-300,0,600);
  14. LocationFive = FVector(-400,-600,600);
  15. MyBox = FBox(FVector(0,0,0), FVector(200,200,200));
  16. }
  17. // Called when the game starts or when spawned
  18. void AMyDrawDebugHelpers::BeginPlay()
  19. {
  20. Super::BeginPlay();
  21. DrawDebugPoint(GetWorld(), LocationOne, 200, FColor(52,220,239), true);
  22. DrawDebugSphere(GetWorld(), LocationTwo, 200, 26, FColor(181,0,0), true, -1, 0, 2);
  23. DrawDebugCircle(GetWorld(), CircleMatrix, 200, 50, FColor(0,104,167), true, -1, 0, 10);
  24. DrawDebugCircle(GetWorld(), LocationFour, 200, 50, FColor(0,0,0), true, -1, 0, 10);
  25. DrawDebugSolidBox(GetWorld(), MyBox, FColor(20, 100, 240), MyTransform, true);
  26. DrawDebugBox(GetWorld(), LocationFive, FVector(100,100,100), FColor::Purple, true, -1, 0, 10);
  27. DrawDebugLine(GetWorld(), LocationTwo, LocationThree, FColor::Emerald, true, -1, 0, 10);
  28. DrawDebugDirectionalArrow(GetWorld(), FVector(-300, 600, 600), FVector(-300, -600, 600), 120.f, FColor::Magenta, true, -1.f, 0, 5.f);
  29. DrawDebugCrosshairs(GetWorld(), FVector(0,0,1000), FRotator(0,0,0), 500.f, FColor::White, true, -1.f, 0);
  30. }
  31. // Called every frame
  32. void AMyDrawDebugHelpers::Tick(float DeltaTime)
  33. {
  34. Super::Tick(DeltaTime);
  35. }

最终效果如下

 

 

文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/119100805

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。