Version: 2022.2
言語: 日本語
public Vector3 GetCellCenterWorld (Vector3Int position);

パラメーター

position Grid cell position.

戻り値

Vector3 Returns the center of the cell transformed into world space coordinates.

説明

Gets the logical center coordinate of a Grid cell in world space. The logical center for a cell of the Tilemap is defined by the Tile Anchor of the Tilemap.

In a rectangular grid layout, a call to GridLayout.CellToWorld with Vector3Int parameter returns a Vector3 coordinate that represents the lower left of the cell. This is mathematically correct, but in certain cases such as when instantiating a GameObject into the grid, you may prefer the center of the cell instead.

// 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.WorldToCell(transform.position); transform.position = tilemap.GetCellCenterWorld(cellPosition); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961