構造函數用構造函數的創建組件的接口,非構造函數用非構造函數的接口。
構造函數中
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