HUD绘制UI,其他绘制相关的接口看源码就可以使用了
创建继承自AHUD的c++类-AMyHUD,构造函数中加载一些资源
MyHUD.h
UCLASS() class MYQWE_API AMyHUD : public AHUD { GENERATED_UCLASS_BODY() public: virtual void DrawHUD() override; //重写父类的绘制函数 void DrawMyIcons(); //绘制图片 void DrawMyItems(); //绘制文字 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyHUD") UTexture2D* mTexture2d; float mScaleFactor; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyHUD") FCanvasIcon mIcons1; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyHUD") FCanvasIcon mIcons2; UTexture2D* mTextureArrowDown; UTexture2D* mTextureArrowLeft; FCanvasIcon mAllIcons[2]; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyHUD") UFont* mFont; };
MyHUD.cpp
#include "Engine/StreamableManager.h" AMyHUD::AMyHUD(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { //加载资源方式1 FStreamableManager* stream = new FStreamableManager(); FStringAssetReference ref(TEXT("/Game/UI/bb")); mTexture2d = Cast<UTexture2D>(stream->SynchronousLoad(ref)); //加载资源方式2 static ConstructorHelpers::FObjectFinder<UTexture2D> TextureArrowDownOb(TEXT("/Game/UI/Arrow_Down_1")); static ConstructorHelpers::FObjectFinder<UTexture2D> TextureArrowLeftOb(TEXT("/Game/UI/Arrow_Left_2")); mTextureArrowDown = TextureArrowDownOb.Object; mTextureArrowLeft = TextureArrowLeftOb.Object; //四个float理解为Rect即可,只不过表示的是贴图中的uv坐标,而不是坐标系中xy坐标 //以像素为单位,而不是uv中的范围0-1 mAllIcons[0] = UCanvas::MakeIcon(mTextureArrowDown, 0, 0, 60, 60); mAllIcons[1] = UCanvas::MakeIcon(mTextureArrowLeft, 0, 0, 60, 60); mIcons1 = mAllIcons[0]; mIcons2 = mAllIcons[1]; //加载字体 static ConstructorHelpers::FObjectFinder<UFont> FontOb(TEXT("/Game/UI/Roboto51")); mFont = FontOb.Object; } void AMyHUD::DrawHUD() { Super::DrawHUD(); if (!Canvas) return; mScaleFactor = Canvas->ClipY / 1080.0f; DrawMyIcons(); DrawMyItems(); Canvas->DrawText(mFont, FString(TEXT("--- DrawText")), 150.f, 150.f); } void AMyHUD::DrawMyIcons() { const float StartX = Canvas->ClipX / 2.0f; const float StartY = Canvas->ClipY / 2.0f; for (size_t i = 0; i < 2; i++) { //使用Canvas绘制icon Canvas->DrawIcon(mAllIcons[i], StartX + 100 * i, StartY + 100 * i, mScaleFactor); } } void AMyHUD::DrawMyItems() { FCanvasTextItem textItem = FCanvasTextItem(FVector2D::ZeroVector, FText::GetEmpty(), mFont, FLinearColor::Red); textItem.Text = FText::FromString(TEXT("--- TextItem String!")); Canvas->DrawItem(textItem, 300.f, 300.f); }
资源路径
构造中加载直接就可以在编辑器中看到
作者:yangxuan0261 发表于2017/1/20 14:14:31 原文链接
阅读:12 评论:0 查看评论