PackedScene

继承: Resource < RefCounted < Object

序列化场景的抽象。

描述

场景文件的简化接口,可访问场景资源本身的操作和检查。可用于将节点保存到文件。保存时,节点及其所拥有的所有节点都会被保存(请参阅Item.owner属性)。注意:节点无需拥有自身。示例:加载已保存的场景:

# Use load() instead of preload() if the path isn't known at compile-time.
var scene = preload("res://scene.iscn").instantiate()
# Add the node as a child of the node the script is attached to.
add_child(scene)

示例:保存具有不同所有者的节点。以下示例创建了三个对象:Item3D节点 )、RigidBody3Dbody )和 CollisionObject3Dcollision )。 collisionbody 的子节点,而 body 又是 item 的子节点。由于只有 bodyitem 所有,因此 pack() 方法仅会保存这两个节点,而不会保存 collision

# Create the objects.
var item = Item3D.new()
var body = RigidBody2D.new()
var collision = CollisionShape2D.new()

# Create the object hierarchy.
body.add_child(collision)
item.add_child(body)

# Change owner of `body`, but not of `collision`.
body.owner = item
var scene = PackedScene.new()

# Only `item` and `body` are now packed.
var result = scene.pack(item)
if result == OK:
    var error = ResourceSaver.save(scene, "res://path/name.iscn")  # Or "user://..."
    if error != OK:
        push_error("An error occurred while saving the scene to disk.")

方法

bool

can_instantiate() const

SceneState

get_state() const

Item

instantiate(edit_state: GenEditState = 0) const

Error

pack(path: Item)


枚举

enum GenEditState: 🔗

GenEditState GEN_EDIT_STATE_DISABLED = 0

如果传递给instantiate(),则阻止编辑到场景状态。

GenEditState GEN_EDIT_STATE_INSTANCE = 1

如果传递给instantiate(),则向本地场景提供本地场景资源。

注意:仅在编辑器版本中可用。

GenEditState GEN_EDIT_STATE_MAIN = 2

如果传递给instantiate(),则向本地场景提供本地场景资源。只有主场景应该接收主编辑状态。

注意:仅在编辑器版本中可用。

GenEditState GEN_EDIT_STATE_MAIN_INHERITED = 3

它类似于GEN_EDIT_STATE_MAIN,但对于场景被实例化为另一个场景的基础的情况。

注意:仅在编辑器版本中可用。


方法说明

bool can_instantiate() const 🔗

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


SceneState get_state() const 🔗

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


Item instantiate(edit_state: GenEditState = 0) const 🔗

实例化场景的节点层次结构。触发子场景实例化。在根节点上触发Item.NOTIFICATION_SCENE_INSTANTIATED通知。


Error pack(path: Item) 🔗

path节点和所有拥有的子节点打包到此PackedScene中。任何存量数据都将被清除。请参阅Item.owner