:github_url: hide .. DO NOT EDIT THIS FILE!!! .. Generated automatically from Godot engine sources. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/NavigationPolygon.xml. .. _class_NavigationPolygon: NavigationPolygon ================= **实验性:** This class may be changed or removed in future versions. **继承:** :ref:`Resource` **<** :ref:`RefCounted` **<** :ref:`Object` 2D 导航网格,描述用于寻路的可穿越表面。 .. rst-class:: classref-introduction-group 描述 ---- 导航网格可以通过在 :ref:`NavigationServer2D` 的帮助下烘焙它来创建,也可以通过手动添加顶点和凸多边形索引数组来创建。 要烘焙导航网格,至少需要添加一个轮廓来定义烘焙区域的外部边界。 .. tabs:: .. code-tab:: gdscript var new_navigation_mesh = NavigationPolygon.new() var bounding_outline = PackedVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)]) new_navigation_mesh.add_outline(bounding_outline) NavigationServer2D.bake_from_source_geometry_data(new_navigation_mesh, NavigationMeshSourceGeometryData2D.new()); $NavigationRegion2D.navigation_polygon = new_navigation_mesh .. code-tab:: csharp var newNavigationMesh = new NavigationPolygon(); var boundingOutline = new Vector2[] { new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0) }; newNavigationMesh.AddOutline(boundingOutline); NavigationServer2D.BakeFromSourceGeometryData(newNavigationMesh, new NavigationMeshSourceGeometryData2D()); GetNode("NavigationRegion2D").NavigationPolygon = newNavigationMesh; 手动添加顶点和多边形索引。 .. tabs:: .. code-tab:: gdscript var new_navigation_mesh = NavigationPolygon.new() var new_vertices = PackedVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)]) new_navigation_mesh.vertices = new_vertices var new_polygon_indices = PackedInt32Array([0, 1, 2, 3]) new_navigation_mesh.add_polygon(new_polygon_indices) $NavigationRegion2D.navigation_polygon = new_navigation_mesh .. code-tab:: csharp var newNavigationMesh = new NavigationPolygon(); var newVertices = new Vector2[] { new Vector2(0, 0), new Vector2(0, 50), new Vector2(50, 50), new Vector2(50, 0) }; newNavigationMesh.Vertices = newVertices; var newPolygonIndices = new int[] { 0, 1, 2, 3 }; newNavigationMesh.AddPolygon(newPolygonIndices); GetNode("NavigationRegion2D").NavigationPolygon = newNavigationMesh; .. rst-class:: classref-introduction-group 教程 ---- - :doc:`使用 NavigationMesh <../tutorials/navigation/navigation_using_navigationmeshes>` - `2D 导航多边形演示 `__ .. rst-class:: classref-reftable-group 属性 ---- .. table:: :widths: auto +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`float` | :ref:`agent_radius` | ``10.0`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`Rect2` | :ref:`baking_rect` | ``Rect2(0, 0, 0, 0)`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`Vector2` | :ref:`baking_rect_offset` | ``Vector2(0, 0)`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`float` | :ref:`border_size` | ``0.0`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`float` | :ref:`cell_size` | ``1.0`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`int` | :ref:`parsed_collision_mask` | ``4294967295`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`ParsedGeometryType` | :ref:`parsed_geometry_type` | ``2`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`StringName` | :ref:`source_geometry_group_name` | ``&"navigation_polygon_source_geometry_group"`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ | :ref:`SourceGeometryMode` | :ref:`source_geometry_mode` | ``0`` | +----------------------------------------------------------------------+------------------------------------------------------------------------------------------------+-------------------------------------------------+ .. rst-class:: classref-reftable-group 方法 ---- .. table:: :widths: auto +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_outline`\ (\ outline\: :ref:`PackedVector2Array`\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_outline_at_index`\ (\ outline\: :ref:`PackedVector2Array`, index\: :ref:`int`\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`add_polygon`\ (\ polygon\: :ref:`PackedInt32Array`\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`clear`\ (\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`clear_outlines`\ (\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`clear_polygons`\ (\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`NavigationMesh` | :ref:`get_navigation_mesh`\ (\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedVector2Array` | :ref:`get_outline`\ (\ idx\: :ref:`int`\ ) |const| | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_outline_count`\ (\ ) |const| | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`get_parsed_collision_mask_value`\ (\ layer_number\: :ref:`int`\ ) |const| | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedInt32Array` | :ref:`get_polygon`\ (\ idx\: :ref:`int`\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_polygon_count`\ (\ ) |const| | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedVector2Array` | :ref:`get_vertices`\ (\ ) |const| | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`make_polygons_from_outlines`\ (\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`remove_outline`\ (\ idx\: :ref:`int`\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_outline`\ (\ idx\: :ref:`int`, outline\: :ref:`PackedVector2Array`\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_parsed_collision_mask_value`\ (\ layer_number\: :ref:`int`, value\: :ref:`bool`\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_vertices`\ (\ vertices\: :ref:`PackedVector2Array`\ ) | +-----------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 枚举 ---- .. _enum_NavigationPolygon_ParsedGeometryType: .. rst-class:: classref-enumeration enum **ParsedGeometryType**: :ref:`🔗` .. _class_NavigationPolygon_constant_PARSED_GEOMETRY_MESH_INSTANCES: .. rst-class:: classref-enumeration-constant :ref:`ParsedGeometryType` **PARSED_GEOMETRY_MESH_INSTANCES** = ``0`` 将网格实例解析为障碍几何体。这包括 :ref:`Polygon2D`\ 、\ :ref:`MeshInstance2D`\ 、\ :ref:`MultiMeshInstance2D` 和 :ref:`TileMap` 节点。 仅当网格使用 2D 顶点表面格式时才会对其进行解析。 .. _class_NavigationPolygon_constant_PARSED_GEOMETRY_STATIC_COLLIDERS: .. rst-class:: classref-enumeration-constant :ref:`ParsedGeometryType` **PARSED_GEOMETRY_STATIC_COLLIDERS** = ``1`` 将 :ref:`StaticBody2D` 和 :ref:`TileMap` 碰撞器解析为障碍几何体。碰撞器应在由 :ref:`parsed_collision_mask` 指定的层中。 .. _class_NavigationPolygon_constant_PARSED_GEOMETRY_BOTH: .. rst-class:: classref-enumeration-constant :ref:`ParsedGeometryType` **PARSED_GEOMETRY_BOTH** = ``2`` :ref:`PARSED_GEOMETRY_MESH_INSTANCES` 和 :ref:`PARSED_GEOMETRY_STATIC_COLLIDERS`. .. _class_NavigationPolygon_constant_PARSED_GEOMETRY_MAX: .. rst-class:: classref-enumeration-constant :ref:`ParsedGeometryType` **PARSED_GEOMETRY_MAX** = ``3`` 代表 :ref:`ParsedGeometryType` 枚举的大小。 .. rst-class:: classref-item-separator ---- .. _enum_NavigationPolygon_SourceGeometryMode: .. rst-class:: classref-enumeration enum **SourceGeometryMode**: :ref:`🔗` .. _class_NavigationPolygon_constant_SOURCE_GEOMETRY_ROOT_NODE_CHILDREN: .. rst-class:: classref-enumeration-constant :ref:`SourceGeometryMode` **SOURCE_GEOMETRY_ROOT_NODE_CHILDREN** = ``0`` 在根节点的子节点中递归扫描几何体。 .. _class_NavigationPolygon_constant_SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN: .. rst-class:: classref-enumeration-constant :ref:`SourceGeometryMode` **SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN** = ``1`` 以递归方式扫描组中的节点及其子节点以获取几何图形。该组由 :ref:`source_geometry_group_name` 指定。 .. _class_NavigationPolygon_constant_SOURCE_GEOMETRY_GROUPS_EXPLICIT: .. rst-class:: classref-enumeration-constant :ref:`SourceGeometryMode` **SOURCE_GEOMETRY_GROUPS_EXPLICIT** = ``2`` 将组中的节点用于几何。该组由 :ref:`source_geometry_group_name` 指定。 .. _class_NavigationPolygon_constant_SOURCE_GEOMETRY_MAX: .. rst-class:: classref-enumeration-constant :ref:`SourceGeometryMode` **SOURCE_GEOMETRY_MAX** = ``3`` 代表 :ref:`SourceGeometryMode` 枚举的大小。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 属性说明 -------- .. _class_NavigationPolygon_property_agent_radius: .. rst-class:: classref-property :ref:`float` **agent_radius** = ``10.0`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_agent_radius**\ (\ value\: :ref:`float`\ ) - :ref:`float` **get_agent_radius**\ (\ ) 烘焙导航网格时侵蚀/收缩可行走表面的距离。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_property_baking_rect: .. rst-class:: classref-property :ref:`Rect2` **baking_rect** = ``Rect2(0, 0, 0, 0)`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_baking_rect**\ (\ value\: :ref:`Rect2`\ ) - :ref:`Rect2` **get_baking_rect**\ (\ ) 如果烘焙的 :ref:`Rect2` 存在面积,则导航网格烘焙将被限制在其封闭区域内。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_property_baking_rect_offset: .. rst-class:: classref-property :ref:`Vector2` **baking_rect_offset** = ``Vector2(0, 0)`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_baking_rect_offset**\ (\ value\: :ref:`Vector2`\ ) - :ref:`Vector2` **get_baking_rect_offset**\ (\ ) 应用于 :ref:`baking_rect` :ref:`Rect2` 的位置偏移量。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_property_border_size: .. rst-class:: classref-property :ref:`float` **border_size** = ``0.0`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_border_size**\ (\ value\: :ref:`float`\ ) - :ref:`float` **get_border_size**\ (\ ) 由 :ref:`baking_rect` :ref:`Rect2` 定义的烘焙边界区域周围的不可导航边框的大小。 与 :ref:`baking_rect` 结合使用,边框大小可用于烘焙图块对齐的导航网格,而图块边缘不会因 :ref:`agent_radius` 而缩小。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_property_cell_size: .. rst-class:: classref-property :ref:`float` **cell_size** = ``1.0`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_cell_size**\ (\ value\: :ref:`float`\ ) - :ref:`float` **get_cell_size**\ (\ ) 用于将导航网格顶点栅格化的单元格大小。必须与导航地图上的单元格大小相匹配。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_property_parsed_collision_mask: .. rst-class:: classref-property :ref:`int` **parsed_collision_mask** = ``4294967295`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_parsed_collision_mask**\ (\ value\: :ref:`int`\ ) - :ref:`int` **get_parsed_collision_mask**\ (\ ) 用于扫描静态碰撞器的物理层。 仅在 :ref:`parsed_geometry_type` 是 :ref:`PARSED_GEOMETRY_STATIC_COLLIDERS` 或 :ref:`PARSED_GEOMETRY_BOTH` 时才使用。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_property_parsed_geometry_type: .. rst-class:: classref-property :ref:`ParsedGeometryType` **parsed_geometry_type** = ``2`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_parsed_geometry_type**\ (\ value\: :ref:`ParsedGeometryType`\ ) - :ref:`ParsedGeometryType` **get_parsed_geometry_type**\ (\ ) 决定哪种类型的节点可解析为几何图形。可能的取值见 :ref:`ParsedGeometryType`\ 。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_property_source_geometry_group_name: .. rst-class:: classref-property :ref:`StringName` **source_geometry_group_name** = ``&"navigation_polygon_source_geometry_group"`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_source_geometry_group_name**\ (\ value\: :ref:`StringName`\ ) - :ref:`StringName` **get_source_geometry_group_name**\ (\ ) 应被解析以烘焙源几何体的节点的组名称。 只有当 :ref:`source_geometry_mode` 是 :ref:`SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN` 或 :ref:`SOURCE_GEOMETRY_GROUPS_EXPLICIT` 时才使用。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_property_source_geometry_mode: .. rst-class:: classref-property :ref:`SourceGeometryMode` **source_geometry_mode** = ``0`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_source_geometry_mode**\ (\ value\: :ref:`SourceGeometryMode`\ ) - :ref:`SourceGeometryMode` **get_source_geometry_mode**\ (\ ) 烘焙时使用的几何体的来源。可能的取值见 :ref:`SourceGeometryMode`\ 。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 方法说明 -------- .. _class_NavigationPolygon_method_add_outline: .. rst-class:: classref-method |void| **add_outline**\ (\ outline\: :ref:`PackedVector2Array`\ ) :ref:`🔗` 将包含轮廓顶点的 :ref:`PackedVector2Array` 追加到包含所有轮廓的内部数组。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_add_outline_at_index: .. rst-class:: classref-method |void| **add_outline_at_index**\ (\ outline\: :ref:`PackedVector2Array`, index\: :ref:`int`\ ) :ref:`🔗` 将一个包含轮廓顶点的 :ref:`PackedVector2Array` 添加到包含固定位置处的所有轮廓的内部数组。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_add_polygon: .. rst-class:: classref-method |void| **add_polygon**\ (\ polygon\: :ref:`PackedInt32Array`\ ) :ref:`🔗` 使用调用 :ref:`get_vertices` 得到的顶点的索引添加一个多边形。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_clear: .. rst-class:: classref-method |void| **clear**\ (\ ) :ref:`🔗` 清除顶点和多边形索引的内部数组。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_clear_outlines: .. rst-class:: classref-method |void| **clear_outlines**\ (\ ) :ref:`🔗` 清除轮廓数组,但不清除顶点和由顶点创建的多边形。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_clear_polygons: .. rst-class:: classref-method |void| **clear_polygons**\ (\ ) :ref:`🔗` 清除多边形数组,但不清除轮廓和顶点数组。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_get_navigation_mesh: .. rst-class:: classref-method :ref:`NavigationMesh` **get_navigation_mesh**\ (\ ) :ref:`🔗` 返回由该导航多边形产生的 :ref:`NavigationMesh`\ 。该导航网格可用于使用 :ref:`NavigationServer3D.region_set_navigation_mesh` API 直接更新区块的导航网格(因为 2D 在幕后使用 3D 服务器)。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_get_outline: .. rst-class:: classref-method :ref:`PackedVector2Array` **get_outline**\ (\ idx\: :ref:`int`\ ) |const| :ref:`🔗` 返回一个 :ref:`PackedVector2Array`\ ,其中包含在编辑器中或通过脚本创建的轮廓的顶点。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_get_outline_count: .. rst-class:: classref-method :ref:`int` **get_outline_count**\ (\ ) |const| :ref:`🔗` 返回在编辑器或脚本中创建的轮廓的数量。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_get_parsed_collision_mask_value: .. rst-class:: classref-method :ref:`bool` **get_parsed_collision_mask_value**\ (\ layer_number\: :ref:`int`\ ) |const| :ref:`🔗` 返回 :ref:`parsed_collision_mask` 中是否启用了指定的层,给定的 ``layer_number`` 应在 1 和 32 之间。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_get_polygon: .. rst-class:: classref-method :ref:`PackedInt32Array` **get_polygon**\ (\ idx\: :ref:`int`\ ) :ref:`🔗` 返回创建多边形的顶点索引,形式为 :ref:`PackedInt32Array`\ 。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_get_polygon_count: .. rst-class:: classref-method :ref:`int` **get_polygon_count**\ (\ ) |const| :ref:`🔗` 返回多边形的数量。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_get_vertices: .. rst-class:: classref-method :ref:`PackedVector2Array` **get_vertices**\ (\ ) |const| :ref:`🔗` 返回一个 :ref:`PackedVector2Array`\ ,其中包含用于创建多边形的所有顶点。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_make_polygons_from_outlines: .. rst-class:: classref-method |void| **make_polygons_from_outlines**\ (\ ) :ref:`🔗` **已弃用:** Use :ref:`NavigationServer2D.parse_source_geometry_data` and :ref:`NavigationServer2D.bake_from_source_geometry_data` instead. 从编辑器中添加的轮廓或通过脚本创建多边形。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_remove_outline: .. rst-class:: classref-method |void| **remove_outline**\ (\ idx\: :ref:`int`\ ) :ref:`🔗` 删除在编辑器或脚本中创建的轮廓。你必须调用 :ref:`make_polygons_from_outlines` 来更新多边形。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_set_outline: .. rst-class:: classref-method |void| **set_outline**\ (\ idx\: :ref:`int`, outline\: :ref:`PackedVector2Array`\ ) :ref:`🔗` 更改在编辑器或脚本中创建的轮廓。你必须调用 :ref:`make_polygons_from_outlines` 来更新多边形。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_set_parsed_collision_mask_value: .. rst-class:: classref-method |void| **set_parsed_collision_mask_value**\ (\ layer_number\: :ref:`int`, value\: :ref:`bool`\ ) :ref:`🔗` 根据 ``value``\ ,启用或禁用 :ref:`parsed_collision_mask` 中指定的层,给定的 ``layer_number`` 应在 1 和 32 之间。 .. rst-class:: classref-item-separator ---- .. _class_NavigationPolygon_method_set_vertices: .. rst-class:: classref-method |void| **set_vertices**\ (\ vertices\: :ref:`PackedVector2Array`\ ) :ref:`🔗` 设置顶点,可以使用 :ref:`add_polygon` 方法对其进行索引,创建多边形。 .. |virtual| replace:: :abbr:`virtual (本方法通常需要用户覆盖才能生效。)` .. |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 (无返回值。)`