生成/销毁Actor总览

在虚幻引擎4中生成Actor的指南。

Choose your operating system:

Windows

macOS

Linux

选择实现方法:

Blueprints

C++

很多时候,玩家在游戏中会不断获得新物品或新武器,甚至是遇到新的敌人。

在你的游戏中,你可能会希望在关卡中动态添加物品或敌人,这种行文称作"生成"。

在本篇入门指南中,你将创建一个Actor类,并且能通过点击按钮让这个Actor在出生点位置生成。

![image alt text](image_0.gif)(convert:false)

创建要生成的Actor

在此小节中,你将创建一个Actor,它包含球体组件、静态网格体组件和粒子系统组件。

  1. 首先新建一个 第三人称(Third Person) 蓝图(Blueprint) 游戏项目,其中 初学者内容包(Starter Content) 设置为 启用(enabled) 。将该项目命名为 SpawnDestroy

    ![](image_1. png)(convert:false)

  2. 内容浏览器(Content Browser) 中选择 添加/导入(Add/Import) 按钮,新建一个 蓝图Actor(Blueprint Actor) 类,并将其命名为 Bp_ActorToSpawn

    ![](image_2.png)(convert:false)

  3. 类默认值(Class Defaults) 中,导航至 组件(Components) 选项卡并选择 添加组件(Add Component) 。然后,搜索并选择 球体组件(Sphere Component) ,并将其拖动到 场景组件(Scene Component) 上,使其成为新的 根组件(Root Component)

    ![](image_3.png)(convert:false)

  4. 球体组件(Sphere Component) 细节(Details) 面板中,导航至 形状(Shape) 类别,并将 球体半径(Sphere Radius) 值更改为 16.0

    ![](image_4.png)(convert:false)

  5. 导航至 物理(Physics) 类别,并选中 模拟物理(Simulate Physics) 变量旁边的框。

    ![](image_5.png)(convert:false)

  6. 导航至 组件(Components) 选项卡并选择 添加组件(Add Component) ,然后搜索并选择 静态网格体组件(Static Mesh Component)

    ![](image_6.png)(convert:false)

  7. 细节(Details) 面板中,找到 静态网格体(Static Mesh) 类别,选择 静态网格体(Static Mesh) 变量旁边的下拉箭头。然后,搜索并选择 Shape_Sphere静态网格体(Shape_Sphere Static Mesh) 资产。

    ![](image_7.png)(convert:false)

  8. 导航至 变换(Transform) 类别,并在 比例(Scale) 变量中将 X、Y Z 值更改为 0 .25 、0 .25 、0 .25 。然后在 位置(Location) 变量中将 Z 值更改为 -12.0

    ![](image_8.png)(convert:false)

  9. 导航至 组件(Components) 选项卡,选择 添加组件(Add Component) 。搜索并选择 粒子系统组件(Particle System Component)

    ![](image_9.png)(convert:false)

  10. 导航至 细节(Details) 面板,在 粒子(Particles) 类别中选择 模板(Template) 变量旁边的下拉箭头,然后搜索并选择 P_Fire

    ![](image_10.png)(convert:false)

  11. 编译(Compile) 保存(save) 蓝图。

    ![](image_11. png)(convert:false)

完成的蓝图

![](image_12.png)(convert:false)

创建Actor生成器

现在已经创建了要生成的Actor,接下来将创建一个Actor生成器,其中包含用于生成新Bp_ActorSpawn蓝图的自定义函数。

  1. 内容浏览器(Content Browser) 中选择 添加/导入(Add/Import) 按钮,新建一个 蓝图Actor(Blueprint Actor) 类,并将其命名为 Bp_ActorSpawner

    ![](image_13.png)(convert:false)

  2. 类默认值(Class Defaults) 中,导航至 组件(Components) 选项卡并选择 添加组件(Add Component) 。然后搜索并选择 盒体组件(Box Component) ,并将其重命名为 SpawnVolume

    ![](image_14.png)(convert:false)

  3. 右键点击 事件图表(Event Graph) ,从 操作(Actions) 上下文菜单中搜索并选择 添加自定义事件(Add Custom Event) ,然后将其命名为 SpawnActor

    ![](image_15.png)(convert:false)

  4. 生成Actor自定义事件(Spawn Actor Custom Event) 执行引脚拖出一根引线,然后在 可执行操作(Executable actions) 下拉菜单中搜索并选择 Spawn Actor From Class

    ![](image_16.png)(convert:false)

  5. SpawnActor 节点的 类(Class) 引脚中,选择 BP_ActorToSpawn 类。

    ![](image_17.png)(convert:false)

  6. 点击 Spawn Actor from Class 节点中的 生成变换(Spawn Transform) 引脚并从中拖出一根引线,然后从 操作(Actions) 下拉菜单中搜索并选择 Get Actor Transform

    ![](image_18.png)(convert:false)

  7. 编译(Compile) 保存(save) 蓝图。

    ![](image_19.png)(convert:false)

