Skip to content

Instantly share code, notes, and snippets.

@Tunied
Created May 1, 2022 11:02
Show Gist options
  • Save Tunied/a633bb68493423d5859343262e577187 to your computer and use it in GitHub Desktop.
Save Tunied/a633bb68493423d5859343262e577187 to your computer and use it in GitHub Desktop.
Tilemap World和Local的空间转换
/// <summary>
/// World -> World
/// 比如房间的墙体放在Map里面去Gen,在Logic层存储的都是WorldTileIndex,这样和Modify的Tile去混合时候都可以使用一套相同的坐标系
/// </summary>
public static Vector2Int WorldPos_To_WorldTileIndex(Vector3 _worldPos) { return new(Mathf.FloorToInt(_worldPos.x), Mathf.FloorToInt(_worldPos.y)); }
/// <summary>
/// 把Tilemap的局部Index转到世界做标下
/// </summary>
public static Vector2Int LocalIndexToWorldIndex(this Tilemap _target, Vector2Int _localIndex)
{
var tileCenterWorldPosV3 = _target.GetCellCenterWorld(new Vector3Int(_localIndex.x, _localIndex.y, 0));
return WorldPos_To_WorldTileIndex(tileCenterWorldPosV3);
}
/// <summary>
/// 世界TileIndex->Local TileIndex
/// </summary>
public static Vector3Int WorldIndexToLocalIndex(this Tilemap _target, Vector2Int _worldIndex)
{
var worldTileCenterPos = new Vector3(_worldIndex.x + 0.5f, _worldIndex.y + 0.5f, 0);
return _target.WorldToCell(worldTileCenterPos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment