Закрашивание клеток Grid Tilemap в Unity.
Всем здравствуйте. Я делаю систему работы с Grid по этому видео:
https://rutube.ru/video/f7fd1022eb0dea40b8de56007a7e95fc/?r=plemwd . Проблема в том, что у него всё в 2D. Перемещение я поменял на 3D через Raycast.
Переменные и методы:
public Camera customCamera;
public Vector3 vec;
public GameObject ui_document;
public static GridBuildingSystem current;
public GridLayout gridLayout;
public Tilemap Maintilemap;
public float cellSize = 1.5f; // Размер клетки
public Transform gridTransform;
public Tilemap tilemap;
public Camera camera;
public Grid grid;
private static Dictionary<TileType, TileBase> tileBases = new Dictionary<TileType, TileBase>();
private Building_grid temp;
private Vector3 prevPos;
private BoundsInt prevArea;
public enum TileType
{
Empty,
White,
Green,
Red
}
private static void SetTilesBlock(BoundsInt area , TileType type ,Tilemap tilemap)
{
int size = area.size.x * area.size.y * area.size.z;
TileBase[] tileArray = new TileBase[size];
FillTiles(tileArray, type);
tilemap.SetTilesBlock(area, tileArray);
}
private static void FillTiles(TileBase[] arr , TileType type)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i] = tileBases[type];
}
}
private void ClearArea()
{TileBase[] toClear = new TileBase[prevArea.size.x * prevArea.size.y * prevArea.size.z];
FillTiles(toClear, TileType.Empty);
tilemap.SetTilesBlock(prevArea, toClear);
}
private void FollowBuilding()
{
ClearArea();
temp.area.position = gridLayout.WorldToCell(temp.gameObject.transform.position);
BoundsInt buildingArea = temp.area;
TileBase[] baseArray = GetTilesBlock(buildingArea, Maintilemap);
int size = baseArray.Length;
TileBase[] tileArray = new TileBase[size];
for(int i = 0; i < baseArray.Length; i++)
{
if (baseArray[i] == tileBases[TileType.White])
{
tileArray[i] = tileBases[TileType.Green];
}
else
{
FillTiles(tileArray, TileType.Red);
break;
}
}
tilemap.SetTilesBlock(buildingArea, tileArray);
prevArea = buildingArea;
}
Буду рад если поможете сделать закрашивание клеток в 3D.)
1. Получение позиции с помощью Raycast
```csharp
void Update()
{
if (Input.GetMouseButton(0)) // ЛКМ для закрашивания
{
Ray ray = customCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 hitPoint = hit.point;
Vector3Int cellPosition = gridLayout.WorldToCell(hitPoint);
PaintCell(cellPosition);
}
}
}
```
2. Метод закрашивания клетки:
```csharp
private void PaintCell(Vector3Int cellPosition)
{
TileBase[] currentTiles = tilemap.GetTilesBlock(new BoundsInt(cellPosition, new Vector3Int(1, 1, 1)));
if (currentTiles[0] == tileBases[TileType.White])
{
tilemap.SetTile(cellPosition, tileBases[TileType.Green]);
}
else
{
tilemap.SetTile(cellPosition, tileBases[TileType.Red]);
}
}
```
3. Обновление метода FollowBuilding:
```csharp
private void FollowBuilding()
{
ClearArea();
temp.area.position = gridLayout.WorldToCell(temp.gameObject.transform.position);
BoundsInt buildingArea = temp.area;
TileBase[] baseArray = GetTilesBlock(buildingArea, Maintilemap);
TileBase[] tileArray = new TileBase[baseArray.Length];
for(int i = 0; i < baseArray.Length; i++)
{
if (baseArray[i] == tileBases[TileType.White])
{
tileArray[i] = tileBases[TileType.Green];
}
else
{
FillTiles(tileArray, TileType.Red);
break;
}
}
tilemap.SetTilesBlock(buildingArea, tileArray);
prevArea = buildingArea;
}
```
Могут быть ошибки,програмирование недавно начал изучать.
Спасибо
,.!
для закрашивания клеток в 3d попробуй использовать mesh renderer и materials просто меняй цвет на нужный
.
сделай так чтобы при наведении на клетку она меняла цвет через материал или шейдер в 3д
для закрашивания клеток в 3d попробуй использовать mesh renderer и менять материал на лету
Не знаю)