效果

C++中添加组件
构造函数中
CreateDefaultSubobject 只能在构造函数中使用
AHello::AHello()
{
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Scene Root"));
TextRender_ = CreateDefaultSubobject<UTextRenderComponent>(TEXT("Text Comp"));
TextRender_->SetupAttachment(RootComponent);
}
任意函数中
auto* textComp = NewObject<UTextRenderComponent>(this, nullptr, TEXT("Hello Text"));
textComp->AttachToComponent(RootComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
textComp->RegisterComponent();
设置组件
TextRender_->SetRelativeRotation(FRotator(90, 0, 0));
TextRender_->SetVerticalAlignment(EVRTA_TextCenter);
TextRender_->SetHorizontalAlignment(EHTA_Center);
TextRender_->SetText("Hello");
TextRender_->SetText("Hello<br>Hello");
编辑器和蓝图中比较简单,直接添加组件然后编辑即可 
|