Unity/코드 예제들

타일맵의 셀좌표와 포지션 정보 얻어오기

소나무꼴 2019. 10. 10. 17:32
	public Camera _mainCamera;
    public Tilemap _tileMap;
    private Vector3 _clickPos = Vector3.zero;
    private Vector3Int _tilemapCell = Vector3Int.zero;

	IEnumerator OnMouseUp()
    {
        if (EventSystem.current.IsPointerOverGameObject() == true)
        {
            yield break;
        }

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);
        
        Vector3 scrSpace = Camera.main.WorldToScreenPoint(transform.position);
        Vector3 tilePos = _mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, scrSpace.z)) - transform.position;

        _tilemapCell = _tileMap.LocalToCell(tilePos);        
        _clickPos = _tileMap.GetCellCenterLocal(_tilemapCell);

        yield return null;
    }

 

타일맵을 클릭했을때 좌표에 셀 정보를 얻어 온다