생각들,
퍼즐 구성 자체를 처음해봐서 그런지 전혀 감도 안잡히다가 강사님이 말씀해주신 Raycast로 좌표찍기를 했는데 좌표가 계속 (0,0) 이 나와서 어떻게 해야할지 몰랐다.
해결하게된 방법 -
후 코드
TestSaveLocation이라는 스크립트를 Block과 Wall Prefab에 넣어두고 생성이 될 때마다 GetLocation이라는 메서드로 위치 정보를 (0,0), (0,1) ... 이렇게 계속 저장해주었고
PutLocation이라는 메서드로 위치정보를 Debug.Log로 찍어주는 메서드였다. 근데 계속 (0,0)이 나와서 왜 그런지..
몇시간 동안 계속 헷갈렸는데 이유를 알아냈다.
전 코드
계속 Wall이랑 Block을 찍어서 gameObject를 받아와도 TestMain 함수 내에서 TestSaveLocation을 받고있어서 당연히 hit.collider.gameObject로 Component를 받아왔어야했는데 생각이 짧았다.
뭔가 계속 생각하던게 머리속으로는 이렇게 하는게 생성할 때 Row, Col값 주고 하는게 맞는데 왜 안되는건지
이해가 안되고 있다가 갑자기 생각이 나서 해결하게되었다.
Script
TestMain
역할 - 2차원 배열을 구성하고 2차원 배열 str[row, col]값이 0, 1, 2, 3, 4, 5 라면 TestBlock에서 메서드를 불러와
Prefab들을 instantiate 해주고 위치지정을 해주는 역할을 가지고있다.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class TestMain : MonoBehaviour
{
[SerializeField]
private GameObject wallPrefab;
[SerializeField]
private GameObject blockPrefab;
[SerializeField]
private Transform boardTr;
private TestBlock testBlock;
private int[,] str =
{
{0, 0, 4, 2, 4, 0, 0},
{0, 0, 5, 1, 2, 0, 0},
{0, 0, 5, 4, 3, 0, 0},
{0, 0, 1, 2, 3, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0}
};
// Start is called before the first frame update
void Start()
{
int maxRow = 7;
int maxCol = 7;
this.testBlock = new TestBlock(maxRow, maxCol);
for(int row = 0; row < maxRow; row++)
{
for(int col = 0; col < maxCol; col++)
{
switch (str[row, col])
{
case 0:
testBlock.Move(row, col);
testBlock.InstantiateObj(this.wallPrefab, this.boardTr, TestBlock.eBlockType.WALL);
break;
case 1:
testBlock.Move(row, col);
testBlock.InstantiateObj(this.blockPrefab, this.boardTr, TestBlock.eBlockType.RED);
break;
case 2:
testBlock.Move(row, col);
testBlock.InstantiateObj(this.blockPrefab, this.boardTr, TestBlock.eBlockType.BLUE);
break;
case 3:
testBlock.Move(row, col);
testBlock.InstantiateObj(this.blockPrefab, this.boardTr, TestBlock.eBlockType.GREEN);
break;
case 4:
testBlock.Move(row, col);
testBlock.InstantiateObj(this.blockPrefab, this.boardTr, TestBlock.eBlockType.YELLOW);
break;
case 5:
testBlock.Move(row, col);
testBlock.InstantiateObj(this.blockPrefab, this.boardTr, TestBlock.eBlockType.PURPLE);
break;
}
}
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
if (hit.collider.CompareTag("Wall") || hit.collider.CompareTag("Block"))
{
//좌표찍기
TestSaveLocation location = hit.collider.gameObject.GetComponent<TestSaveLocation>();
location.PutLocation();
//Debug.LogFormat("({0},{1})", location.row, location.col);
}
else
{
Debug.Log("DD");
}
}
}
}
TestBlock
TestMain에서 메서드를 불러오면 testMain에서 prefab 정보, parent transform 위치를 받아와 instantiate 와 위치 정보를 저장하는 메서드를 가지고있다.
그리고 Instantiate될 때 TestSaveLocation에 GetLocation메서드로 위치정보를 보내주면서 Instantiate될 때 row와 col 값을 저장해준다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestBlock : MonoBehaviour
{
private int nRow;
private int nCol;
private int m_nRow;
private int m_nCol;
private eBlockType[,] str;
public eBlockType[,] Str
{
get { return this.str; }
}
public enum eBlockType
{
WALL = 0,
RED = 1,
BLUE = 2,
GREEN = 3,
YELLOW = 4,
PURPLE = 5,
}
private eBlockType m_BlockType;
public eBlockType blockType
{
get { return this.m_BlockType; }
}
public TestBlock(int maxRow, int maxCol)
{
this.m_nRow = maxRow;
this.m_nCol = maxCol;
//배열 방 크기 만들기
str = new eBlockType[maxRow, maxCol];
}
public void InstantiateObj(GameObject objPrefab, Transform boardTr, eBlockType type)
{
GameObject obj = Instantiate(objPrefab, boardTr);
obj.transform.position = boardTr.position + new Vector3(nCol, nRow);
TestSaveLocation location = obj.GetComponent<TestSaveLocation>();
location.GetLocation(nRow, nCol);
SpriteRenderer objSp = obj.GetComponent<SpriteRenderer>();
this.m_BlockType = type;
objSp.sprite = Resources.Load<Sprite>($"Test/{type}");
str[nRow, nCol] = this.m_BlockType;
Debug.LogFormat("({0},{1}) = {2}", nRow, nCol, str[nRow, nCol]);
}
public void Move(int row, int col)
{
this.nRow = row;
this.nCol = col;
}
}
TestSaveLocation
TestBlock에서 Row와 Col값을 받아오고 내보내는 역할
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestSaveLocation : MonoBehaviour
{
public int row;
public int col;
//public int Row
//{
// get { return row; }
//}
//public int Col
//{
// get { return col; }
//}
//블럭 인스턴스할 때 가져와서 값 넣어줌
public TestSaveLocation()
{
}
public void GetLocation(int row, int col)
{
this.row = row;
this.col = col;
Debug.LogFormat("({0},{1})", row, col);
}
public void PutLocation()
{
Debug.LogFormat("({0},{1})", row, col);
}
}
'워리어 콤보 모작 개발일지' 카테고리의 다른 글
워리어콤보 개발일지 - Block 삭제하기 (0) | 2023.09.17 |
---|---|
워리어콤보 개발일지 - Block 인식하기 (0) | 2023.09.16 |
워리어콤보 개발일지 - Test [보드생성] (0) | 2023.09.15 |
워리어콤보 개발일지 - 2차원 배열 출력 (0) | 2023.09.14 |
워리어콤보 모작 개발일지 - 04 [StageInfo, StageReader][연습] (0) | 2023.09.14 |