[Day 15] Color Fill Tutorial 4
오블완 챌린지 15일차

주말에 본가 가야돼서
분주하게 준비하고 있는
GamJia 입니다
어제 강의에 이어서
Color Fill을 만들어 보겠습니다
일단 Complete 표시를 위해
Text를 하나 만들어주겠습니다
이거는 스테이지를 클리어 했을 때
나와야 하니까 비활성화 해둘게요
public void UpdateGridPrefabs()
{
maxCount=0;
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
for (int y = 0; y < gridSize; y++)
{
for (int x = 0; x < gridSize; x++)
{
GameObject prefab = grid[x, y] ? enablePrefab : disablePrefab;
GameObject tile = Instantiate(prefab, transform);
if (grid[x, y])
{
maxCount++;
}
float positionX = x * gridScale;
float positionY = y * -gridScale;
tile.transform.localPosition = new Vector2(positionX, positionY);
}
}
Debug.Log(maxCount);
float gridW = gridSize * gridScale;
float gridH = gridSize * gridScale;
transform.position = new Vector2(-gridW / 2 + gridScale / 2, gridH / 2 - gridScale / 2);
}
ColorFillManager에 maxCount(전체 enable 개수),
count(현재 칠한 enable 개수)라는 이름의 int형
변수를 추가해주고 enable을 생성할 때
maxCount를 1씩 증가 시키고 마지막에
maxCount를 출력 시켜보았습니다
count가 잘 증가한걸 확인했습니다
지금 보면 48번칸만 색이 다른데
그거는 지금 진행할 내용입니다..ㅎㅎ
public class ColorFillManager : MonoBehaviour
{
public GameObject enablePrefab; // true일 때 인스턴스화할 Prefab
public GameObject disablePrefab; // false일 때 인스턴스화할 Prefab
public GameObject completeText;
public bool[,] grid; // 그리드 배열
public int gridSize = 5; // 그리드 크기
public float gridScale = 1.2f;
private int maxCount=0;
private int count=0;
public static ColorFillManager Instance => instance;
private static ColorFillManager instance;
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
// 그리드 크기 확인 및 초기화
if (grid == null || grid.GetLength(0) != gridSize || grid.GetLength(1) != gridSize)
{
grid = new bool[gridSize, gridSize];
}
LoadGridData(); // 데이터를 로드
}
...
public void UpdateCount()
{
count++;
if(count==maxCount)
{
completeText.SetActive(true);
}
}
}
일단 아까 만들어준 text 객체를 연결해주고
Enable의 trigger Collider에 닿은게 확인되면
count를 증가시켜 모든 객체가 칠해졌을 때
text를 active true로 전환해주겠습니다
Enable 객체로 돌아가서
Color Change라는 component 하나 만들어줄게요
위에 Target Color는 trigger collider에 닿았을 때
바뀌는 색인데 원하는거 아무거나 넣으세요ㅎㅎ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ColorFill;
namespace ColorFill
{
public class ColorChange : MonoBehaviour
{
[SerializeField] private Color targetColor; // 변경할 색상
private Collider2D collider;
void Awake()
{
collider = GetComponent<Collider2D>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
Player player = collision.GetComponent<Player>();
if (player != null)
{
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer != null)
{
// 색상 변경
spriteRenderer.color = targetColor;
}
collider.enabled = false;
ColorFillManager.Instance.UpdateCount();
}
}
}
}
Collider를 불러와주고 Trigger Collider에
player 객체가 닿은게 확인되면
방금 정한 색깔로 변경해주고
방금 만든 count 증가 기능을 연결해주겠습니다
추가적인 충돌을 방지하기 위해
collider의 enabled를 false처리 해주었습니다
저장 후 실행해보니 enabled color가
다 바뀌었을 때 "Complete"
Text가 뜨는걸 확인했습니다!
오늘은 여기까지 하고
GitHub에 Commit하는걸로
마무리 하겠습니다!
게임 기능은 끝났고..
내일은 아마 캐릭터에 좀 더
빨리 가는 듯한 효과를 줄 것 같습니다!
감사합니다!!