유니티 심화

절대강좌 유니티 - 싱글톤, Invoke, CancelInvoke, DontDestroyonLoad

다모아 2023. 8. 28. 11:29

GameManager.p498

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public Transform[] points;
    //public List<Transform> points = new List<Transform>();

    private void Reset()
    {
        Debug.Log("Reset");
    }
    // Start is called before the first frame update
    void Start()
    {
        Transform spawnPointGroup = GameObject.Find("SpawnPointGroup")?.transform;

        Debug.LogFormat("spawnPointGroup: {0}", spawnPointGroup);

        this.points = spawnPointGroup?.GetComponentsInChildren<Transform>();

        Debug.LogFormat("points: {0}", points);
        
        foreach (Transform child in spawnPointGroup)
        {
            Debug.Log(child);
            points.Add(child);
        }
    }
}

 

비활성화된 오브젝트 찾기

    void Start()
    {
        Transform spawnPointGroup = GameObject.Find("SpawnPointGroup")?.transform;

        Debug.LogFormat("spawnPointGroup: {0}", spawnPointGroup);

        spawnPointGroup?.GetComponentsInChildren<Transform>(true, this.points);

        Debug.LogFormat("points.Count: {0}", this.points.Count);

        //비활성화된 오브젝트 찾기
        //foreach(Transform point in this.points)
        //{
        //    Debug.Log(point.gameObject.activeInHierarchy);
        //    if(!point.gameObject.activeInHierarchy)
        //    {
        //        Debug.Log(point.gameObject.name);
        //    }
        //}

        List<Transform> inactivePoints = this.points.Where(x => !x.gameObject.activeInHierarchy).ToList();

        int count = 0;
        inactivePoints.ForEach(x => {
            Debug.LogFormat("{0}: {1}", count++, x.gameObject.name);
        });
    }

 

 

GameManager 싱글톤 바꾸기

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    //public Transform[] points;
    [SerializeField]
    private GameObject monsterPrefab;

    //몬스터 생성 시간
    private float createTime = 3.0f;

    public List<Transform> points = new List<Transform>();

    //게임 오버
    private bool isGameOver = false;

    public bool IsGameOver
    {
        get
        {
            return this.isGameOver;
        }
        set
        {
            this.isGameOver = value;
            if(this.isGameOver)
            {
                CancelInvoke("CreateMonster");
            }
        }
    }

    //싱글턴 인스턴스 선언
    public static GameManager instance = null;

    private void Awake()
    {
        //instance가 할당되지 않았을 경우
        if(instance == null)
        {
            instance = this;
        }
        //instance에 할당된 클래스의 인스턴스가 다를 경우 새로 생성된 클래스를 의미함
        else if(instance != this)
        {
            Destroy(this.gameObject);
        }

        //다른 씬으로 넘어가도 삭제 X 유지
        DontDestroyOnLoad(this.gameObject);
    }
    private void Reset()
    {
        Debug.Log("Reset");
    }
    // Start is called before the first frame update
    void Start()
    {
        Transform spawnPointGroup = GameObject.Find("SpawnPointGroup")?.transform;

        Debug.LogFormat("spawnPointGroup: {0}", spawnPointGroup);

        spawnPointGroup?.GetComponentsInChildren<Transform>(true, this.points);

        Debug.LogFormat("points.Count: {0}", this.points.Count);

        foreach (Transform child in spawnPointGroup)
        {
            //Debug.Log(child);
            points.Add(child);
        }

        //2초 후에 createTime(3초)마다 호출, 시작 시에는 2초 후 호출
        InvokeRepeating("CreateMonster", 2.0f, this.createTime);
    }

    private void CreateMonster()
    {
        //생성할 위치/ 회전 정보 필요
        int index = Random.Range(0, this.points.Count);
        Transform point = this.points[index];
        //몬스터 프리팹의 인스턴스를 생성
        Instantiate(this.monsterPrefab, point.position, point.rotation);
        
    }
}