PackedScene

Inherits: Resource < Reference < Object

Inherited By: PackedSceneGLTF

一个序列化场景的抽象。

描述

场景文件的简化接口。提供针对场景资源本身的操作和检查。

可以用来将节点保存到文件中。当保存时,该节点以及它所拥有的所有节点都会被保存(请参阅 Node 中的属性 owner)。

注意:节点不需要拥有自己。

加载已保存场景的示例:

  1. # 如果在编译时不知道路径,就使用 `load()` 而不是 `preload()`。
  2. var scene = preload("res://scene.tscn").instance()
  3. # 将该节点作为脚本所依附的节点的一个子节点。
  4. add_child(scene)

保存具有不同所有者节点的例子:下面的例子创建了 3 个对象:Node2Dnode)、RigidBody2Drigid)、CollisionObject2Dcollision)。collisionrigid 的子节点、rigid 是 node 的子节点。只有 rigidnode拥有,因此 pack 只会保存这两个节点,不会保存 collision

  1. # 创建对象。
  2. var node = Node2D.new()
  3. var rigid = RigidBody2D.new()
  4. var collision = CollisionShape2D.new()
  5. # 创建对象的层次结构。
  6. rigid.add_child(collision)
  7. node.add_child(rigid)
  8. # 改变 `rigid` 的所有者,但不改变 `collision` 的所有者。
  9. rigid.owner = node
  10. var scene = PackedScene.new()
  11. # 现在只有 `node` 和 `rigid` 被打包。
  12. var result = scene.pack(node)
  13. if result == OK:
  14. var error = ResourceSaver.save("res://path/name.scn", scene) # 或者 "user://..."
  15. if error != OK:
  16. push_error("保存场景到磁盘时发生错误。")

教程

属性

Dictionary

_bundled

{“conn_count”: 0,”conns”: PoolIntArray(  ),”editable_instances”: [  ],”names”: PoolStringArray(  ),”node_count”: 0,”node_paths”: [  ],”nodes”: PoolIntArray(  ),”variants”: [  ],”version”: 2}

方法

bool

can_instance ( ) const

SceneState

get_state ( )

Node

instance ( GenEditState edit_state=0 ) const

Error

pack ( Node path )

枚举

enum GenEditState:

  • GEN_EDIT_STATE_DISABLED = 0 —- 如果传递给instance,则阻止对场景状态的编辑。

  • GEN_EDIT_STATE_INSTANCE = 1 —- 如果传递给instance,则向本地场景提供本地场景资源。

注意: 只在编辑器构建中可用。

  • GEN_EDIT_STATE_MAIN = 2 —- 如果传递给instance,向本地场景提供本地场景资源。只有主场景应该接收主编辑状态。

注意: 只在编辑器构建中可用。

  • GEN_EDIT_STATE_MAIN_INHERITED = 3 —- It’s similar to GEN_EDIT_STATE_MAIN, but for the case where the scene is being instantiated to be the base of another one.

Note: Only available in editor builds.

属性说明

Default

{“conn_count”: 0,”conns”: PoolIntArray(  ),”editable_instances”: [  ],”names”: PoolStringArray(  ),”node_count”: 0,”node_paths”: [  ],”nodes”: PoolIntArray(  ),”variants”: [  ],”version”: 2}

场景内容的字典表示。

可用的字段包括资源的“rnames”和“variants”,节点的“node_count”、“nodes”、“node_paths”,基本场景子级覆盖的“editable_instances”,信号连接的“conn_count”和“conns”,以及 PackedScene 格式样式的版本“version”。

方法说明

  • bool can_instance ( ) const

如果场景文件有节点,返回 true


返回代表场景文件内容的 SceneState


实例化场景的节点层次结构。触发子场景实例化。在根节点上触发一个 Node.NOTIFICATION_INSTANCED 通知。


包将忽略不属于给定节点的任何子节点。请参阅 Node.owner