Как визуализировать сетку с компонентами UI TextMeshPro, если сетка большая со многими клетками? игра становится медленнC#

Место общения программистов C#
Anonymous
 Как визуализировать сетку с компонентами UI TextMeshPro, если сетка большая со многими клетками? игра становится медленн

Сообщение Anonymous »

Проблема в том, когда я запускаю игру, она работает нормально, но затем, если я выберу одну ячейку визуальных объектов сетки в иерархии, вся игра медленно почти замерзает. < /p>
Массив размеров лабиринта составляет 50, 50, и я проверил, используя точку останова, что он создает визуальный лабиринт один раз в начале. < /p>
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class MazeGridUI : MonoBehaviour
{
public MazeGenerator mazeGenerator;
public GameObject cellPrefab; // Assign the new MazeCell prefab
public RectTransform gridParent; // Assign the Panel with Grid Layout Group

private GameObject[,] cellGrid; // Store references to created cells

private void Start()
{
if (mazeGenerator == null || cellPrefab == null || gridParent == null)
{
Debug.LogError("MazeGridUI: Missing references!");
return;
}

GenerateGrid();
}

private void GenerateGrid()
{
bool[,] maze = mazeGenerator.GetMazeArray(); // Get maze grid
int width = maze.GetLength(0);
int height = maze.GetLength(1);

// If cells were already created, reuse them instead of destroying them
if (cellGrid != null && cellGrid.GetLength(0) == width && cellGrid.GetLength(1) == height)
{
UpdateGrid(maze);
return;
}

// Clear previous UI elements if they exist
foreach (Transform child in gridParent)
{
Destroy(child.gameObject);
}

// Create new grid and store references
cellGrid = new GameObject[width, height];

for (int y = height - 1; y >= 0; y--) // Invert Y for proper display
{
for (int x = 0; x < width; x++)
{
GameObject cell = Instantiate(cellPrefab, gridParent);
cellGrid[x, y] = cell; // Store reference for later reuse

TextMeshProUGUI textComponent = cell.GetComponentInChildren();
if (textComponent != null)
{
textComponent.text = maze[x, y] ? "Wall" : "Path";
textComponent.color = maze[x, y] ? Color.red : Color.green; // Red for walls, green for paths
}
}
}
}

private void UpdateGrid(bool[,] maze)
{
int width = maze.GetLength(0);
int height = maze.GetLength(1);

for (int y = height - 1; y >= 0; y--)
{
for (int x = 0; x < width; x++)
{
TextMeshProUGUI textComponent = cellGrid[x, y].GetComponentInChildren();
if (textComponent != null)
{
textComponent.text = maze[x, y] ? "Wall" : "Path";
textComponent.color = maze[x, y] ? Color.red : Color.green;
}
}
}
}
}
< /code>
Визуальный лабиринт слева я изменил размер панели, чтобы сделать ее меньше.
и я выбрал одну сотовую префаву, а затем игра становится очень медленно почти замораживающей. < / / p>
вот скриншот: < /p>
Визуальный лабиринт < /p>

Подробнее здесь: https://stackoverflow.com/questions/794 ... -with-many

Вернуться в «C#»