language:unity:코드조각:gameobjectpool
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판이전 판다음 판 | 이전 판 | ||
| language:unity:코드조각:gameobjectpool [2014/05/26 14:24] – kieuns | language:unity:코드조각:gameobjectpool [2024/04/23 22:45] (현재) – 바깥 편집 127.0.0.1 | ||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| + | 오브젝트 disable은 다른 부분에서 직접 호출해줘야 한다. | ||
| + | 음 .. 음.. 왜 만든거지. | ||
| + | |||
| + | <code csharp> | ||
| + | using UnityEngine; | ||
| + | using System.Collections; | ||
| + | using System.Collections.Generic; | ||
| + | |||
| + | public class GameObjectPool | ||
| + | { | ||
| + | protected Queue< | ||
| + | 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_, | ||
| + | { | ||
| + | mDefautQueue = new Queue< | ||
| + | |||
| + | mMaxNumber = maxNum_; | ||
| + | mTargetPrefab = targetObjPrefab_; | ||
| + | mParentObj = parentObj_; | ||
| + | |||
| + | for( int i = 0; i < mMaxNumber; ++i ) | ||
| + | { | ||
| + | GameObject _newObj = GameObject.Instantiate( targetObjPrefab_, | ||
| + | _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, | ||
| + | _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_ ); | ||
| + | } | ||
| + | } | ||
| + | </ | ||