答小白

Good Luck!

UE4-GamePlay架构1-Actor和Component关系


学习笔记2019-09-23

UE4-GamePlay架构1-Actor和Component关系

 

一、 Actor生命周期

Unrealengine中对Actor定义“Actor is the base class for an UObject that can be placed or spawned in a level.”

UObject是UE4世界中的最基础类,提供了元数据、反射生成、GC垃圾回收、编辑器可见等基础能力。

clip_image002.jpg

AActor派生于UObject,可被放置在地图中并伴随Level加载而创建。

clip_image004.jpg

 

 

其创建和销毁官网给出如下:

clip_image006.jpg

二、 Actor和ActorComponent的关系

每个Actor都需要很多功能,比如如何移动、如何渲染、如何碰撞检测等,如果这些功能都叠加到Actor身上,随着功能的扩展,继承带来的代码维护性越来越差。因此,在UE4版本中,采用了“组合优于继承”的思想,将各个功能拆分成一个个ActorComponent,与Actor形成组合关系。

clip_image008.jpg

Actor实例化过程中,必然会实例化一个RootComponent,Actor实例和RootComponent实例形成一一对应关系。能作为RootComponent的必然是继承USceneComponent类,而USceneComponent有两大能力:一是Transform,二是SceneComponent的相互嵌套。

对于Component家族,可以用下图简化表示:

clip_image010.png

对于直接继承UActorComponent的组件,如UMovementComponent、AIComponent或者自定义写的Component,它们不存在父子嵌套关系,和RootComponent 并行存放。在UE观念里,只有带Transform的SceneComponent才有资格被嵌套。

基于USceneComponent的父子关系设计,能确定出Actor之间的父子关系。UE里通过Child:AttachToActor或Child:AttachToComponent来创建父子连接。

clip_image012.jpg

clip_image014.jpg

而UChildActorComponent就是为了添加子Actor而设计的。当在蓝图中添加一个UChildActorComponent组件,在Actor实例化过程中,会调用到UChildActorComponent的OnRegister()让子Actor通过AttachToComponent()方式添加进来。

 

 

 

 

参考:

https://docs.unrealengine.com/en-US/API/Runtime/Engine/GameFramework/AActor/index.html

https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/Actors/ActorLifecycle/index.html

https://blog.csdn.net/qq_29523119/article/details/86368182

https://www.cnblogs.com/fjz13/p/5942098.html#4131206

 

 

 


  • 评论列表:

发表评论: