Skip to content

Commit 007535b

Browse files
committed
添加对象池管理器,可管理类对象+资源游戏对象
1 parent 24e7004 commit 007535b

File tree

17 files changed

+366
-0
lines changed

17 files changed

+366
-0
lines changed

Assets/LuaFramework/Resources.meta

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!1 &180150
4+
GameObject:
5+
m_ObjectHideFlags: 0
6+
m_PrefabParentObject: {fileID: 0}
7+
m_PrefabInternal: {fileID: 100100000}
8+
serializedVersion: 4
9+
m_Component:
10+
- 4: {fileID: 433712}
11+
m_Layer: 0
12+
m_Name: TestGameObjectPrefab
13+
m_TagString: Untagged
14+
m_Icon: {fileID: 0}
15+
m_NavMeshLayer: 0
16+
m_StaticEditorFlags: 0
17+
m_IsActive: 1
18+
--- !u!4 &433712
19+
Transform:
20+
m_ObjectHideFlags: 1
21+
m_PrefabParentObject: {fileID: 0}
22+
m_PrefabInternal: {fileID: 100100000}
23+
m_GameObject: {fileID: 180150}
24+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
25+
m_LocalPosition: {x: 0, y: 0, z: 0}
26+
m_LocalScale: {x: 1, y: 1, z: 1}
27+
m_Children: []
28+
m_Father: {fileID: 0}
29+
m_RootOrder: 0
30+
--- !u!1001 &100100000
31+
Prefab:
32+
m_ObjectHideFlags: 1
33+
serializedVersion: 2
34+
m_Modification:
35+
m_TransformParent: {fileID: 0}
36+
m_Modifications: []
37+
m_RemovedComponents: []
38+
m_ParentPrefab: {fileID: 0}
39+
m_RootGameObject: {fileID: 180150}
40+
m_IsPrefabParent: 1

Assets/LuaFramework/Resources/TestGameObjectPrefab.prefab.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/LuaFramework/Scripts/ConstDefine/ManagerName.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public class ManagerName {
1111
public const string Network = "NetworkManager";
1212
public const string Resource = "ResourceManager";
1313
public const string Thread = "ThreadManager";
14+
public const string ObjectPool = "ObjectPoolManager";
1415
}
1516
}

Assets/LuaFramework/Scripts/Controller/Command/StartUpCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public override void Execute(IMessage message) {
2323
AppFacade.Instance.AddManager<NetworkManager>(ManagerName.Network);
2424
AppFacade.Instance.AddManager<ResourceManager>(ManagerName.Resource);
2525
AppFacade.Instance.AddManager<ThreadManager>(ManagerName.Thread);
26+
AppFacade.Instance.AddManager<ObjectPoolManager>(ManagerName.ObjectPool);
2627
AppFacade.Instance.AddManager<GameManager>(ManagerName.Game);
2728
}
2829
}

Assets/LuaFramework/Scripts/Framework/Core/Base.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Base : MonoBehaviour {
1111
private SoundManager m_SoundMgr;
1212
private TimerManager m_TimerMgr;
1313
private ThreadManager m_ThreadMgr;
14+
private ObjectPoolManager m_ObjectPoolMgr;
1415

1516
/// <summary>
1617
/// 注册消息
@@ -94,4 +95,13 @@ protected ThreadManager ThreadManager {
9495
return m_ThreadMgr;
9596
}
9697
}
98+
99+
protected ObjectPoolManager ObjPoolManager {
100+
get {
101+
if (m_ObjectPoolMgr == null) {
102+
m_ObjectPoolMgr = facade.GetManager<ObjectPoolManager>(ManagerName.ObjectPool);
103+
}
104+
return m_ObjectPoolMgr;
105+
}
106+
}
97107
}

Assets/LuaFramework/Scripts/Manager/GameManager.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,33 @@ public void OnResourceInited() {
245245

246246
Util.CallMethod("Game", "OnInitOK"); //初始化完成
247247
initialize = true; //初始化完
248+
249+
//类对象池测试
250+
var classObjPool = ObjPoolManager.CreatePool<TestObjectClass>("TestObjectClass", null, null);
251+
//方法1
252+
//objPool.Release(new TestObjectClass("abcd", 100, 200f));
253+
//var testObj1 = objPool.Get();
254+
255+
//方法2
256+
ObjPoolManager.PushObject<TestObjectClass>("TestObjectClass", new TestObjectClass("abcd", 100, 200f));
257+
var testObj1 = ObjPoolManager.GetObject<TestObjectClass>("TestObjectClass");
258+
259+
Debugger.Log("TestObjectClass--->>>" + testObj1.ToString());
260+
261+
//游戏对象池测试
262+
var prefab = Resources.Load("TestGameObjectPrefab", typeof(GameObject)) as GameObject;
263+
var gameObjPool = ObjPoolManager.CreatePool("TestGameObject", 5, 10, prefab);
264+
265+
var gameObj = Instantiate(prefab) as GameObject;
266+
gameObj.name = "TestGameObject_01";
267+
gameObj.transform.localScale = Vector3.one;
268+
gameObj.transform.localPosition = Vector3.zero;
269+
270+
ObjPoolManager.PushObject("TestGameObject", gameObj);
271+
var backObj = ObjPoolManager.GetObject("TestGameObject");
272+
backObj.transform.SetParent(null);
273+
274+
Debug.Log("TestGameObject--->>>" + backObj);
248275
}
249276

250277
/// <summary>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine.Events;
5+
6+
namespace LuaFramework {
7+
/// <summary>
8+
/// 对象池管理器,分普通类对象池+资源游戏对象池
9+
/// </summary>
10+
public class ObjectPoolManager : Manager {
11+
private Transform m_PoolRootObject = null;
12+
private Dictionary<string, object> m_ObjectPools = new Dictionary<string, object>();
13+
private Dictionary<string, GameObjectPool> m_GameObjectPools = new Dictionary<string, GameObjectPool>();
14+
15+
Transform PoolRootObject {
16+
get {
17+
if (m_PoolRootObject == null) {
18+
var objectPool = new GameObject("ObjectPool");
19+
objectPool.transform.SetParent(transform);
20+
objectPool.transform.localScale = Vector3.one;
21+
objectPool.transform.localPosition = Vector3.zero;
22+
m_PoolRootObject = objectPool.transform;
23+
}
24+
return m_PoolRootObject;
25+
}
26+
}
27+
28+
public GameObjectPool CreatePool(string poolName, int initSize, int maxSize, GameObject prefab) {
29+
var pool = new GameObjectPool(poolName, prefab, initSize, maxSize, PoolRootObject);
30+
m_GameObjectPools[poolName] = pool;
31+
return pool;
32+
}
33+
34+
public GameObjectPool GetPool(string poolName) {
35+
if (m_GameObjectPools.ContainsKey(poolName)) {
36+
return m_GameObjectPools[poolName];
37+
}
38+
return null;
39+
}
40+
41+
public GameObject GetObject(string poolName) {
42+
GameObject result = null;
43+
if (m_GameObjectPools.ContainsKey(poolName)) {
44+
GameObjectPool pool = m_GameObjectPools[poolName];
45+
result = pool.NextAvailableObject();
46+
if (result == null) {
47+
Debug.LogWarning("No object available in pool. Consider setting fixedSize to false.: " + poolName);
48+
}
49+
} else {
50+
Debug.LogError("Invalid pool name specified: " + poolName);
51+
}
52+
return result;
53+
}
54+
55+
public void PushObject(string poolName, GameObject go) {
56+
if (m_GameObjectPools.ContainsKey(poolName)) {
57+
GameObjectPool pool = m_GameObjectPools[poolName];
58+
pool.ReturnObjectToPool(poolName, go);
59+
} else {
60+
Debug.LogWarning("No pool available with name: " + poolName);
61+
}
62+
}
63+
64+
///-----------------------------------------------------------------------------------------------
65+
66+
public ObjectPool<T> CreatePool<T>(string poolName, UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease) where T : class {
67+
var pool = new ObjectPool<T>(actionOnGet, actionOnRelease);
68+
m_ObjectPools[poolName] = pool;
69+
return pool;
70+
}
71+
72+
public ObjectPool<T> GetPool<T>(string poolName) where T : class {
73+
ObjectPool<T> pool = null;
74+
if (m_ObjectPools.ContainsKey(poolName)) {
75+
pool = m_ObjectPools[poolName] as ObjectPool<T>;
76+
}
77+
return pool;
78+
}
79+
80+
public T GetObject<T>(string poolName) where T : class {
81+
var pool = GetPool<T>(poolName);
82+
if (pool != null) {
83+
return pool.Get();
84+
}
85+
return default(T);
86+
}
87+
88+
public void PushObject<T>(string poolName, T obj) where T : class {
89+
var pool = GetPool<T>(poolName);
90+
if (pool != null) {
91+
pool.Release(obj);
92+
}
93+
}
94+
}
95+
}

Assets/LuaFramework/Scripts/Manager/ObjectPoolManager.cs.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/LuaFramework/Scripts/ObjectPool.meta

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)