Question by
FlamingFox1307 · Jun 29, 2020 at 07:11 AM ·
switch-caseelse if
Is there a better way to do connected textures for a tileset?
I'm trying to make connected textures for a tile set, and I'm wondering if there's a better way than using a hundred else if statements. Would it be possible to use a switch-case with this?
using UnityEngine;
using UnityEngine.Tilemaps;
// Tile that displays a Sprite when it is alone and a different Sprite when it is orthogonally adjacent to the same NeighourTile
[CreateAssetMenu]
public class Block : TileBase
{
public Sprite block;
public Sprite blockR;
public Sprite blockRL;
public Sprite blockL;
public Sprite blockD;
public Sprite blockRD;
public Sprite blockLD;
public Sprite blockUD;
public Sprite blockU;
public Sprite blockUR;
public Sprite blockUL;
public Sprite blockULR;
public Sprite blockRLD;
public Sprite blockULD;
public Sprite blockURD;
public Sprite blockUDRL;
[Space]
public Sprite blockDLC;
public Sprite blockDRC;
public Sprite blockURC;
public Sprite blockULC;
[Space]
public Sprite blockUDRC;
public Sprite blockUDLC;
public Sprite blockURLC;
public Sprite blockDRLC;
[Space]
public Sprite blockUDRLC;
public override void RefreshTile(Vector3Int position, ITilemap tilemap)
{
for (int yd = -1; yd <= 1; yd++)
{
Vector3Int location = new Vector3Int(position.x, position.y + yd, position.z);
if (IsNeighbour(location, tilemap))
tilemap.RefreshTile(location);
}
for (int xd = -1; xd <= 1; xd++)
{
Vector3Int location = new Vector3Int(position.x + xd, position.y, position.z);
if (IsNeighbour(location, tilemap))
tilemap.RefreshTile(location);
}
}
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
{
tileData.sprite = block;
Vector3Int up = new Vector3Int(position.x, position.y + 1, position.z);
Vector3Int down = new Vector3Int(position.x, position.y - 1, position.z);
Vector3Int left = new Vector3Int(position.x - 1, position.y, position.z);
Vector3Int right = new Vector3Int(position.x + 1, position.y, position.z);
Vector3Int upRight = new Vector3Int(position.x + 1, position.y + 1, position.z);
Vector3Int downLeft = new Vector3Int(position.x - 1, position.y - 1, position.z);
Vector3Int upLeft = new Vector3Int(position.x - 1, position.y + 1, position.z);
Vector3Int downRight = new Vector3Int(position.x + 1, position.y - 1, position.z);
if (IsNeighbour(up, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(right, tilemap) && !IsNeighbour(left, tilemap))
{
tileData.sprite = blockUD;
}
else if (IsNeighbour(up, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(right, tilemap) && !IsNeighbour(left, tilemap))
{
tileData.sprite = blockU;
}
else if (IsNeighbour(down, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(right, tilemap) && !IsNeighbour(left, tilemap))
{
tileData.sprite = blockD;
}
else if (IsNeighbour(left, tilemap) && IsNeighbour(right, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(down, tilemap))
{
tileData.sprite = blockRL;
}
else if (IsNeighbour(left, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(right, tilemap))
{
tileData.sprite = blockL;
}
else if (IsNeighbour(right, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(left, tilemap))
{
tileData.sprite = blockR;
}
else if (IsNeighbour(up, tilemap) && IsNeighbour(right, tilemap) && !IsNeighbour(upRight, tilemap) && !IsNeighbour(left, tilemap) && !IsNeighbour(down, tilemap))
{
tileData.sprite = blockUR;
}
else if (IsNeighbour(up, tilemap) && IsNeighbour(left, tilemap) && !IsNeighbour(upLeft, tilemap) && !IsNeighbour(right, tilemap) && !IsNeighbour(down, tilemap))
{
tileData.sprite = blockUL;
}
else if (IsNeighbour(left, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(downLeft, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(right, tilemap))
{
tileData.sprite = blockLD;
}
else if (IsNeighbour(right, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(downRight, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(left, tilemap))
{
tileData.sprite = blockRD;
}
else if (IsNeighbour(up, tilemap) && IsNeighbour(right, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(left, tilemap))
{
tileData.sprite = blockUDRC;
}
else if (IsNeighbour(up, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(right, tilemap))
{
tileData.sprite = blockUDLC;
}
else if (IsNeighbour(right, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(up, tilemap) && !IsNeighbour(down, tilemap))
{
tileData.sprite = blockURLC;
}
else if (IsNeighbour(right, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(up, tilemap))
{
tileData.sprite = blockDRLC;
}
else if (IsNeighbour(right, tilemap) && IsNeighbour(up, tilemap) && IsNeighbour(upRight, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(left, tilemap))
{
tileData.sprite = blockURC;
}
else if (IsNeighbour(left, tilemap) && IsNeighbour(up, tilemap) && IsNeighbour(upLeft, tilemap) && !IsNeighbour(down, tilemap) && !IsNeighbour(right, tilemap))
{
tileData.sprite = blockULC;
}
else if (IsNeighbour(right, tilemap) && IsNeighbour(down, tilemap) && IsNeighbour(downRight, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(left, tilemap))
{
tileData.sprite = blockDRC;
}
else if (IsNeighbour(left, tilemap) && IsNeighbour(down, tilemap) && IsNeighbour(downLeft, tilemap) && !IsNeighbour(up, tilemap) && !IsNeighbour(right, tilemap))
{
tileData.sprite = blockDLC;
}
else if (IsNeighbour(right, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(up, tilemap) && IsNeighbour(down, tilemap) && IsNeighbour(upLeft, tilemap) && IsNeighbour(upLeft, tilemap) && IsNeighbour(downRight, tilemap) && IsNeighbour(downLeft, tilemap))
{
tileData.sprite = blockUDRLC;
}
else if (IsNeighbour(right, tilemap) && IsNeighbour(left, tilemap) && IsNeighbour(up, tilemap) && IsNeighbour(down, tilemap) && !IsNeighbour(upLeft, tilemap) && !IsNeighbour(upLeft, tilemap) && !IsNeighbour(downRight, tilemap) && !IsNeighbour(downLeft, tilemap))
{
tileData.sprite = blockUDRL;
}
else
{
tileData.sprite = block;
}
}
private bool IsNeighbour(Vector3Int position, ITilemap tilemap)
{
TileBase tile = tilemap.GetTile(position);
return (tile != null && tile == this);
}
}
Comment