Michael Christensen-Calvin / October 2023 (1310 Words, 8 Minutes)
List of All Common Classes
using UnityEngine; using UnityEngine.Tilemaps; using System.Collections.Generic; public static class CommonTilemap { public delegate bool TileMatchPredicate<T>(T tile) where T : TileBase; public static List<TileBase> GetAllTiles(Tilemap map, bool includeEmptyTiles = false) => GetAllTiles<TileBase>(map, includeEmptyTiles); public static List<T> GetAllTiles<T>(Tilemap map, bool includeEmptyTiles = false) where T : TileBase { List<T> tiles = new List<T>(); ForEachTile(map, (T tile) => { if (includeEmptyTiles || tile != null) { tiles.Add(tile); } }); return tiles; } public static List<T> GetAllTiles<T>(Tilemap map, TileMatchPredicate<T> predicate) where T : TileBase { List<T> tiles = new List<T>(); ForEachTile(map, (T tile) => { if (tile != null && predicate(tile)) { tiles.Add(tile); } }); return tiles; } public static List<Vector3Int> GetAllTilePositions<T>(Tilemap map, TileMatchPredicate<T> predicate) where T : TileBase { List<Vector3Int> result = new List<Vector3Int>(); ForEachTile(map, (Vector3Int pos) => { T tile = map.GetTile<T>(pos); if (tile != null && predicate(tile)) { result.Add(pos); } }); return result; } public static List<Vector3Int> GetSurroudingTilePositions(Vector3Int pos) { List<Vector3Int> result = new List<Vector3Int>(); // [ ] [ ] [ ] // [ ] [X] [ ] // [ ] [ ] [ ] result.Add(new Vector3Int(pos.x - 1, pos.y + 1, pos.z)); result.Add(new Vector3Int(pos.x, pos.y + 1, pos.z)); result.Add(new Vector3Int(pos.x + 1, pos.y + 1, pos.z)); result.Add(new Vector3Int(pos.x - 1, pos.y, pos.z)); result.Add(new Vector3Int(pos.x + 1, pos.y, pos.z)); result.Add(new Vector3Int(pos.x - 1, pos.y - 1, pos.z)); result.Add(new Vector3Int(pos.x, pos.y - 1, pos.z)); result.Add(new Vector3Int(pos.x + 1, pos.y - 1, pos.z)); return result; } public static void ForEachTile(Tilemap map, System.Action<TileBase> tileAction) => ForEachTile<TileBase>(map, tileAction); public static void ForEachTile<T>(Tilemap map, System.Action<T> tileAction) where T : TileBase => ForEachTile(map, (Vector3Int pos, Tilemap map) => { T tile = map.GetTile<T>(pos); tileAction?.Invoke(tile); }); public static void ForEachTile<T>(Tilemap map, System.Action<T, Vector3Int> tileAction) where T : TileBase => ForEachTile(map, (Vector3Int pos, Tilemap map) => { T tile = map.GetTile<T>(pos); tileAction?.Invoke(tile, pos); }); public static void ForEachTile<T>(Tilemap map, System.Action<Vector3Int> tileAction) where T : TileBase => ForEachTile(map, (Vector3Int pos, Tilemap map) => { T tile = map.GetTile<T>(pos); if (tile != null) { tileAction?.Invoke(pos); } }); public static void ForEachTile(Tilemap map, System.Action<Vector3Int> tileAction) => ForEachTile(map, (Vector3Int pos, Tilemap map) => { tileAction?.Invoke(pos); }); public static void ForEachTile(Tilemap map, System.Action<Vector3Int, Tilemap> tileAction) { // Foreach tilemap, we want to set the z value of its tiles. BoundsInt bounds = map.cellBounds; for (int i = 0; i < bounds.size.x; i++) { for (int j = 0; j < bounds.size.y; j++) { for (int k = 0; k < bounds.size.z; k++) { Vector3Int tilePos = new Vector3Int(bounds.min.x + i, bounds.min.y + j, bounds.min.z + k); tileAction?.Invoke(tilePos, map); } } } } public static Vector3Int RandomPositionInBounds(Tilemap map) { BoundsInt bounds = map.cellBounds; return new Vector3Int( x: Random.Range(bounds.min.x, bounds.max.x - 1), y: Random.Range(bounds.min.y, bounds.max.y - 1), z: Random.Range(bounds.min.z, bounds.max.z - 1)); } public static bool IsInBounds(Tilemap map, Vector3Int pos) { BoundsInt bounds = map.cellBounds; return pos.x > bounds.min.x && pos.x < bounds.max.x && pos.y > bounds.min.y && pos.y < bounds.max.y && pos.z > bounds.min.z && pos.z < bounds.max.z; } }