using UnityEngine;
[CreateAssetMenu(menuName = "ChessData/Pieces")]
public class Pieces : ScriptableObject
{
public string xcord;
public string ycord;
public Sprite msprite;
public string piecetype;
public string pside;
public string startxcord;
public string startycord;
}
using System;
using UnityEngine;
using UnityEngine.UI;
public class PieceMove : MonoBehaviour
{
public GeneralBehaviour sc;
private Image tempimage;
private Pieces movingpiece;
private int ttimer;
public bool notoccupied;
public void Move(Pieces p, string cord)
{
p.xcord = cord[0].ToString();
p.ycord = cord[1].ToString();
sc.UpdateImg();
}
public void MoveType(string cord)
{
movingpiece = sc.pieces.Find(x => (x.xcord + x.ycord) == cord);
GameObject tempsquare = sc.squares.Find(x => x.name == (cord));
Debug.Log(movingpiece.name);
tempsquare.GetComponent<Button>().onClick.AddListener(() => CancelMove(tempsquare));
int tempxcord = Convert.ToInt32(movingpiece.xcord); int tempycord = Convert.ToInt32(movingpiece.ycord);
switch (movingpiece.piecetype)
{
case "rook":
TempMove(tempxcord, tempycord, "01");
TempMove(tempxcord, tempycord, "02");
TempMove(tempxcord, tempycord, "10");
TempMove(tempxcord, tempycord, "20");
break;
case "horse":
Check(tempxcord + 1, tempycord + 2);
Check(tempxcord + 1, tempycord - 2);
Check(tempxcord - 1, tempycord - 2);
Check(tempxcord - 1, tempycord + 2);
Check(tempxcord + 2, tempycord + 1);
Check(tempxcord + 2, tempycord - 1);
Check(tempxcord - 2, tempycord + 1);
Check(tempxcord - 2, tempycord - 1);
break;
case "bishop":
TempMove(tempxcord, tempycord, "11");
TempMove(tempxcord, tempycord, "12");
TempMove(tempxcord, tempycord, "21");
TempMove(tempxcord, tempycord, "22");
break;
case "pawn":
switch (movingpiece.pside)
{
case "w":
if (movingpiece.ycord == "2") { Check(tempxcord, tempycord + 2); }
Check(tempxcord, tempycord + 1);
break;
case "b":
if (movingpiece.ycord == "7") { Check(tempxcord, tempycord - 2); }
Check(tempxcord, tempycord - 1);
break;
}
break;
case "king":
Check(tempxcord + 1, tempycord);
Check(tempxcord, tempycord + 1);
Check(tempxcord + 1, tempycord + 1);
Check(tempxcord - 1, tempycord);
Check(tempxcord, tempycord - 1);
Check(tempxcord - 1, tempycord - 1);
Check(tempxcord + 1, tempycord - 1);
Check(tempxcord - 1, tempycord + 1);
break;
case "queen":
TempMove(tempxcord, tempycord, "11");
TempMove(tempxcord, tempycord, "12");
TempMove(tempxcord, tempycord, "21");
TempMove(tempxcord, tempycord, "22");
TempMove(tempxcord, tempycord, "01");
TempMove(tempxcord, tempycord, "02");
TempMove(tempxcord, tempycord, "10");
TempMove(tempxcord, tempycord, "20");
break;
}
}
private void TempMove(int xx, int yy, string id)
{
notoccupied = true;
switch(id)
{
case "01":
for (int ttimer = 1; notoccupied && ttimer <= 8; ttimer++) { Check(xx, yy + ttimer); }
break;
case "10":
for (int ttimer = 1; notoccupied && ttimer <= 8; ttimer++) { Check(xx + ttimer, yy); }
break;
case "11":
for (int ttimer = 1; notoccupied && ttimer <= 8; ttimer++) { Check(xx + ttimer, yy + ttimer); }
break;
case "21":
for (int ttimer = 1; notoccupied && ttimer <= 8; ttimer++) { Check(xx - ttimer, yy + ttimer); }
break;
case "12":
for (int ttimer = 1; notoccupied && ttimer <= 8; ttimer++) { Check(xx + ttimer, yy - ttimer); }
break;
case "20":
for (int ttimer = 1; notoccupied && ttimer <= 8; ttimer++) { Check(xx - ttimer, yy); }
break;
case "02":
for (int ttimer = 1; notoccupied && ttimer <= 8; ttimer++) { Check(xx, yy - ttimer); }
break;
case "22":
for (int ttimer = 1; notoccupied && ttimer <= 8; ttimer++) { Check(xx - ttimer, yy - ttimer); }
break;
}
}
public void CancelMove(GameObject sq)
{
sq.GetComponent<Button>().onClick.RemoveAllListeners();
sc.showsquares.Clear();
sc.UpdateImg();
}
private void Check(int xcord, int ycord)
{
if (1 <= xcord && xcord <= 8 && 1 <= ycord && ycord <= 8)
{
string tempcord = xcord.ToString() + ycord.ToString();
Pieces p = sc.pieces.Find(x => (x.xcord + x.ycord) == (tempcord));
if (p == null) { ShowMove(tempcord); } else { Debug.Log(tempcord + "occupied"); notoccupied = false; }
}
}
private void ShowMove(string cord)
{
GameObject tempsquare = sc.squares.Find(x => x.name == cord);
tempimage = tempsquare.GetComponent<Image>();
sc.UpdateImg();
sc.showsquares.Add(tempsquare);
tempsquare.GetComponent<Button>().onClick.AddListener(() => Move(movingpiece, cord));
}
}
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GeneralBehaviour : MonoBehaviour
{
public List<GameObject> squares;
public List<GameObject> showsquares;
public GameObject squareparent;
public List<Pieces> pieces;
private Image tempimage;
public Sprite empty;
public Sprite tempsprite;
public PieceMove pm;
void Start()
{
Screen.SetResolution(720, 720, false);
PieceReset();
UpdateImg();
}
public void UpdateImg()
{
for (int a = 0; a < squares.Count; a++)
{
GameObject tempsquare = squares[a];
tempimage = tempsquare.GetComponent<Image>();
tempimage.GetComponent<Image>().sprite = empty;
tempsquare.GetComponent<Button>().interactable = false;
}
for (int a = 0; a < showsquares.Count; a++)
{
GameObject tempsquare = showsquares[a];
tempimage = tempsquare.GetComponent<Image>();
tempsquare.GetComponent<Image>().sprite = tempsprite;
tempsquare.GetComponent<Button>().interactable = true;
}
for (int i = 0; i < pieces.Count; i++)
{
String piececord = pieces[i].xcord + pieces[i].ycord;
GameObject tempsquare = squares.Find(x => x.name == (piececord));
tempimage = tempsquare.GetComponent<Image>();
tempsquare.GetComponent<Image>().sprite = pieces[i].msprite;
tempsquare.GetComponent<Button>().onClick.AddListener(() => pm.CancelMove(tempsquare));
tempsquare.GetComponent<Button>().onClick.AddListener(() => pm.MoveType(piececord));
tempsquare.GetComponent<Button>().interactable = true;
}
}
private void PieceReset()
{
for (int i = 0; i < pieces.Count; i++)
{
pieces[i].xcord = pieces[i].startxcord; pieces[i].ycord = pieces[i].startycord;
}
}
}
After a few calls of MoveType() by clicking on chess pieces, Unity Editor increases its ram usage and freezes. I tried to remove the DataType Type = new DataType; kind of lines to decrease the creation of new lists/gameobjects to avoid lag, but it continues. Thanks for replies in advance.