유니티 기초

몬스터 동적으로 데이터관리 생성

다모아 2023. 8. 10. 10:43
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Test3
{
    //씬의 모든 객체들을 관리
    public class Test_CreatePortalMain : MonoBehaviour
    {
        [SerializeField]
        private MonsterGenerator monsterGenerator;
        // Start is called before the first frame update
        void Start()
        {
            this.monsterGenerator.Generate(GameEnums.eMonsterType.Turtle, new Vector3(-3, 0, 0));
            this.monsterGenerator.Generate(GameEnums.eMonsterType.Slime, new Vector3(0, 0, 3));
        }

        // Update is called once per frame
        void Update()
        {

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

namespace Test3
{
    public class MonsterGenerator : MonoBehaviour
    {
        //[SerializeField] private GameObject turtlePrefab;
        //[SerializeField] private GameObject slimePrefab;
        [SerializeField]
        private List<GameObject> prefabList; //동적배열 (컬렉션 사용전 반드시 인스턴스화)

        // Start is called before the first frame update
        void Start()
        {
            //int index = 0;
            //foreach(GameObject prefab in this.prefabList)
            //{
            //    Debug.LogFormat("index: {0}, prefab: {1}",index++, prefab);
            //}

            for(int i = 0; i < this.prefabList.Count; i++)
            {
                GameObject prefab = this.prefabList[i];
                Debug.LogFormat("index: {0}, prefab: {1}", i, prefab);
            }
        }

        /// <summary>
        /// 몬스터 생성
        /// </summary>
        /// <param name="monsterType">생성하려고하는 몬스터의 타입</param>
        /// <param name="initPosition">생성된 몬스터의 World Position 초기월드좌표</param>
        public void Generate(GameEnums.eMonsterType monsterType, Vector3 initPosition)
        {
            Debug.LogFormat("monsterType: {0}", monsterType);
            //몬스터 타입에 따라 어떤 프리팹으로 프리팹 복사본(인스턴스)를 생성할지 결정해야함
            //몬스터 타입을 인덱스로 변경
            int index = (int)monsterType;
            
            Debug.LogFormat("index: {0}", index);
            //프리팹 배열에서 인덱스로 프리팹 가져옴
            GameObject prefab = this.prefabList[index];

            Debug.LogFormat("prefab: {0}", index);
            //프리팹 인스턴스를 생성
            GameObject go = Instantiate(prefab); //위치를 결정하지 않은 상태이기 때문 (프리팹의 설정된 위치에 생성됨)
            //위치를 설정
            go.transform.position = initPosition;
            //if(monsterType == GameEnums.eMonsterType.Turtle)
            //{
            //    Instantiate(this.turtlePrefab) ;
            //}
            //else if(monsterType == GameEnums.eMonsterType.Slime)
            //{
            //    Instantiate(this.slimePrefab);
            //}

            //switch (monsterType)
            //{
            //    case GameEnums.eMonsterType.Turtle:
            //        Instantiate(this.turtlePrefab);
            //        break;

            //    case GameEnums.eMonsterType.Slime:
            //        Instantiate(this.slimePrefab);
            //        break;
            //}


        }
    }
}