2021년 4학년 1학기 기업연계프로젝트2
컴퓨터소프트웨어공학과 <원광투어팀>
팀장 : 송유진
팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
//========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
using System;using System.Collections.Generic;using UnityEngine;using UnityEngine.Events;
namespace HTC.UnityPlugin.Utility{ public class ObjectPool<T> { private readonly Stack<T> stack = new Stack<T>(); private readonly Func<T> actionOnNew; private readonly UnityAction<T> actionOnGet; private readonly UnityAction<T> actionOnRelease;
public int CountAll { get; private set; } public int CountInactive { get { return stack.Count; } } public int CountActive { get { return CountAll - CountInactive; } }
public ObjectPool(Func<T> onNew, UnityAction<T> onGet = null, UnityAction<T> onRelease = null) { actionOnNew = onNew; actionOnGet = onGet; actionOnRelease = onRelease; }
public T Get() { T element; if (stack.Count == 0) { element = actionOnNew == null ? default(T) : actionOnNew.Invoke(); CountAll++; } else { element = stack.Pop(); }
if (actionOnGet != null) { actionOnGet.Invoke(element); } return element; }
public void Release(T element) { if (stack.Count > 0 && ReferenceEquals(stack.Peek(), element)) { Debug.LogError("Internal error. Trying to destroy object that is already released to pool."); }
if (actionOnRelease != null) { actionOnRelease.Invoke(element); }
stack.Push(element);
if (stack.Count > CountAll) { CountAll = stack.Count; } } }}
|