-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachePool.cs
More file actions
195 lines (167 loc) · 4.96 KB
/
Copy pathCachePool.cs
File metadata and controls
195 lines (167 loc) · 4.96 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using XLua;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SG
{
/// <summary>
/// 缓存接口,实现此接口的类可通过CachePool进行缓存管理。
/// </summary>
public interface ICacheable
{
/// <summary>
/// 被创建时调用。
/// </summary>
void OnCreate();
/// <summary>
/// 被使用时调用。
/// </summary>
void OnUse(object data = null);
/// <summary>
/// 被回收时调用。
/// </summary>
void OnRecycle();
/// <summary>
/// 若缓存池被销毁或已到上限时调用。
/// </summary>
void OnRelease();
}
/// <summary>
/// 缓存对象类,提供默认操作。
/// </summary>
[Hotfix]
public class CacheObject : ICacheable
{
public virtual void OnCreate() { }
public virtual void OnUse(object data = null) { }
public virtual void OnRecycle() { }
public virtual void OnRelease() { }
}
/// <summary>
/// CachePool。
/// </summary>
[Hotfix]
public class CachePool<T> where T : ICacheable, new()
{
/// <summary>
/// 创建缓存对象的函数委托。
/// </summary>
/// <returns>缓存对象类型。</returns>
public delegate T NewDelegate();
#region 对外操作----------------------------------------------------------------
/// <summary>
/// 构造函数。
/// </summary>
/// <param name="size">缓存大小。</param>
public CachePool(int size = 10, NewDelegate newfun = null)
{
m_Size = size;
m_NewFunction = newfun;
}
/// <summary>
/// 释放缓存。
/// </summary>
public void Clear()
{
while (m_CacheObjects.Count > 0)
{
T top = m_CacheObjects.Pop();
top.OnRelease();
}
}
/// <summary>
/// 获取一个对象。
/// </summary>
/// <param name="data">传递给OnUse的参数数据。</param>
/// <returns>缓存对象。</returns>
public T Get(object data = null)
{
if (m_CacheObjects.Count > 0)
{
T top = m_CacheObjects.Pop();
top.OnUse(data);
return top;
}
T newobj = m_NewFunction == null ? new T() : m_NewFunction();
if (newobj != null)
{
newobj.OnCreate();
newobj.OnUse(data);
}
return newobj;
}
public void Cache(T obj)
{
if (obj == null)
{
return;
}
if (m_CacheObjects.Contains(obj))
{
#if UNITY_EDITOR
LogMgr.LogError("重复回收");
#endif
return;
}
obj.OnRecycle();
if (m_CacheObjects.Count < m_Size)
{
m_CacheObjects.Push(obj);
}
else
{
obj.OnRelease();
#if UNITY_EDITOR
//LogMgr.LogWarning("The cache pool is full and the obj has be release. Size:{0}", m_Size);
#endif
}
}
#endregion
#region 对外属性----------------------------------------------------------------
/// <summary>
/// 当前缓存的数量。
/// </summary>
public int Count
{
get { return m_CacheObjects.Count; }
}
/// <summary>
/// 获取或设置缓存大小。
/// </summary>
public int Size
{
get { return m_Size; }
set
{
//移除超过缓存大小的对象。
m_Size = Mathf.Max(0, value);
while (m_CacheObjects.Count > m_Size)
{
T top = m_CacheObjects.Pop();
top.OnRelease();
#if UNITY_EDITOR
LogMgr.LogWarning("The new size is too small. Need release a object. Size:{0} Count:{1}", m_Size, m_CacheObjects.Count + 1);
#endif
}
}
}
#endregion
#region 内部操作----------------------------------------------------------------
#endregion
#region 内部数据----------------------------------------------------------------
/// <summary>
/// 创建对象的函数,若该函数为null则直接new。
/// </summary>
private NewDelegate m_NewFunction = null;
/// <summary>
/// 缓存堆栈。
/// </summary>
private Stack<T> m_CacheObjects = new Stack<T>();
/// <summary>
/// 缓存上限,超过上限的将不会再缓存,并调用缓存对象的OnRelease。
/// </summary>
private int m_Size;
#endregion
}
}