유니티 기초

디자인패턴

다모아 2023. 8. 14. 11:54

PrefabManager는 다시 만들어보기


PrefabManager

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

namespace Test_Boss
{
    public class PrefabManager : MonoBehaviour
    {
        [SerializeField]
        private List<GameObject> prefabs;
        //private GameObject[] prefabs;

        public static PrefabManager Instance;
        
        private void Awake()
        {
            if(PrefabManager.Instance == null)
            {
                PrefabManager.Instance = this;
            }
            else
            {
                Destroy(this.gameObject);
            }
            //씬 전환시 씬에서 제거되지 않는다.
            DontDestroyOnLoad(this.gameObject);
        }

        public GameObject GetPrefabByName(string prefabName)
        {
            foreach(var name in this.prefabs)
            {
                Debug.Log(name);
            }
            return this.prefabs.Find(x => x.name.Replace("Prefab", "") == prefabName);
        }
    }
}

 

CommonMonster

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

namespace Test_Boss
{
    public class CommonMonster : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {

        }

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

        }
    }
}

 

CommonMonsterFactory

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

namespace Test_Boss
{
    public class CommonMonsterFactory : AbstractMonsterFactory
    {
        //PrefabManager가 싫으면 이 방식으로 사용
        //private GameObject prefab;
        //public void Init(GameObject prefab)
        //{
        //    this.prefab = prefab;
        //}
        public override void CreateBossMonster()
        {
        }

        public override CommonMonster CreateCommonMonster(GameEnums.eCommonMonsterType monsterType)
        {
            //string prefabName = string.
            GameObject prefab = PrefabManager.Instance.GetPrefabByName(monsterType.ToString() + "Prefab");
            Debug.LogFormat("prefab: {0}", prefab);
            GameObject go = UnityEngine.Object.Instantiate(prefab);
            return go.GetComponent<Slime>();
        }
    }
}

 

AbstractMonsterFactory

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

namespace Test_Boss
{
    public abstract class AbstractMonsterFactory
    {
        public abstract CommonMonster CreateCommonMonster(GameEnums.eCommonMonsterType monsterType);
        public abstract void CreateBossMonster();
    }
}

 

BossMonsterFactory

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

namespace Test_Boss
{
    public class BossMonsterFactory : AbstractMonsterFactory
    {
        public override void CreateBossMonster()
        {
        }

        public override CommonMonster CreateCommonMonster(GameEnums.eCommonMonsterType monsterType)
        {
            return null;
        }
    }
}

 

Test_BossMain

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

namespace Test_Boss
{
    public class Test_BossMain : MonoBehaviour
    {

        // Start is called before the first frame update
        void Start()
        {
            CommonMonsterFactory factory = new CommonMonsterFactory();
            factory.CreateCommonMonster(GameEnums.eCommonMonsterType.Slime);
            factory.CreateCommonMonster(GameEnums.eCommonMonsterType.Turtle);
        }

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

        }
    }
}

 

Bull

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

namespace Test_Boss
{
    public class Bull : BossMonster
    {

    }
}

 

Slime

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

namespace Test_Boss
{
    public class Slime : CommonMonster
    {
    }
}

 

Turtle

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

namespace Test_Boss
{
    public class Turtle : CommonMonster
    {
    }
}

 

GameEnums

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

namespace Test_Boss
{
    public class GameEnums
    {

        public enum eMonsterType
        {
            Common, Boss
        }

        public enum eCommonMonsterType
        {
            Turtle, Slime
        }

        public enum eBossMonsterType
        {
            Bull
        }
    }
}

 

BossMonster

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

namespace Test_Boss
{
    public class BossMonster : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {

        }

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

        }
    }
}