Wie visuell ein Gitter mit UI -Textmeshpro -Komponenten, wenn das Netz mit vielen Zellen groß ist? Das Spiel wird langsaC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Wie visuell ein Gitter mit UI -Textmeshpro -Komponenten, wenn das Netz mit vielen Zellen groß ist? Das Spiel wird langsa

Post by Anonymous »

Das Problem ist, wenn ich das Spiel ausführe, funktioniert es gut Das Labyrinth -Größenarray ist 50, 50, und ich habe über einen Haltepunkt nachgesehen, dass es einmal das visuelle Labyrinth zu Beginn erstellt. < /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>
Das visuelle Labyrinth ist links Ich habe die Panelgröße geändert, um es kleiner zu machen. p>
Hier ist ein Screenshot: < /p>
visuelles Labyrinth < /p < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post