完成的蓝图

![](image_20.png)(convert:false)

设置其他角色操作命令

现在已经创建了两个Actor类(Bp_ActorToSpawn和Bp_ActorSpawner),接下来的任务是设置第三人称角色,使其拥有调用Actor生成器的Spawn Actor函数的功能。

  1. 导航至 编辑(Edit) > 项目设置(Project Settings) > 引擎(Engine) > 输入(Input) ,然后从 绑定(Bindings) > 操作(Action) 映射(Mappings) 类别中,点击 + 按钮 额外添加两个名为 SpawnActors DestroyActors 的映射。

    ![](image_21. png)(convert:false)

  2. SpawnActors 操作键映射设置为数字键 1 ,将 DestroyActors 操作键映射到数字键 2

    ![](image_22.png)(convert:false)

  3. 内容浏览器(Content Browser) 导航至 第三人称角色(Third Person Character) 蓝图,双击打开其类默认值,然后右键点击 事件图表(Event Graph) 。从 此蓝图的所有操作(All Actions for this Blueprint) 上下文菜单中,搜索并选择 SpawnActors 输入操作。

    ![](image_23.png)(convert:false)

  4. 按下(Pressed) 执行引脚拖出一根引线,然后在 可执行操作(Executable actions) 下拉菜单中搜索并选择 Get Actor Of Class 节点。然后,点击 Actor类(Actor Class) 的下拉箭头,选择 Bp Actor Spawner

    ![](image_24.png)(convert:false)

  5. Get Actor of Class 节点的 返回值引脚(Return value pin) 拖出一根引线。在 操作菜单(Actions menu) 中搜索并选择调用函数 Spawn Actor ,然后将 Get Actor Of Class 节点的执行引脚连接到 Spawn Actor 节点的执行引脚。

    ![](image_25.png)(convert:false)

  6. 右键点击 事件图表(Event Graph) ,在上下文菜单中搜索并选择 DestroyActors 输入操作事件。

    ![](image_26.png)(convert:false)

  7. 按下(Pressed) 执行引脚拖出一根引线,并在 可执行操作(Executable actions) 下拉菜单中搜索并选择 Get All Actors Of Class 节点。然后,点击 Actor类(Actor Class) 的下拉箭头,并选择 要生成的Bp Actor(Bp Actor to Spawn)

    ![](image_27.png)(convert:false)

  8. Out Actors返回值引脚(Out Actors Return value pin) 拖出一根引线,然后在下拉菜单中搜索并选择 For Each Loop With Break 节点。

    ![](image_28.png)(convert:false)

  9. 数组元素引脚(Array Element pin) 拖出一根引线,并在 操作(Actions) 菜单中搜索并选择 Destroy Actor 函数,然后将 Get All Actors of Class 节点的输出执行引脚连接到 For Each Loop with Break 节点的 执行引脚(the Execution Pin)

  10. 接下来,将For Each Loop with Break的 循环主体(Loop Body) 执行引脚连接到 销毁Actor(Destroy Actor) 输入执行引脚。

    ![](image_29.png)(convert:false)

  11. 编译(Compile) 保存(save)

    ![](image_30.png)(convert:false)

完成的蓝图

![](image_31. png)(convert:false)

设置场景

现在已经完成了玩家角色所需的功能创建,可以调用Bp_Spawner的SpawnActor函数,接下来将需要选择Bp_Spawner在关卡中的路径。

  1. 内容浏览器(Content Browser) 中,将 Bp_ActorSpawner 的实例拖动到关卡中。

    ![](image_32.png)(convert:false)

  2. 选择 PIE (在编辑器中运行),以测试你的Bp_ActorSpawner。

最终结果

在编辑器中进行测试时,如果按 1 键,Actor生成器类将在关卡中生成Actor。如果决定按 2 键,则关卡中的所有ActorsToSpawn都将被销毁。

![](image_33.gif)(convert:false)

[INCLUDE:ProgrammingAndScripting/SpawnAndDestroyActors\CPP]
欢迎帮助改进虚幻引擎文档!请告诉我们该如何更好地为您服务。
填写问卷调查
取消