This repository was archived by the owner on Jun 24, 2025. It is now read-only.
forked from krockot/Unity-TaskManager
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectPool.cs
More file actions
44 lines (40 loc) · 1.26 KB
/
ObjectPool.cs
File metadata and controls
44 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine;
namespace TexDrawLib
{
public class ObjectPool<T> where T : new ()
{
private readonly Stack<T> m_Stack = new Stack<T>();
#if UNITY_EDITOR
public int countAll { get; private set; }
public int countActive { get { return countAll - countInactive; } }
public int countInactive { get { return m_Stack.Count; } }
#endif
public T Get()
{
T element;
if (m_Stack.Count == 0)
{
element = new T();
#if UNITY_EDITOR
countAll++;
// Debug.LogFormat( "Pop New {0}, Total {1}", typeof(T).Name, countAll);
#endif
}
else
{
element = m_Stack.Pop();
}
return element;
}
public void Release(T element)
{
#if UNITY_EDITOR
if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
#endif
m_Stack.Push(element);
}
}
}