【UE4】c++ 創建組件


        構造函數用構造函數的創建組件的接口,非構造函數用非構造函數的接口。

構造函數中

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM