S
S
Sergey Ivanov2019-06-12 16:13:48
C++ / C#
Sergey Ivanov, 2019-06-12 16:13:48

Unity how to make game mesh loading?

There is a game in the form of Math2048
-FX1iMNP1yI.jpg
It has a script responsible for the information of cubes

spoiler
using System.Collections;
using UnityEngine;

public class Match3Node : MonoBehaviour {
    public int ids;
    public int Cost;
  public SpriteRenderer sprite; // спрайт узла
  public GameObject highlight; // объект подсветки узла
  public int id { get; set; }
  public bool ready { get; set; }
  public int x { get; set; }
  public int y { get; set; }
}

I do the initial structure of the field like this:
spoiler
[SerializeField] private Mode mode; // два режима перемещения, 'MatchOnly' означает, что передвинуть узел можно если произошло совпадение, иначе произойдет возврат
  [SerializeField] private float speed = 5.5f; // скорость движения объектов
  [SerializeField] private float destroyTimeout = .5f; // пауза в секундах, перед тем как уничтожить совпадения
  [SerializeField] private LayerMask layerMask; // маска узла (префаба)
  [SerializeField] private Color[] color; // набор цветов/id
    [SerializeField] private int gridWidth = 7; // ширина игрового поля
  [SerializeField] private int gridHeight = 10; // высота игрового поля
  [SerializeField] private Match3Node sampleObject; // образец узла (префаб)
  [SerializeField] private float sampleSize = 1; // размер узла (ширина и высота)
    private int CCCost;
    private int[] Numbers = { 1, 2, 3, 4, 6, 8};
  private Match3Node[,] grid;
  private Match3Node[] nodeArray;
  private Vector3[,] position;
  private Match3Node current, last;
  private Vector3 currentPos, lastPos;
  private List<Match3Node> lines;
  private bool isLines, isMove, isMode;
  private float timeout;
    //FFFFFF
    private List<Match3Node>[] xodList;
  void Start()
  {
        //for(int i = 0; i < 10; i++)
        //{
        //    color1[i]= new Color(Random.Range(0,255), Random.Range(0, 255), Random.Range(0, 255));
        //}
        // создание игрового поля (2D массив) с заданными параметрами
        grid = Create2DGrid<Match3Node>(sampleObject, gridWidth, gridHeight, sampleSize, transform);
        //FFFFFF
        xodList = new List<Match3Node>[gridHeight * gridWidth];
    

    SetupField();
  }


void SetupField() // стартовые установки, подготовка игрового поля
  {
    position = new Vector3[gridWidth, gridHeight];
    nodeArray = new Match3Node[gridWidth * gridHeight];

    int i = 0;
    int id = -1;
    int step = 0;

    for(int y = 0; y < gridHeight; y++)
    {
      for(int x = 0; x < gridWidth; x++)
      {
        int j = Random.Range(0, color.Length);
        if(id != j) id = j; else step++;
        if(step > 2)
        {
          step = 0;
          id = (id + 1 < color.Length-1) ? id + 1 : id - 1;
        }

        grid[x, y].ready = false;
        grid[x, y].x = x;
        grid[x, y].y = y;
        grid[x, y].id = id;
        grid[x, y].sprite.color = color[id];
        grid[x, y].gameObject.SetActive(true);
        grid[x, y].highlight.SetActive(false);
                CCCost = Numbers[id];
                grid[x, y].Cost = Numbers[id];
                grid[x, y].GetComponentInChildren<Text>().text = Numbers[id].ToString();
                position[x, y] = grid[x, y].transform.position;
        nodeArray[i] = grid[x, y];
        i++;
      }
    }

    current = null;
    last = null;
  }

Here is how I save:
public void SavePreviousMoves() // сохранение игрового поля
    {
        previousMovesList.Clear();
        for (int i = 0; i < nodeArray.Length; i++)
        {
            previousMovesList.Add(nodeArray[i]);
        }


    }

I want to make it possible to cancel moves, that is, in the array I have the data of the last move, how can I load them
ps I tried to simply assign the data, but then everything broke

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question