[TIL] Unity - Sprite - Day 54

뭉크의 개발·2023년 9월 27일
0

Unity - Camp

목록 보기
24/70
post-thumbnail

🐧 들어가기 앞서

타워를 스프라이트로 하느냐, 이미지로 하느냐

각자의 장점이 있지만,

스프라이트로 하기로 결정하고

이미지로 했던 모든 작업을 날리고 다시 만들었다.


🐧 오늘 배운 것

제네릭


🐧 기억할 것 & 진행

타워 스폰매니저를 만들었다.

using System.Collections.Generic;
using UnityEngine;

public class TowerSpawnManager : MonoBehaviour
{
    public static TowerSpawnManager instance;
    public GameObject towerPrefab;
    public GameObject spawnPointPrefab;
    private List<Transform> spawnPoints = new List<Transform>();
    public float startX = 0f;
    public float startY = 0f;

    void Start()
    {
        GenerateSpawnPoints(5, 3);
    }
    private void Awake()
    {
        instance = this;
    }

    public void SpawnRandomTower()
    {
        List<Transform> emptySpawnPoints = new List<Transform>();
        foreach (Transform spawnPoint in spawnPoints)
        {
            if (spawnPoint.childCount == 0)
            {
                emptySpawnPoints.Add(spawnPoint);
            }
        }
        if (emptySpawnPoints.Count == 0) return;

        Transform randomEmptySpawnPoint = emptySpawnPoints[Random.Range(0, emptySpawnPoints.Count)];

        Instantiate(towerPrefab, randomEmptySpawnPoint.position, Quaternion.identity, randomEmptySpawnPoint);
    }

    private void GenerateSpawnPoints(int width, int height)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Vector3 position = new Vector3(startX + x * 1.5f, startY + y * 1.5f, 0);
                GameObject spawnPointInstance = Instantiate(spawnPointPrefab, position, Quaternion.identity);
                spawnPoints.Add(spawnPointInstance.transform);
            }
        }
    }

}

🐧 게임에 구현한다면?

스폰 위치와 타워, 도트 모두 프리팹으로 구현해서 코드로 관리했다.

이미지를 활성화하고 비활성화하는데 굉장히 작업이 번거로웠고

슬롯 자체를 만들다보니 조금 활성화 하는 부분이 까다로웠다.

스폰 포인트를 만들고, 해당 위치에 프리팹을 생성하도록 만들었다.

좋은 점은 원하는 위치에 스폰을 할 수 있게 관리가 가능하다는 점!


🐧 내일 할 일

머지 기능 만들기

0개의 댓글