Summary.
기존의 Object Pooling을 사용하기위해 사용하던것을 Generic Class화 하여 봤습니다.
코드의 문제가 군데군데 보이지만 이후 조금씩 보완해 나가겠습니다.
Pool.cs
using UnityEngine; using System.Collections; using System.Collections.Generic; public class Pool<T> : MonoBehaviour where T : MonoBehaviour { public GameObject _PoolObject; public int _PoolLength; private int mPoolLength; private List<T> mPoolList; private int? mIndex = null; void Awake() { Initialize(_PoolLength); } private void Initialize(int length) { mPoolLength = length; mPoolList = new List<T>(); for (int i = 0; i < mPoolLength; ++i) { GameObject tmp = Instantiate(_PoolObject) as GameObject; tmp.transform.parent = transform; tmp.name = typeof(T).ToString() + i; mPoolList.Add(tmp.GetComponent<T>()); } } public T _GetObject { get { if (mIndex == null) mIndex = 0; else mIndex++; return mPoolList[(int)mIndex]; } } public int? _Index { get { return mIndex; } set { mIndex = value; } } public List<T> _PoolList { get { return mPoolList; } set { mPoolList = value; } } public int Length { get { return mPoolLength; } } }How to Use.
Enemy.cs
using UnityEngine; using System.Collections; public class Enemy : MonoBehaviour { public void Spawn(Vector2 pos) { Debug.Log("Spawn : " + name + "pos :" + pos); } public void Die() { Debug.Log("Die : " + name); } }EnemyPool.cs
using UnityEngine; using System.Collections; using System.Collections.Generic; public class EnemyPool : Pool<Enemy> { public void AllEnemySpawn() { foreach (Enemy ene in _PoolList) ene.Spawn(Vector2.zero); _Scope = 0; } public void AllEnemyDie() { foreach (Enemy ene in _PoolList) ene.Die(); _Scope = 0; } public void EnemySpwan(Vector2 pos) { _GetObject.Spawn(pos); } public void EnemySpawn() { _GetObject.Spawn(Vector2.zero); } public void EnemyDie(Enemy target) { _PoolList.Find( delegate(Enemy enemy) { if (enemy == target) { enemy.Die(); return true; } else return false; } ); } }fix-
v1.0 - 첫 업데이트. 기본적인 Object Pool 구조 작성.
v1.1 - value name modify
v1.2 - method name modify
0 개의 댓글:
댓글 쓰기