:github_url: hide .. _class_ResourceUID: ResourceUID =========== **继承:** :ref:`Object` 管理项目中所有资源的唯一标识符的单例。 .. rst-class:: classref-introduction-group 描述 ---- 资源 UID(Unique IDentifier,唯一标识符)可以使引擎保持资源之间引用关系的完整性,即使文件发生重命名或移动。可以使用 ``uid://`` 访问。 \ **ResourceUID** 能够跟踪项目中所有已注册的资源 UID,生成新的 UID,也能够将标识符在字符串表示和整数表示之间进行转换。 .. rst-class:: classref-reftable-group 方法 ---- .. table:: :widths: auto +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_id`\ (\ id\: :ref:`int`, path\: :ref:`String`\ ) | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`create_id`\ (\ ) | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`create_id_for_path`\ (\ path\: :ref:`String`\ ) | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`ensure_path`\ (\ path_or_uid\: :ref:`String`\ ) |static| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_id_path`\ (\ id\: :ref:`int`\ ) |const| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has_id`\ (\ id\: :ref:`int`\ ) |const| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`id_to_text`\ (\ id\: :ref:`int`\ ) |const| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`path_to_uid`\ (\ path\: :ref:`String`\ ) |static| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`remove_id`\ (\ id\: :ref:`int`\ ) | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_id`\ (\ id\: :ref:`int`, path\: :ref:`String`\ ) | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`text_to_id`\ (\ text_id\: :ref:`String`\ ) |const| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`uid_to_path`\ (\ uid\: :ref:`String`\ ) |static| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 常量 ---- .. _class_ResourceUID_constant_INVALID_ID: .. rst-class:: classref-constant **INVALID_ID** = ``-1`` :ref:`🔗` 用于无效 UID 的值,例如无法加载的资源。 对应的文本表示为 ``uid://``\ 。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 方法说明 -------- .. _class_ResourceUID_method_add_id: .. rst-class:: classref-method |void| **add_id**\ (\ id\: :ref:`int`, path\: :ref:`String`\ ) :ref:`🔗` 添加一个新的 UID 值,将其映射到给定的资源路径。 如果 UID 已经存在,则会返回错误,因此请务必先使用 :ref:`has_id()` 进行检查,或者改用 :ref:`set_id()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_create_id: .. rst-class:: classref-method :ref:`int` **create_id**\ (\ ) :ref:`🔗` 生成随机的资源 UID,该 UID 在当前加载的 UID 列表中保证唯一。 要注册这个 UID,你必须调用 :ref:`add_id()` 或 :ref:`set_id()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_create_id_for_path: .. rst-class:: classref-method :ref:`int` **create_id_for_path**\ (\ path\: :ref:`String`\ ) :ref:`🔗` 与 :ref:`create_id()` 类型,但是 UID 会使用 ``path`` 路径和项目名称设置随机数种子。在当前项目中为该路径生成的 UID 始终相同。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_ensure_path: .. rst-class:: classref-method :ref:`String` **ensure_path**\ (\ path_or_uid\: :ref:`String`\ ) |static| :ref:`🔗` Returns a path, converting ``path_or_uid`` if necessary. Fails and returns an empty string if an invalid UID is provided. .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_get_id_path: .. rst-class:: classref-method :ref:`String` **get_id_path**\ (\ id\: :ref:`int`\ ) |const| :ref:`🔗` 返回给定 UID 值引用的路径。 如果 UID 不存在则失败并报错,因此请务必使用 :ref:`has_id()` 事先检查。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_has_id: .. rst-class:: classref-method :ref:`bool` **has_id**\ (\ id\: :ref:`int`\ ) |const| :ref:`🔗` 返回给定的 UID 值是否为缓存所知。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_id_to_text: .. rst-class:: classref-method :ref:`String` **id_to_text**\ (\ id\: :ref:`int`\ ) |const| :ref:`🔗` 将给定的 UID 转换为 ``uid://`` 字符串值。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_path_to_uid: .. rst-class:: classref-method :ref:`String` **path_to_uid**\ (\ path\: :ref:`String`\ ) |static| :ref:`🔗` 将 ``path`` 中提供的资源路径转换为 UID。如果没有与之关联的 UID 则会原样返回该路径。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_remove_id: .. rst-class:: classref-method |void| **remove_id**\ (\ id\: :ref:`int`\ ) :ref:`🔗` 从缓存中删除一个已加载的 UID 值。 如果 UID 不存在则失败并报错,因此请务必提前使用 :ref:`has_id()` 检查。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_set_id: .. rst-class:: classref-method |void| **set_id**\ (\ id\: :ref:`int`, path\: :ref:`String`\ ) :ref:`🔗` 更新现有 UID 的资源路径。 如果 UID 不存在,则失败并出现错误,因此请务必提前使用 :ref:`has_id()` 检查,或者改用 :ref:`add_id()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_text_to_id: .. rst-class:: classref-method :ref:`int` **text_to_id**\ (\ text_id\: :ref:`String`\ ) |const| :ref:`🔗` 从给定的 ``uid://`` 字符串中提取 UID 值。 .. rst-class:: classref-item-separator ---- .. _class_ResourceUID_method_uid_to_path: .. rst-class:: classref-method :ref:`String` **uid_to_path**\ (\ uid\: :ref:`String`\ ) |static| :ref:`🔗` 将 ``uid`` 转换为路径。如果该 UID 无效则打印错误。 .. |virtual| replace:: :abbr:`virtual (本方法通常需要用户覆盖才能生效。)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (本方法无副作用,不会修改该实例的任何成员变量。)` .. |vararg| replace:: :abbr:`vararg (本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。)` .. |constructor| replace:: :abbr:`constructor (本方法用于构造某个类型。)` .. |static| replace:: :abbr:`static (调用本方法无需实例,可直接使用类名进行调用。)` .. |operator| replace:: :abbr:`operator (本方法描述的是使用本类型作为左操作数的有效运算符。)` .. |bitfield| replace:: :abbr:`BitField (这个值是由下列位标志构成位掩码的整数。)` .. |void| replace:: :abbr:`void (无返回值。)`