#ChessBoard

1 messages · Page 1 of 1 (latest)

civic bone
#
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;



[CreateAssetMenu(fileName = "PiecePlacementFile", menuName = "PiecePlacementFile", order = 1)]
public class PiecePlacementSO : ScriptableObject
{
    public Dictionary<string, ChesspieceSO> piecesPlacement = new Dictionary<string, ChesspieceSO>();

}

#if UNITY_EDITOR
[CustomEditor(typeof(PiecePlacementSO))]
public class PiecePlacementSOEditor : Editor
{

    int gridSize = 8;
    int spacing = 5;
    int labelOffset = 0;
    Vector2 cellDims = new Vector2(85, 40);
    Vector2 labelDims = new Vector2(45, 40);
    ChesspieceSO[,] tiles = new ChesspieceSO[8, 8];

    private PiecePlacementSO piecePlacementFile;

    private void Initialize()
    {
        if (tiles == null)
        {
            tiles = new ChesspieceSO[gridSize, gridSize];
        }

        if (piecePlacementFile.piecesPlacement == null)
        {
            piecePlacementFile.piecesPlacement = new Dictionary<string, ChesspieceSO>();
        }

        foreach (var pair in piecePlacementFile.piecesPlacement)
        {
            int i = gridSize - int.Parse(pair.Key[1].ToString());
            int j = (int)pair.Key[0] - (int)'A';
            tiles[i, j] = pair.Value;
        }
    }
    private void OnEnable()
    {
        //tiles = new ChesspieceSO[gridSize, gridSize];
        piecePlacementFile = (PiecePlacementSO)target;
        Initialize();
        EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
    }
#
    {
        // Save the changes when the editor is disabled
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.SetDirty(piecePlacementFile);
        EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
    }

    private void OnPlayModeStateChanged(PlayModeStateChange state)
    {
        if (state == PlayModeStateChange.ExitingEditMode || state == PlayModeStateChange.EnteredEditMode || state == PlayModeStateChange.EnteredPlayMode)
        {
            // Save the changes when entering or exiting play mode
            EditorUtility.SetDirty(piecePlacementFile);
        }
    }```