using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SimpleObjectPool : MonoBehaviour {
public GameObject Prefab = null;
public int MaxObjectCount = 1;
public bool IsCreatableMore = false;
private Queue Pool= new Queue();
void Awake()
{
MakePool();
}
public GameObject Pop()
{
if( Pool.Count == 0 )
{
if( IsCreatableMore )
{
Debug.log( "It will create one in the object pool" );
MakePool( Prefab, 1);
}
else
{
Debug.log( "No more objects available" );
return null;
}
}
return Pool.Dequeue();
}
public void Push(GameObject item)
{
item.gameObject.SetActive(false);
item.transform.SetParent( this.transform, true );
Pool.Enqueue( item );
}
void MakePool()
{
MakePool( Prefab, MaxObjectCount);
}
void MakePool( GameObject prefab, int count )
{
for( int i = 0; i < count; ++i )
{
GameObject go = Instantiate( prefab );
go.SetActive(false);
go.transform.SetParent(this.transform, true );
Pool.Enqueue( go );
}
}
}
'Unity3D' 카테고리의 다른 글
유니티3디의 JsonUtility 사용법 링크 (0) | 2020.04.21 |
---|---|
반복문을 가지는 코루틴 대기 하는 방법 - Coroutine Loop (0) | 2020.04.01 |
Curved World with Surface Shader (0) | 2020.02.28 |
Loading Scene Asynchronously for Unity (0) | 2020.02.26 |
Generic Singleton for Unity - 싱글턴 패턴 사용 하기 (0) | 2020.02.25 |