I’m using the Unity tilemap, and want to create a function where you can click and drag te select tiles (the blue squares in my image)
I’m getting the tiles by using a Vector3Int (WorldToCell) to make it easy to fill in the tiles between the “startTile” and “endTile”
In my exemple the values are:
startTile: -1,-2,0
endTile: -4, -4, 0
On release of the mousebutton I like to fire a function for each tile.
So far I tried someting like this, but I walk into issues fast, Like, of course, this only works in the + direction, and when you make the selection smaller, the tiles keep being set.
if (Input.GetMouseButtonDown(0))
{
Debug.Log(tile);
startTile = tile;
isSelecting = true;
}
if (Input.GetMouseButtonUp(0))
{
isSelecting = false;
}
if (isSelecting)
{
endTile = tile;
for (int x = startTile.x; x < endTile.x; x++)
{
for (int y = startTile.y; y < endTile.y; y++)
{
Vector3Int localTile = new Vector3Int(x, y, 0);
digMap.SetTile(localTile, digTile);
}
}
}
I figure there must be a better method to do this. Any help or nudge in the right direction is greatly appreciated.
For now I fabricated something like this. Which is not the most elegant solution I guess. So tips or better solutions are still more than welcome
if (startTile.x <= endTile.x)
{
for (int x = startTile.x; x <= endTile.x; x++)
{
if (startTile.y <= endTile.y)
{
for (int y = startTile.y; y <= endTile.y; y++)
{
Vector3Int localTile = new Vector3Int(x, y, 0);
Function(localTile);
}
}
else if (endTile.y <= startTile.y)
{
for (int y = startTile.y; y >= endTile.y; y--)
{
Vector3Int localTile = new Vector3Int(x, y, 0);
Function(localTile);
}
}
}
}
else if (startTile.x >= endTile.x)
{
for (int x = startTile.x; x >= endTile.x; x--)
{
if (startTile.y <= endTile.y)
{
for (int y = startTile.y; y <= endTile.y; y++)
{
Vector3Int localTile = new Vector3Int(x, y, 0);
Function(localTile);
}
}
else if (endTile.y <= startTile.y)
{
for (int y = startTile.y; y >= endTile.y; y--)
{
Vector3Int localTile = new Vector3Int(x, y, 0);
Function(localTile);
}
}
}
}
For testing purposes I call a function, later I going to add the tiles to a list
// On Mouse Up
var bounds = GetMarqueeBounds(startTile, endTile);
for (var localTile in oldBounds.allPositionsWithin)
{
ClearFunction(localTile); // Clear Selection
}
for (var localTile in bounds.allPositionsWithin)
{
SetFunction(localTile); // Set Selection
}
oldBounds = bounds;