개발일지/Puzzle Bubble
구슬 배치 색 랜덤 변경
101won
2024. 10. 13. 21:47
다음은 선택된 구슬의 좌표와 색을 불러와보자.
using System.Collections;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using TMPro;
public class Test : MonoBehaviour
{
int rows = 11; // 행 수
int colsOdd = 8; // 홀수 줄 열 수
int colsEven = 7; // 짝수 줄 열 수
List<Vector3> hexagonCenters; // 육각형 중심점 리스트
public GameObject B_Bubble;
private GameObject BbubbleGo;
float posX = 0f; //x좌표
float posY = 0f; //y좌표
float startX = -2.5f; // x축 시작 지점
float startY = -1.5f; // y축 시작 지점 (좌하단)
float hexRadius = 0.5f; // 육각형의 반지름
public Sprite[] bubbleSprites; // 구슬 스프라이트 어싸인
void Start()
{
hexagonCenters = new List<Vector3>(); // 리스트 초기화
ArrayBubble();
}
void ArrayBubble()
{
float xSpacing = hexRadius; // 열 간격 (육각형의 가로폭)
float ySpacing = 0.87f * hexRadius; // 행 간격 (육각형 긴 높이, 대략 0.87배)
for (int row = 0; row < rows; row++) // 11개 행 생성
{
int cols = (row % 2 == 0) ? colsOdd : colsEven; // 짝수 행이면 colsEven 7열 , 홀수 행이면 colsOdd 8열
for (int col = 0; col < cols; col++) // 위에서 받은 cols만큼 열 반복
{
// 좌표 계산
float xOffset = (row % 2 == 0) ? 0f : hexRadius / 2; // 홀수 줄은 약간 우측으로 밀기
posX = startX + col * xSpacing + xOffset; // x 좌표 계산
posY = startY + row * ySpacing; // y 좌표 계산 (아래에서 위로 올라가도록)
// 프리팹 생성 및 위치 설정
BbubbleGo = Instantiate(B_Bubble);
BbubbleGo.transform.position = new Vector3(posX, posY, 0);
int randomIndex = Random.Range(0, bubbleSprites.Length); // 구슬 스프라이트 어싸인 갯수 내에서 랜덤 인덱스 숫자 생성
BbubbleGo.GetComponent<SpriteRenderer>().sprite = bubbleSprites[randomIndex]; // 랜덤~ 숫자! 스프라이트 할당
// NNTxt 컴포넌트 추가
NNTxt nnTxt = BbubbleGo.AddComponent<NNTxt>(); // 버블 프리팹에 있는 자식 텍스트를 불러
TextMeshProUGUI textComponent = BbubbleGo.GetComponentInChildren<TextMeshProUGUI>();
nnTxt.SetIndex(row, col); // 구슬 행,열 인덱스
hexagonCenters.Add(new Vector3(posX, posY, 0)); // 중심점 좌표 리스트에 저장
}
}
}
//for (int i = 0; i < hexagonCenters.Count; i++)
//{
// Debug.Log($"Index {i}: {hexagonCenters[i]}"); // 저장한 중심점 좌표 리스트 출력
//}
}