Код: Выделить всё
using System.Collections.Generic;
using UnityEngine;
public class Pieces : MonoBehaviour
{
public static GameObject[] pieceContainers;
public static GameObject[] pieceTemplates;
public static Sprite[] sprites;
public static Vector2[,] locPoss;
GraphicalChessBoard graphicalChessBoard;
BoardMemory memoryBoard;
Piece[,] board;
[SerializeField] GameObject chessBoard;
[SerializeField] GameObject boardMemory;
public string fen;
// Sprites for different pieces
public static Sprite r, n, bi, q, k, p, R, N, BI, Q, K, P;
void Awake()
{
graphicalChessBoard = chessBoard.GetComponent();
memoryBoard = boardMemory.GetComponent();
// Load sprites
r = Resources.Load("Chess Pieces/rookb");
n = Resources.Load("Chess Pieces/knightb");
bi = Resources.Load("Chess Pieces/bishopb");
q = Resources.Load("Chess Pieces/queenb");
k = Resources.Load("Chess Pieces/kingb");
p = Resources.Load("Chess Pieces/pawnb");
R = Resources.Load("Chess Pieces/rookw");
N = Resources.Load("Chess Pieces/knightw");
BI = Resources.Load("Chess Pieces/bishopw");
Q = Resources.Load("Chess Pieces/queenw");
K = Resources.Load("Chess Pieces/kingw");
P = Resources.Load("Chess Pieces/pawnw");
sprites = new Sprite[] { r, n, bi, q, k, p, R, N, BI, Q, K, P };
CreatePieceContainersAndTemplates();
board = memoryBoard.Board;
}
void Start()
{
locPoss = graphicalChessBoard.GetSquarePoss();
// Load a custom FEN or default
// fen = "r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1";
DrawAllPiecesFromMemory(board);
}
void CreatePieceContainersAndTemplates()
{
pieceContainers = new GameObject[sprites.Length];
pieceTemplates = new GameObject[sprites.Length];
for (int i = 0; i < sprites.Length; i++)
{
// Container (holds clones)
GameObject container = new GameObject(sprites[i].name + "_Container");
container.transform.parent = this.transform;
container.transform.localPosition = Vector3.zero;
pieceContainers[i] = container;
// Template (used to instantiate clones)
GameObject template = new GameObject(sprites[i].name + "_Template");
template.transform.parent = this.transform;
template.transform.localPosition = Vector3.zero;
pieceTemplates[i] = template;
}
}
public void DrawPiece(GameObject parentContainer, GameObject template, Vector2 localPos, Sprite sprite)
{
GameObject piece = Instantiate(template);
piece.transform.parent = parentContainer.transform;
piece.transform.localPosition = localPos;
piece.transform.localScale = new Vector3(0.125f, 0.125f, 1);
SpriteRenderer renderer = piece.AddComponent();
renderer.sprite = sprite;
renderer.sortingOrder = 2;
}
public void ClearAllPieceContainers()
{
foreach (GameObject container in pieceContainers)
{
foreach (Transform child in container.transform)
{
Destroy(child.gameObject);
}
}
}
public void DrawAllPiecesFromMemory(Piece[,] board)
{
ClearAllPieceContainers();
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
Piece piece = board[row, col];
if (piece == null)
continue;
int index = GetPieceIndex(piece.type);
if (index >= 0 && index < sprites.Length)
{
DrawPiece(pieceContainers[index], pieceTemplates[index], locPoss[row, col], sprites[index]);
}
}
}
}
// Match piece character to index in sprite/template arrays
int GetPieceIndex(char type)
{
switch (type)
{
case 'r': return 0;
case 'n': return 1;
case 'b': return 2;
case 'q': return 3;
case 'k': return 4;
case 'p': return 5;
case 'R': return 6;
case 'N': return 7;
case 'B': return 8;
case 'Q': return 9;
case 'K': return 10;
case 'P': return 11;
default: return -1;
}
}
}
Код: Выделить всё
using System;
using JetBrains.Annotations;
using UnityEngine;
public class BoardMemory : MonoBehaviour
{
public static string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
public Piece BRook = new Piece(Piece.rook_b);
public Piece BKnight = new Piece(Piece.knight_b);
public Piece BBishop = new Piece(Piece.bishop_b);
public Piece BQueen = new Piece(Piece.queen_b);
public Piece BKing = new Piece(Piece.king_b);
public Piece BPawn = new Piece(Piece.pawn_b);
public Piece WRook = new Piece(Piece.rook_w);
public Piece WKnight = new Piece(Piece.knight_w);
public Piece WBishop = new Piece(Piece.bishop_w);
public Piece WQueen = new Piece(Piece.queen_w);
public Piece WKing = new Piece(Piece.king_w);
public Piece WPawn = new Piece(Piece.pawn_w);
public Piece[,] Board { get; private set; }
// Start is called once before the first execution of Update after the MonoBehaviour is created
[Obsolete]
void Start()
{
Debug.Log("memory is running...");
Board = createChessBoard(fen);
}
public Piece[,] createChessBoard(string fen)
{
Piece[,] chessBoard = new Piece[8, 8];
int rowCount = 0;
int colCount = 0;
foreach (char element in fen)
{
if (element == '/') { rowCount += 1; colCount = 0; }
else if (element == 'r') { chessBoard[rowCount, colCount] = BRook; colCount += 1; }
else if (element == 'n') { chessBoard[rowCount, colCount] = BKnight; colCount += 1; }
else if (element == 'b') { chessBoard[rowCount, colCount] = BBishop; colCount += 1; }
else if (element == 'q') { chessBoard[rowCount, colCount] = BQueen; colCount += 1; }
else if (element == 'k') { chessBoard[rowCount, colCount] = BKing; colCount += 1; }
else if (element == 'p') { chessBoard[rowCount, colCount] = BPawn; colCount += 1; }
else if (element == 'R') { chessBoard[rowCount, colCount] = WRook; colCount += 1; }
else if (element == 'N') { chessBoard[rowCount, colCount] = WKnight; colCount += 1; }
else if (element == 'B') { chessBoard[rowCount, colCount] = WBishop; colCount += 1; }
else if (element == 'Q') { chessBoard[rowCount, colCount] = WQueen; colCount += 1; }
else if (element == 'K') { chessBoard[rowCount, colCount] = WKing; colCount += 1; }
else if (element == 'P') { chessBoard[rowCount, colCount] = WPawn; colCount += 1; }
else
{
int num = Convert.ToInt32(Char.GetNumericValue(element));
for (int _ = 0; _ < num; _++)
{
chessBoard[rowCount, colCount] = null;
colCount += 1;
}
}
}
return chessBoard;
}
public Piece GetPiece(int row, int col)
{
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
return Board[row, col];
}
return null;
}
}
public class Piece
{
public char type;
public Piece(char piecetype)
{
type = piecetype;
}
public const char king_b = 'k';
public const char queen_b = 'q';
public const char rook_b = 'r';
public const char bishop_b = 'b';
public const char knight_b = 'n';
public const char pawn_b = 'p';
public const char king_w = 'K';
public const char queen_w = 'Q';
public const char rook_w = 'R';
public const char bishop_w = 'B';
public const char knight_w = 'N';
public const char pawn_w = 'P';
public const string starting_pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
}
NullReferenceException: Object reference not set to an instance of an object
Pieces.DrawAllPiecesFromMemory (Piece[,] board) (at Assets/Pieces.cs:112)
Pieces.Start () (at Assets/pieces.cs: 57)
Подробнее здесь: https://stackoverflow.com/questions/796 ... in-c-sharp