Unity/Unity 리서치

[RTS Engine] Object Pooling 이 제대로 안될 때

lipnus 2022. 3. 30. 20:53
반응형

Effect의 Pooling을 담당하는 부분

 

EffectObjPool.cs

        private Dictionary<string, Queue<EffectObj>> effectObjs = new Dictionary<string, Queue<EffectObj>>(); //this holds all inactive effect objects of different types.

		// ...

        public void AddFreeEffectObj (EffectObj instance)
        {
            Queue<EffectObj> currentQueue = new Queue<EffectObj>();
            if (effectObjs.TryGetValue(instance.GetCode(), out currentQueue) == false) //if the queue for this effect object type is not found
            {
                effectObjs.Add(instance.GetCode(), currentQueue); //add it
            }

            Debug.Log($"AddFreeEffectObj: {instance.GetCode()}");
            currentQueue.Enqueue(instance); //add the effect object to the right queue

        }

여기 Queue에 들어가야 재활용 될 수 있음

 

EffecObj.cs

// ...
        public void Disable()
        {
            if (CurrentState == State.disabling) //if the effect object is already being disabled
                return;

            onDisableEvent.Invoke(); //invoke the event.

            timer = disableTime; //start the disable timer
            CurrentState = State.disabling; //we're now disabling the effect object
        }

        private void DisableInternal ()
        {
            transform.SetParent(null, true); //Make sure it has no parent object anymore.
            CurrentState = State.inactive;
            gameObject.SetActive(false);
            gameMgr.EffectPool.AddFreeEffectObj(this);

            EffectObjDisabled(this); //trigger custom event
        }

Destroy되면 제발로 Queue에 들어간다. 구분은 Code의 string값으로 함

 

 

이런식으로 OnDestroyEvent에서 SetActive(false)해버리면,

Queue에 들어가기 전에 InActive되어서 Queue에 못들어가서 재활용 불가.

 

EffectObj.cs코드보면 저 이벤트 발생이 Queue에 들어가는 것보다 선행됨.

반응형

'Unity > Unity 리서치' 카테고리의 다른 글

Collider2D, Collider 상속 관계  (0) 2023.01.23
유니티 강좌 모음  (0) 2023.01.03
Unity 웹뷰(WebView) - 흰 화면만 뜨는 경우  (0) 2022.03.18
[Addressable] Build Mode Script  (0) 2022.02.27
[Addressable] Update 과정  (0) 2022.02.26