1. 程式人生 > >UE4 C++程式碼中使用材質,字型等資源

UE4 C++程式碼中使用材質,字型等資源

當我使用ue4 editor製作了一個材質(Material)或字型(Font)之後,該材質和字型等資源不僅可以在editor中使用,還可以在C++程式碼中使用。
下圖為我在editor中製作的資源:
ue4材質和字型資源圖片
我們可以通過右鍵點選該資源,點選“Copy Reference”來獲得資源的參考路徑。
在這裡,我獲得的 字型(font)資源參考路徑為“Font’/Game/NewFont5.NewFont5’”,
材質(material)資源參考路徑為“Font’/Game/NewFont4.NewFont4’

然後在C++中定義該型別資源,定義該型別資源,並進行使用:

給RenderTextActor設定字型

//定義一個RenderTextActor物件
UTextRenderComponent *CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
//定義一個字型物件
ConstructorHelpers::FObjectFinder<UFont> ScenceMaterial(TEXT("Font'/Game/NewFont5.NewFont5'"));
//給RenderTextActor物件設定一個字型
CountdownText->SetFont(ScenceMaterial.Object);

定義材質物件:

ConstructorHelpers::FObjectFinder<UMaterial> ScenceMaterial(TEXT("Material'/Game/SenceMaterial.SenceMaterial'"));