Grid.GetCellCenterWorld

切换到手册
public Vector3 GetCellCenterWorld (Vector3Int position);

参数

position网格单元格位置。

返回

Vector3 变换为世界空间坐标的单元格中心。

描述

获取世界空间中网格单元格的逻辑中心坐标。

在矩形网格布局中,使用 Vector3Int 参数对 GridLayout.CellToWorld 进行的调用会返回表示单元格左下角的 Vector3 坐标。这在数学上是正确的,不过,像 GameObject 实例化为网格等情形,您通常会更希望获得单元格中心。

// Snap the GameObject to parent Grid center of cell
using UnityEngine;

public class ExampleClass : MonoBehaviour { void Start() { Grid grid = transform.parent.GetComponent<Grid>(); Vector3Int cellPosition = grid.WorldToCell(transform.position); transform.position = grid.GetCellCenterWorld(cellPosition); } }