Когда я нажимаю кнопку запуска игры в единстве, экран Unity замирает и постоянно увеличивается количество оперативной памяти. (Я думаю бесконечный l oop) Может ли кто-нибудь помочь с ошибкой в моем коде? (Без отладки) Большое спасибо. :) У меня есть префабы и префабы с изображением и текстом. (и не беспокойтесь, что я вставляю код в префабы и холст.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grid2 : MonoBehaviour
{
public int columns = 0;
public int rows = 0;
public float every_square_offset = 0.0f;
public GameObject grid_square;
public UnityEngine.Vector2 start_position = new UnityEngine.Vector2(0.0f, 0.0f);
private List<GameObject> grid_squares_ = new List<GameObject>();
void Start()
{
if (grid_square.GetComponent<GridSquare>() == null)
Debug.LogError("grid_square object need to have GridSquare script attached!");
CreateGrid();
SetGridNumbers();
}
private void CreateGrid()
{
SpawnGridSquares();
SetSquarePosition();
}
private void SpawnGridSquares()
{
//0, 1,3,4,5,6
//7,8,9,10 ...
for(int row = 0; row < rows; row++)
{
for(int column = 0; column < columns; columns++)
{
grid_squares_.Add(Instantiate(grid_square) as GameObject);
grid_squares_[grid_squares_.Count - 1].transform.parent = this.transform; // instantiate this game object as a child of the object holding this script.
}
}
}
private void SetSquarePosition()
{
var square_rect = grid_squares_[0].GetComponent<RectTransform>();
UnityEngine.Vector2 offset = new UnityEngine.Vector2();
offset.x = square_rect.rect.width * square_rect.transform.localScale.x + every_square_offset;
offset.y = square_rect.rect.height * square_rect.transform.localScale.y + every_square_offset;
int column_number = 0;
int row_number = 0;
foreach (GameObject square in grid_squares_)
{
if(column_number + 1 > columns)
{
row_number++;
column_number = 0;
}
var pos_x_offset = offset.x * column_number;
var pos_y_offset = offset.y * row_number;
square.GetComponent<RectTransform>().anchoredPosition = new UnityEngine.Vector3(start_position.x + pos_x_offset, start_position.y - pos_y_offset);
column_number++;
}
}
private void SetGridNumbers()
{
foreach(var square in grid_squares_)
{
square.GetComponent<GridSquare>().SetNumber(Random.Range(0, 10));
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GridSquare : MonoBehaviour
{
public GameObject number_text;
private int number_ = 0;
public void DisplayText()
{
if (number_ <= 0)
number_text.GetComponent<Text>().text = " ";
else
number_text.GetComponent<Text>().text = number_.ToString();
}
public void SetNumber(int number)
{
number_ = number;
}
}