using System;

class Program
{
    static void Main(string[] args)
    {
        int a = 5;
        int b = 10;
        
        Console.WriteLine($"Before Swap: a = {a}, b = {b}");
        Swap(ref a, ref b);
        Console.WriteLine($"After Swap: a = {a}, b = {b}");
        
        string x = "hello";
        string y = "world";
        
        Console.WriteLine($"Before Swap: x = {x}, y = {y}");
        Swap(ref x, ref y);
        Console.WriteLine($"After Swap: x = {x}, y = {y}");
    }
    
    static void Swap<T>(ref T lhs, ref T rhs)
    {
        T temp;
        temp = lhs;
        lhs = rhs;
        rhs = temp;
    }
}

 

https://docs.unity3d.com/ScriptReference/EditorWindow.html

 

Unity - Scripting API: EditorWindow

Create your own custom editor window that can float free or be docked as a tab, just like the native windows in the Unity interface. Editor windows are typically opened using a menu item.

docs.unity3d.com

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class BlockSwapWindow : EditorWindow
{
    [MenuItem("Window/Block Swap Window")]
    
    public static void ShowWindow()
    {
        GetWindow<BlockSwapWindow>("Block Swap Window");
    }

    private void OnGUI()
    {
        GUILayout.Label("Swap Blocks", EditorStyles.boldLabel);
    }
}

 

 

 

https://docs.unity3d.com/ScriptReference/GUILayout.Label.html

 

Unity - Scripting API: GUILayout.Label

Labels have no user interaction, do not catch mouse clicks and are always rendered in normal style. If you want to make a control that responds visually to user input, use a Box control Label in the Game View.

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/EditorGUILayout.IntField.html

 

Unity - Scripting API: EditorGUILayout.IntField

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

 

 

 

using Codice.Client.BaseCommands.Filters;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class BlockSwapWindow : EditorWindow
{
    private int row1;
    private int col1;
    private int row2;
    private int col2;
    [MenuItem("Window/Block Swap Window")]
    
    public static void ShowWindow()
    {
        GetWindow<BlockSwapWindow>("Block Swap Window");
    }

    private void OnGUI()
    {
        GUILayout.Label("Swap Blocks", EditorStyles.boldLabel);

        row1 = EditorGUILayout.IntField("Row 1", row1);
        col1 = EditorGUILayout.IntField("Column 1", col1);
        row2 = EditorGUILayout.IntField("Row 2", row2);
        col2 = EditorGUILayout.IntField("Column 2", col2);
    }
}

 

 

using Codice.Client.BaseCommands.Filters;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

public class BlockSwapWindow : EditorWindow
{
    private int row1;
    private int col1;
    private int row2;
    private int col2;
    [MenuItem("Window/Block Swap Window")]
    
    public static void ShowWindow()
    {
        GetWindow<BlockSwapWindow>("Block Swap Window");
    }

    private void OnGUI()
    {
        GUILayout.Label("Swap Blocks", EditorStyles.boldLabel);

        row1 = EditorGUILayout.IntField("Row 1", row1);
        col1 = EditorGUILayout.IntField("Column 1", col1);
        row2 = EditorGUILayout.IntField("Row 2", row2);
        col2 = EditorGUILayout.IntField("Column 2", col2);

        //버튼 UI추가
        if (GUILayout.Button("Swap"))
        {
            //Board.instance.SwapBlocks(row1, col1, row2, col2);
            Debug.Log($"Swapped blocks at [{row1}, {col1} ane [{row2}, {col2}]");
        }

    }

}

 

이때 배열과 백터에 x, y를 잘 생각해야한다.

배열로 GetLengh(0)으로 하면 행이지만

벡터를 기준으로 했을 때 보면 y축이기 때문에 

Vector2의 매개변수의 순서가 바뀐다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class AtlasManager : MonoBehaviour
{
    public static AtlasManager instance;
    public SpriteAtlas blockAtlas;
    //싱글톤
    private void Awake()
    {
        //AtlasManager 클래스의 인스턴스를 instance에 할당
        instance = this;
    }
}

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Random = UnityEngine.Random;

public class Test : MonoBehaviour
{
    private Block[,] board; //1차원 배열이 Block들을 관리
    public GameObject blockPrefab;
    void Start()
    {
        CreateBoard();
    }

    private void CreateBoard()
    {
        //크기가 9인 BlockType의 1차원 배열 만들기
        board = new Block[5, 9]; // 2차원 배열로 변경
        //5행 9열의 Block타입의 2차원 배열 만들기
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                CreateBlock(i, j);
            }
        }
        PrintBoard(); 
    }

    private void CreateBlock(int row, int col)
    {
        Vector2 pos = new Vector2(col, row);
        Block.BlockType blockType = (Block.BlockType)Random.Range(0, 5);
        GameObject blockGo = Instantiate(blockPrefab);
        Block block = blockGo.GetComponent<Block>();
        block.Init(blockType);
        block.SetPosition(pos);

        //배열의 요소에 블록 넣기
        board[row, col] = block;
    }

    private void PrintBoard()
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < board.GetLength(0); i++)
        {
            for (int j = 0; j < board.GetLength(1); j++)
            {
                sb.Append($"({i},{j})"); // StringBuilder에 문자열 추가
            }
            sb.AppendLine(); // 새로운 행 추가
        }
        Debug.Log(sb); // StringBuilder에 저장된 문자열을 출력
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Block : MonoBehaviour
{
    public enum BlockType
    {
        Blue, Gray, Green, Pink, Yellow
    }

    public BlockType blockType;
    public SpriteRenderer spriteRenderer;
    public void Init(BlockType blockType)
    {
        this.blockType = blockType;
        //이미지 변경 
        ChangeSprite(blockType);
    }

    public void ChangeSprite(BlockType blockType)
    {
        //블록의 이름을 넣어서 아틀라스에서 같은 이름인 sprite를 찾고 할당
        Sprite sp =
            AtlasManager.instance.blockAtlas.GetSprite(blockType.ToString());
        spriteRenderer.sprite = sp;
    }

    public void SetPosition(Vector2 pos)
    {
        transform.position = pos;
    }
}

 

using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Mathematics;
using UnityEngine;

public class Block : MonoBehaviour
{
    public enum BlockType
    {
        Blue, Gray, Green, Pink, Yellow
    }

    public BlockType blockType;
    public SpriteRenderer spriteRenderer;
    public TMP_Text debugText;
    public void Init(BlockType blockType)
    {
        this.blockType = blockType;
        //이미지 변경 
        ChangeSprite(blockType);
    }

    public void ChangeSprite(BlockType blockType)
    {
        //블록의 이름을 넣어서 아틀라스에서 같은 이름인 sprite를 찾고 할당
        Sprite sp =
            AtlasManager.instance.blockAtlas.GetSprite(blockType.ToString());
        spriteRenderer.sprite = sp;
    }

    public void SetPosition(Vector2 pos)
    {
        transform.position = pos;
        var index = Position2Index(pos);
        debugText.text = $"[{index.row}, {index.col}]";
    }

    public static (int row, int col) Position2Index(Vector2 pos)
    {
        return ((int)pos.y, (int)pos.x);
    }

    public static (int x, int y) Index2Position(Vector2 index)
    {
        return ((int)index.x, (int)index.y);
    }
}

+ Recent posts