admin管理员组文章数量:1287577
the problem is when I'm running the game it's working fine but then if i select one cell of the visual grid objects in the hierarchy the whole game slowly almost freezing.
the maze size array is 50, 50 and i checked using a breakpoint that it's creating the visual maze once in the start.
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<TextMeshProUGUI>();
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<TextMeshProUGUI>();
if (textComponent != null)
{
textComponent.text = maze[x, y] ? "Wall" : "Path";
textComponent.color = maze[x, y] ? Color.red : Color.green;
}
}
}
}
}
the visual maze is on the left i changed the panel size to make it smaller. and i selected one cell prefab and then the game become very slowly almost freezing.
here is a screenshot:
visual maze
本文标签:
版权声明:本文标题:c# - How to visual a grid with ui textmeshpro components if the grid is big with many cells? the game become slow and almost fre 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741311143a2371656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论