Skip to content

Commit 87023fe

Browse files
committed
改善对象池管理器,可管理类对象+资源游戏对象
1 parent 007535b commit 87023fe

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

Assets/LuaFramework/Scripts/Manager/GameManager.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,14 @@ public void OnResourceInited() {
247247
initialize = true; //初始化完
248248

249249
//类对象池测试
250-
var classObjPool = ObjPoolManager.CreatePool<TestObjectClass>("TestObjectClass", null, null);
250+
var classObjPool = ObjPoolManager.CreatePool<TestObjectClass>(OnPoolGetElement, OnPoolPushElement);
251251
//方法1
252252
//objPool.Release(new TestObjectClass("abcd", 100, 200f));
253253
//var testObj1 = objPool.Get();
254254

255255
//方法2
256-
ObjPoolManager.PushObject<TestObjectClass>("TestObjectClass", new TestObjectClass("abcd", 100, 200f));
257-
var testObj1 = ObjPoolManager.GetObject<TestObjectClass>("TestObjectClass");
256+
ObjPoolManager.Release<TestObjectClass>(new TestObjectClass("abcd", 100, 200f));
257+
var testObj1 = ObjPoolManager.Get<TestObjectClass>();
258258

259259
Debugger.Log("TestObjectClass--->>>" + testObj1.ToString());
260260

@@ -267,13 +267,29 @@ public void OnResourceInited() {
267267
gameObj.transform.localScale = Vector3.one;
268268
gameObj.transform.localPosition = Vector3.zero;
269269

270-
ObjPoolManager.PushObject("TestGameObject", gameObj);
271-
var backObj = ObjPoolManager.GetObject("TestGameObject");
270+
ObjPoolManager.Release("TestGameObject", gameObj);
271+
var backObj = ObjPoolManager.Get("TestGameObject");
272272
backObj.transform.SetParent(null);
273273

274274
Debug.Log("TestGameObject--->>>" + backObj);
275275
}
276276

277+
/// <summary>
278+
/// 当从池子里面获取时
279+
/// </summary>
280+
/// <param name="obj"></param>
281+
void OnPoolGetElement(TestObjectClass obj) {
282+
Debug.Log("OnPoolGetElement--->>>" + obj);
283+
}
284+
285+
/// <summary>
286+
/// 当放回池子里面时
287+
/// </summary>
288+
/// <param name="obj"></param>
289+
void OnPoolPushElement(TestObjectClass obj) {
290+
Debug.Log("OnPoolPushElement--->>>" + obj);
291+
}
292+
277293
/// <summary>
278294
/// 析构函数
279295
/// </summary>

Assets/LuaFramework/Scripts/Manager/ObjectPoolManager.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public GameObjectPool GetPool(string poolName) {
3838
return null;
3939
}
4040

41-
public GameObject GetObject(string poolName) {
41+
public GameObject Get(string poolName) {
4242
GameObject result = null;
4343
if (m_GameObjectPools.ContainsKey(poolName)) {
4444
GameObjectPool pool = m_GameObjectPools[poolName];
@@ -52,7 +52,7 @@ public GameObject GetObject(string poolName) {
5252
return result;
5353
}
5454

55-
public void PushObject(string poolName, GameObject go) {
55+
public void Release(string poolName, GameObject go) {
5656
if (m_GameObjectPools.ContainsKey(poolName)) {
5757
GameObjectPool pool = m_GameObjectPools[poolName];
5858
pool.ReturnObjectToPool(poolName, go);
@@ -63,30 +63,32 @@ public void PushObject(string poolName, GameObject go) {
6363

6464
///-----------------------------------------------------------------------------------------------
6565

66-
public ObjectPool<T> CreatePool<T>(string poolName, UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease) where T : class {
66+
public ObjectPool<T> CreatePool<T>(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease) where T : class {
67+
var type = typeof(T);
6768
var pool = new ObjectPool<T>(actionOnGet, actionOnRelease);
68-
m_ObjectPools[poolName] = pool;
69+
m_ObjectPools[type.Name] = pool;
6970
return pool;
7071
}
7172

72-
public ObjectPool<T> GetPool<T>(string poolName) where T : class {
73+
public ObjectPool<T> GetPool<T>() where T : class {
74+
var type = typeof(T);
7375
ObjectPool<T> pool = null;
74-
if (m_ObjectPools.ContainsKey(poolName)) {
75-
pool = m_ObjectPools[poolName] as ObjectPool<T>;
76+
if (m_ObjectPools.ContainsKey(type.Name)) {
77+
pool = m_ObjectPools[type.Name] as ObjectPool<T>;
7678
}
7779
return pool;
7880
}
7981

80-
public T GetObject<T>(string poolName) where T : class {
81-
var pool = GetPool<T>(poolName);
82+
public T Get<T>() where T : class {
83+
var pool = GetPool<T>();
8284
if (pool != null) {
8385
return pool.Get();
8486
}
8587
return default(T);
8688
}
8789

88-
public void PushObject<T>(string poolName, T obj) where T : class {
89-
var pool = GetPool<T>(poolName);
90+
public void Release<T>(T obj) where T : class {
91+
var pool = GetPool<T>();
9092
if (pool != null) {
9193
pool.Release(obj);
9294
}
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
using UnityEngine;
22
using System.Collections;
33

4-
public class TestObjectClass {
4+
namespace LuaFramework {
55

6-
public string name;
7-
public int value1;
8-
public float value2;
6+
public class TestObjectClass {
97

10-
// Use this for initialization
11-
public TestObjectClass(string name, int value1, float value2) {
12-
this.name = name;
13-
this.value1 = value1;
14-
this.value2 = value2;
15-
}
8+
public string name;
9+
public int value1;
10+
public float value2;
1611

17-
public string ToString() {
18-
return string.Format("name={0} value1={1} = value2={2}", name, value1, value2);
12+
// Use this for initialization
13+
public TestObjectClass(string name, int value1, float value2) {
14+
this.name = name;
15+
this.value1 = value1;
16+
this.value2 = value2;
17+
}
18+
19+
public string ToString() {
20+
return string.Format("name={0} value1={1} = value2={2}", name, value1, value2);
21+
}
1922
}
20-
}
23+
}

0 commit comments

Comments
 (0)