language:unity:코드조각:gameobjectpool
오브젝트 disable은 다른 부분에서 직접 호출해줘야 한다.
음 .. 음.. 왜 만든거지.
using UnityEngine; using System.Collections; using System.Collections.Generic; public class GameObjectPool { protected Queue<GameObject> mDefautQueue; protected int mRefillTiming = 5; protected int mCntIndex = 0; protected int mMaxNumber = 20; protected GameObject mTargetPrefab; protected GameObject mParentObj; public void initQueue( GameObject parentObj_, GameObject targetObjPrefab_, int maxNum_ ) { mDefautQueue = new Queue<GameObject>(); mMaxNumber = maxNum_; mTargetPrefab = targetObjPrefab_; mParentObj = parentObj_; for( int i = 0; i < mMaxNumber; ++i ) { GameObject _newObj = GameObject.Instantiate( targetObjPrefab_, Vector3.one, Quaternion.identity ) as GameObject; _newObj.transform.parent = parentObj_.transform; _newObj.SetActive( false ); mDefautQueue.Enqueue( _newObj ); } } public void checkAvailObjectAndAutoRefill() { if( mDefautQueue.Count < mRefillTiming ) { for( int i = 0; i < (mMaxNumber / 2); ++i ) { GameObject _newObj = GameObject.Instantiate( mTargetPrefab, Vector3.one, Quaternion.identity ) as GameObject; _newObj.transform.parent = mParentObj.transform; _newObj.SetActive( false ); mDefautQueue.Enqueue( _newObj ); } } } public GameObject getObject( Vector3 initPos_, Quaternion quat_, bool reInsert_ = false ) { GameObject _gameObj = null; checkAvailObjectAndAutoRefill(); { _gameObj = mDefautQueue.Dequeue(); _gameObj.transform.position = initPos_; _gameObj.transform.localRotation = quat_; _gameObj.SetActive( true ); if( reInsert_ ) { mDefautQueue.Enqueue( _gameObj ); } } return _gameObj; } public void deactiveObject( GameObject gameObj_ ) { gameObj_.SetActive( false ); mDefautQueue.Enqueue( gameObj_ ); } public void deactiveObject( GameObject gameObj_, string deactiveFuncName ) { gameObj_.SetActive( false ); gameObj_.SendMessage( deactiveFuncName ); mDefautQueue.Enqueue( gameObj_ ); } }
language/unity/코드조각/gameobjectpool.txt · 마지막으로 수정됨: 2024/04/23 22:45 저자 127.0.0.1