현재 Map생성시 Floor과 Rail 두 가지의 GameObject가 생성되고 있다.
그러나 Map은 이 외에도 게임환경을 구성해줄 건물, 나무 등의 설치물들이 생성되어야 한다.
현재 Rail은 Random하게 생성되고 있으므로 Rail의 위치에 맞게 다른 GameObject들이 생성되도록 구현해야 한다.
우선 소규모의 Structure를 생성해보도록 하쟈.
먼저 Structure를 생산 및 관리해줄 StructurePoolManager 스크립트 및 Structure의 정보가 저장된 Structure 스크립트를 생성하였다.
Structure.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Structure : MonoBehaviour
{
public enum eType
{
Tree, Bench, Lamp, Clock, Sign, AdPole, ElecPole, FireHydrant
}
public eType type;
}
StructurePoolManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StructurePoolManager : MonoBehaviour
{
public static StructurePoolManager Instance;
[SerializeField] private GameObject[] arrStructurePrefabs;
private List<List<GameObject>> listStructures = new List<List<GameObject>>();
public int count = 10;
private void Awake()
{
if(Instance == null)
Instance = this;
}
void Start()
{
this.InitStructure();
}
private void InitStructure()
{
listStructures.Add(new List<GameObject>());
for(int i=0;i<arrStructurePrefabs.Length;i++)
{
for(int j = 0; j < this.count; j++)
{
GameObject go = this.GenerateStructure(i);
go.transform.SetParent(this.transform);
go.SetActive(false);
}
}
}
private GameObject GenerateStructure(int idx)
{
GameObject go = Instantiate(this.arrStructurePrefabs[idx]);
listStructures[idx].Add(go);
return go;
}
public GameObject EnableStructure(int idx)
{
GameObject structure = null;
for(int i = 0; i < this.listStructures[idx].Count;i++)
{
GameObject go = this.listStructures[idx][i];
if (go.activeSelf == false)
{
go.SetActive(true);
go.transform.SetParent(null);
structure = go;
break;
}
}
if (structure == null)
{
structure = this.GenerateStructure(idx);
}
return structure;
}
public void DisableStructure(GameObject go)
{
go.transform.SetParent(this.transform);
go.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
go.SetActive(false);
}
}
Structure.cs에는 Structure의 형태 정보를 나타내줄 eType이 enum 형식으로 구현되어 있다.
미리 구현해놓은 Prefab들에 Structure.cs를 컴포넌트로 부착하고 type을 지정해주었다.
StructurePoolManager의 Field에는 Prefab들을 저장하는 arrStructurePrefabs와 Structure들을 type에 따라 관리할 수 있도록 2차원 배열로 구현된 listStructures가 선언되어있다.
시작 시 structure gameObject들을 생성하는 method를 구현하였다.
미리 준비해놓은 structure 종류 별로 count(10개)만큼 생성되도록 한 뒤 manager가 관리하도록 setparent하고 아직 사용되지 않은 상태이므로 SetActive를 false로 지정하였다.(비활성화)
structure GameObject를 생성하는 method.
idx는 배열에서 원하는 prefab 및 list에 접근하기 위해 사용되었다.
list에 생성한 GameObject를 저장하고 GameObject는 결과값으로 반환된다.
필요한 GameObject를 사용 및 반환하는 method이다.
Enable은 비활성화되어있는(사용중이 아닌) GameObject를 찾아서 사용하도록 구현된 method이다.
만약 list 내의 모든 GameObject들이 활성화되어있다면 GameObject를 더 생성하여 반환되도록 구현하였다.
Disable은 사용되지 않는 GameObject를 비활성화시키는 method이다.
이렇게 적고 보니 기존에 구현해놓은 스크립트들을 수정해야 할듯 싶다,,,,,ㅠ
'K-digital traning > Final Project' 카테고리의 다른 글
Gazzlers 개발일지 - Enemy 이동(Player 추적) (0) | 2023.11.30 |
---|---|
Gazzlers 개발일지 - Map생성(5) (0) | 2023.11.28 |
Gazzlers 개발일지 - Map생성(3) (0) | 2023.11.22 |
Gazzlers 개발일지 - Map 생성(2) (1) | 2023.11.21 |
Gazzlers 개발일지 - Map 생성 (0) | 2023.11.09 |