position | 网格单元格位置。 |
Vector3 Returns the center of the cell transformed into local space coordinates.
Gets the logical center coordinate of a Grid cell in local space. The logical center for a cell of the Tilemap is defined by the Tile Anchor of the Tilemap.
在矩形网格布局中,使用 Vector3Int 参数对 GridLayout.CellToLocal 进行的调用会返回表示单元格左下角的 Vector3 坐标。虽然在数学上是正确的,不过,像 GameObject 实例化为网格等情形,您可能会更希望获得单元格中心。
// Snap the GameObject to parent Tilemap center of cell using UnityEngine; using UnityEngine.Tilemaps;
public class ExampleClass : MonoBehaviour { void Start() { Tilemap tilemap = transform.parent.GetComponent<Tilemap>(); Vector3Int cellPosition = tilemap.LocalToCell(transform.localPosition); transform.localPosition = tilemap.GetCellCenterLocal(cellPosition); } }