UE4 中在 Actor 中动态 Create Component 与ChildActor 的 小笔记

时间:2021-11-30 16:54:12

记录UE4,Component的应用的一些问题.

Q. 在UE4中往Actor/Pawn 中动态创建 Component与ChildActor

Note:旧版本的UE4 的Attach 和12.13版本有些不一样

创建Component:

UCpp_MyComponent* temp_imageCom = NewObject<UCpp_MyComponent>(this, UCpp_MyComponent::StaticClass());
temp_imageCom->RegisterComponent();
temp_imageCom->AttachToComponent(pParentComponent, FAttachmentTransformRules::KeepRelativeTransform);

创建Actor Component:

仅多了一步SetChildActorClass

auto temp_childActorComponent = NewObject<UChildActorComponent>(this, UChildActorComponent::StaticClass());
temp_imageCom->RegisterComponent();
temp_childActorComponent->SetChildActorClass(*MyActorClass);
temp_imageCom->AttachToComponent(pParentComponent, FAttachmentTransformRules::KeepRelativeTransform);

删除函数:(命名真是迷)

DetachRootComponentFromParent

例如:

Note:慎用此UChildActorComponent的动态生成方式

   父类Actor 的SetActorEnableCollision 方法,是不会影响 UChildActorComponent 的 GetChildActor()的,但是最搞笑的是当你调用父类Actor 的 SetActorHiddenGame 却会隐藏掉 UChildActorComponent 的 GetChildActor()。

   如果想通过UChildActorComponent的GetChildActor()对Actor进行Transform 操作的话不能采用Relative, 必须采用World 或者直接Transform Actor 的 ActorComponent