using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Block))]
public class BlockEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Block block = target as Block;
if (GUILayout.Button("제거"))
{
Debug.Log($"[{block.row},{block.col}]을 제거 합니다.");
}
}
}
OnInspectorGUI 메서드는 Unity의 Inspector에서 해당 컴포넌트(Block)의 GUI를 그리는 메서드
base.OnInspectorGUI()는 기본적으로 해당 컴포넌트의 Inspector를 그리는 기능을 수행
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Board))]
public class BoardEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Board board = target as Board;
if (GUILayout.Button("배열 요소 출력"))
{
board.PrintBoard();;
}
}
}
public void PrintBoard()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < board.GetLength(0); i++) //행
{
for (int j = 0; j < board.GetLength(1); j++) //열
{
Block block = board[i, j];
string strElement = (block == null) ? "null" : block.blockType.ToString();
sb.Append($"[{i},{j}] = {strElement} ");
}
sb.AppendLine();
}
Debug.Log(sb);
}
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 UnityEngine.U2D;
using Random = UnityEngine.Random;
public class Test : MonoBehaviour
{
public Board board;
private void Start()
{
board.CreateBoard();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 1);
RaycastHit2D raycastHit2D = Physics2D.Raycast(ray.origin, ray.direction);
if (raycastHit2D.collider != null)
{
Block block = raycastHit2D.collider.GetComponent<Block>();
Debug.Log($"[{block.row}, {block.col}] ({block.transform.position.x}, {block.transform.position.y}), {block.blockType}");
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Random = UnityEngine.Random;
public class Board : MonoBehaviour
{
private Block[,] board; //1차원 배열이 Block들을 관리
public GameObject blockPrefab;
public void CreateBoard()
{
//크기가 9인 BlockType의 1차원 배열 만들기
board = new Block[5, 9]; // 2차원 배열로 변경
//5행 9열의 Block타입의 2차원 배열 만들기
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
CreateBlock(i, j);
}
}
PrintBoard();
}
public 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;
}
public 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 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 int row;
public int col;
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);
row = index.row;
col = index.col;
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);
}
}
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 UnityEngine.U2D;
using Random = UnityEngine.Random;
public class Test : MonoBehaviour
{
public Board board;
private void Start()
{
board.CreateBoard();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Random = UnityEngine.Random;
public class Board : MonoBehaviour
{
private Block[,] board; //1차원 배열이 Block들을 관리
public GameObject blockPrefab;
public void CreateBoard()
{
//크기가 9인 BlockType의 1차원 배열 만들기
board = new Block[5, 9]; // 2차원 배열로 변경
//5행 9열의 Block타입의 2차원 배열 만들기
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
CreateBlock(i, j);
}
}
PrintBoard();
}
public 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;
}
public 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 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);
}
}
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);
}
}
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Unity.VisualScripting;
using UnityEngine;
public class Test : MonoBehaviour
{
private Block[,] board; //1차원 배열이 Block들을 관리
public GameObject blockPrefab;
void Start()
{
CreateBoard();
//PrintBoard();
}
private void CreateBoard()
{
//크기가 9인 BlockType의 1차원 배열 만들기
board = new Block[5, 9]; // 2차원 배열로 변경
Debug.LogFormat("현재 보드의 칸의 총 개수 : {0}", board.Length); // 전체 길이
Debug.LogFormat("행의 개수 : {0} ", board.GetLength(0));
Debug.LogFormat("열의 개수 : {0} ", board.GetLength(1));
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에 저장된 문자열을 출력
//배열의 요소에 넣기(0~8까지의 랜덤한 값을 BlockType으로 바꿔서)
//for (int i = 0; i < board.Length; i++)
//{
// Block.BlockType blockType = (Block.BlockType)Random.Range(0, 5);
// //블록 프리팹 인스턴스를 생성
// GameObject blockGo = Instantiate(blockPrefab);
// //생성된 프리팹을 블록클래스의 블록에 할당
// Block block = blockGo.GetComponent<Block>();
// block.Init(blockType);
// block.SetPosition(i);
//}
}
//private void PrintBoard()
//{
//
//
//
//
//
//
//}
}
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.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(int x)
{
Vector2 pos = transform.position;
pos.x = x;
transform.position = pos;
}
}
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Unity.VisualScripting;
using UnityEngine;
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차원 배열로 변경
PrintBoard();
//배열의 요소에 넣기(0~8까지의 랜덤한 값을 BlockType으로 바꿔서)
//for (int i = 0; i < board.Length; i++)
//{
// Block.BlockType blockType = (Block.BlockType)Random.Range(0, 5);
// //블록 프리팹 인스턴스를 생성
// GameObject blockGo = Instantiate(blockPrefab);
// //생성된 프리팹을 블록클래스의 블록에 할당
// Block block = blockGo.GetComponent<Block>();
// block.Init(blockType);
// block.SetPosition(i);
//}
}
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;
using UnityEngine.U2D;
public class AtlasManager : MonoBehaviour
{
public static AtlasManager instance;
public SpriteAtlas blockAtlas;
//싱글톤
private void Awake()
{
//AtlasManager 클래스의 인스턴스를 instance에 할당
instance = this;
}
}
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(int x)
{
Vector2 pos = transform.position;
pos.x = x;
transform.position = pos;
}
}
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Unity.VisualScripting;
using UnityEngine;
public class Test : MonoBehaviour
{
private Block[] board; //1차원 배열이 Block들을 관리
public GameObject blockPrefab;
void Start()
{
CreateBoard();
PrintBoard();
}
private void CreateBoard()
{
//크기가 9인 BlockType의 1차원 배열 만들기
board = new Block[9];
//배열의 요소에 넣기(0~8까지의 랜덤한 값을 BlockType으로 바꿔서)
for (int i = 0; i < board.Length; i++)
{
Block.BlockType blockType = (Block.BlockType)Random.Range(0, 5);
//블록 프리팹 인스턴스를 생성
GameObject blockGo = Instantiate(blockPrefab);
//생성된 프리팹을 블록클래스의 블록에 할당
Block block = blockGo.GetComponent<Block>();
block.Init(blockType);
block.SetPosition(i);
}
}
private void PrintBoard()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < board.Length; i++)
{
sb.Append($"{board[i]}");
}
Debug.Log(sb);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stet39
{
internal class Program
{
static void Main(string[] args)
{
//Position pos = new Position(1,1);
//Position pos = new Position();
//Position pos;
//pos.x = 1;
//pos.y = 1;
//Console.WriteLine(pos);
//Position pos = new Position(1,1);
//pos.SetOrigin();
//Console.WriteLine("{0}, {1}", pos.x, pos.y);
Marine marine = new Marine(new Position(1,1));
Console.WriteLine("마린의 현재 위치 : {0}, {1}", marine.position.x, marine.position.y); ;
marine.Move(new Position(2, 3));
Position pos = marine.GetPostion();
Console.WriteLine("마린의 현재 위치 : {0}, {1}", pos.x, pos.y);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stet39
{
struct Position
{
public int x;
public int y;
public Position(int x, int y)//매개 변수 없는 생성자 사용불가
{
this.x = x;
this.y = y;
}
public void SetOrigin()
{
this.x = 0;
this.y = 0;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stet39
{
internal class Marine
{
public Position position;
public Marine(Position position)
{
this.position = position;
}
public void Move(Position targetPosition)
{
Console.WriteLine("{0}, {1}로 이동합니다", targetPosition.x, targetPosition.y);
this.position = targetPosition;
}
public Position GetPostion()
{
return this.position;
}
}
}