构造函数用构造函数的创建组件的接口,非构造函数用非构造函数的接口。

构造函数中

UStaticMeshComponent* MyComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyComponent"));
MyComponent->SetupAttachment(RootComponent);

非构造函数

UStaticMeshComponent* MyComponent = NewObject<UStaticMeshComponent>(this, TEXT("MyComponent"));
MyComponent->RegisterComponent();
MyComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);

NOTE:

NewObject+Register的方法不能在构造函数中创建组件。

.h文件中不要声明MyComponent,如果声明的话,不要加class

 

转载需注明出处。

 

相关文章:

  • 2021-08-25
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
  • 2021-05-16
  • 2021-08-31
  • 2021-07-12
猜你喜欢
  • 2021-08-31
  • 2022-12-23
  • 2021-07-06
  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
相关资源
相似解决方案