/* Unity - Introduction to Object Pooling */
public static ObjectPool SharedInstance;
public List pooledObjects;
public GameObject objectToPool;
public int amountToPool;
void Awake()
{
SharedInstance = this;
}
void Start()
{
pooledObjects = new List(); // pool 생성
GameObject tmp;
for(int i = 0; i < amountToPool; i++) // amountToPool만큼 object 생성
{
tmp = Instantiate(objectToPool);
tmp.SetActive(false);
pooledObjects.Add(tmp);
}
}
public GameObject GetPooledObject() // object를 pool에서 꺼내오는 함수
{
for(int i = 0; i < amountToPool; i++) // List 앞부분부터 차례대로 검색
{
if(!pooledObjects[i].activeInHierarchy) // 맨 앞에 있는 오브젝트부터 반환
{
return pooledObjects[i];
}
}
return null;
}
/* 설명에선 pool에 다시 넣을 땐 SetActive(false); 를 통해 deactive 한다고 쓰여있음 */
/* List를 검색해서 꺼내오는 방식보단 Stack을 이용한 구현방식이 더 효율적일 듯? */
ObjectPool objectPool = new ObjectPool(CreateObject,
OnGetFromPool, OnReleaseToPool, OnDestroyPooledObject,
collectionCheck, defaultCapacity, maxSize);
// 오브젝트 생성, 초기화, 활성화, 제거, 예외처리, 기본크기, 최대크기 지정
/* GameManager를 활용한 배터리 및 폭죽 UI 구현 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
/* 배터리 부분 변수 */
public static bool onoff; // true : 켜짐 / false : 꺼짐
public GameObject Target; // 손전등 불빛 오브젝트
public static bool click; // ?
private Image battery;
public Sprite[] sprites;
public int MaxTime = 120;
public static float time = 0;
/* 폭죽 UI 변수 */
int fireworkCount;
public GameObject[] Fireworks = new GameObject[3];
void Start()
{
SetBattery();
}
// Update is called once per frame
void Update()
{
UpdateBattery();
UpdateFirework();
}
void SetBattery()
{
time = MaxTime;
battery = GetComponent();
onoff = false;
Target.SetActive(false);
}
void UpdateBattery()
{
/* 배터리 비율에 따른 UI 변경 */
if (time >= MaxTime * 0.8)
battery.sprite = sprites[0];
else if (time >= MaxTime * 0.6)
battery.sprite = sprites[1];
else if (time >= MaxTime * 0.4)
battery.sprite = sprites[2];
else if (time >= MaxTime * 0.2)
battery.sprite = sprites[3];
else if (time > 0)
battery.sprite = sprites[4];
else
battery.sprite = sprites[5];
/* 손전등 배터리 소모 */
if (onoff && time > 0)
time -= Time.deltaTime;
else if (time < 0) // 배터리 없으면 꺼지도록
{
time = 0;
}
if (Input.GetKeyDown(KeyCode.F) || click) // 손전등 on/off 변경 확인
{
onoff = !onoff;
Target.SetActive(onoff);
click = false;
}
}
void UpdateFirework()
{
fireworkCount = Player.firework_Count;
if (fireworkCount > 0)
{
Fireworks[0].gameObject.SetActive(true);
Fireworks[1].gameObject.SetActive(false);
if (fireworkCount > 1)
{
Fireworks[1].gameObject.SetActive(true);
Fireworks[2].gameObject.SetActive(false);
}
if (fireworkCount > 2)
Fireworks[2].gameObject.SetActive(true);
}
else
{
Fireworks[0].gameObject.SetActive(false);
Fireworks[1].gameObject.SetActive(false);
Fireworks[2].gameObject.SetActive(false);
}
}
}