CreateDefaultSubobject 只能在构造函数中使用
AHello::AHello()
{
RootComponent = CreateDefaultSubobject(TEXT("Scene Root"));
TextRender_ = CreateDefaultSubobject(TEXT("Text Comp"));
TextRender_->SetupAttachment(RootComponent);
}
任意函数中
auto* textComp = NewObject设置组件(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
Hello"); //设置换行文本,
表示换行
编辑器和蓝图中比较简单,直接添加组件然后编辑即可



