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

本文标签: