Skip to content

Commit f96b9f5

Browse files
authored
Merge pull request QianMo#2 from WilsonUnity/master
优化了对象池返回对象的代码
2 parents 8502a0d + 7beaca9 commit f96b9f5

File tree

11 files changed

+36
-27
lines changed

11 files changed

+36
-27
lines changed

Assets/Behavioral Patterns/Command Pattern/Example2.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Behavioral Patterns/Command Pattern/Example3.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Behavioral Patterns/State Pattern/Exmaple3.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Behavioral Patterns/State Pattern/Exmaple4.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Game Programming Patterns/Object Pool Pattern/Example/Script/Core/PoolManager.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private PoolObject NewObjectInstance()
7777
/// <param name="position"></param>
7878
/// <param name="rotation"></param>
7979
/// <returns></returns>
80-
public GameObject NextAvailableObject(Vector3 position, Quaternion rotation)
80+
public PoolObject NextAvailableObject(Vector3 position, Quaternion rotation)
8181
{
8282
PoolObject po = null;
8383
//池中有可用对象,直接从池中取
@@ -111,8 +111,9 @@ public GameObject NextAvailableObject(Vector3 position, Quaternion rotation)
111111
result.transform.rotation = rotation;
112112
}
113113

114-
return result;
115-
}
114+
return po;
115+
116+
}
116117

117118
/// <summary>
118119
/// 将指定对象返回池中,复杂度o(1)
@@ -247,9 +248,10 @@ private void CreatePools()
247248
/// <param name="position">位置</param>
248249
/// <param name="rotation">rotation</param>
249250
/// <returns></returns>
250-
public GameObject GetObjectFromPool(string poolName, Vector3 position, Quaternion rotation)
251+
public PoolObject GetObjectFromPool(string poolName, Vector3 position, Quaternion rotation)
251252
{
252-
GameObject result = null;
253+
//GameObject result = null;
254+
PoolObject result = null;
253255

254256
if (poolMap.ContainsKey(poolName))
255257
{
@@ -270,12 +272,12 @@ public GameObject GetObjectFromPool(string poolName, Vector3 position, Quaternio
270272
return result;
271273
}
272274

273-
public void ReturnObjectToPool(GameObject go)
275+
public void ReturnObjectToPool(PoolObject po)
274276
{
275-
PoolObject po = go.GetComponent<PoolObject>();
277+
//PoolObject po = go.GetComponent<PoolObject>();
276278
if (po == null)
277279
{
278-
Debug.LogWarning("Specified object is not a pooled instance: " + go.name);
280+
Debug.LogWarning("Specified object is not a pooled instance: " + po.name);
279281
}
280282
else
281283
{

Assets/Game Programming Patterns/Object Pool Pattern/Example/Script/Example/ObjectPoolExample.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public class ObjectPoolExample : MonoBehaviour
1010
//池名称
1111
public string poolName;
1212
//对象List
13-
public List<GameObject> ObjectList = new List<GameObject>();
13+
// public List<GameObject> ObjectList = new List<GameObject>();
14+
public List<PoolObject> ObjectList = new List<PoolObject>();
1415

1516
public void Start()
1617
{
@@ -37,10 +38,10 @@ public void GetObjectFromPool()
3738
pos.y = 0f;
3839
pos.z = Random.Range(-5, 6);
3940

40-
GameObject go = PoolManager.Instance.GetObjectFromPool(poolName, pos, Quaternion.identity);
41-
if (go)
41+
PoolObject po = PoolManager.Instance.GetObjectFromPool(poolName, pos, Quaternion.identity);
42+
if (po)
4243
{
43-
ObjectList.Add(go);
44+
ObjectList.Add(po);
4445
}
4546
}
4647

@@ -68,9 +69,9 @@ public void ReturnAllObjectToPool()
6869
return;
6970
}
7071

71-
foreach (GameObject go in ObjectList)
72+
foreach (PoolObject po in ObjectList)
7273
{
73-
PoolManager.Instance.ReturnObjectToPool(go);
74+
PoolManager.Instance.ReturnObjectToPool(po);
7475
}
7576
ObjectList.Clear();
7677
}

Packages/manifest.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"dependencies": {
3+
}
4+
}
Binary file not shown.
17.5 KB
Binary file not shown.

ProjectSettings/ProjectVersion.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
m_EditorVersion: 5.3.3f1
2-
m_StandardAssetsVersion: 0
1+
m_EditorVersion: 2017.1.5f1

0 commit comments

Comments
 (0)