Skip to content

Commit 7beaca9

Browse files
committed
优化了对象池返回对象的代码
1 parent 8502a0d commit 7beaca9

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+
}

ProjectSettings/ProjectSettings.asset

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

Unity-Design-Pattern.sln

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11

2-
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2015
4-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity-Design-Pattern", "Unity-Design-Pattern.csproj", "{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}"
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity-Design-Pattern", "Assembly-CSharp.csproj", "{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}"
55
EndProject
66
Global
77
GlobalSection(SolutionConfigurationPlatforms) = preSolution
88
Debug|Any CPU = Debug|Any CPU
99
Release|Any CPU = Release|Any CPU
1010
EndGlobalSection
1111
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12-
{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13-
{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
14-
{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
15-
{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}.Release|Any CPU.Build.0 = Release|Any CPU
12+
{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}.Release|Any CPU.Build.0 = Release|Any CPU
1616
EndGlobalSection
1717
GlobalSection(SolutionProperties) = preSolution
1818
HideSolutionNode = FALSE
1919
EndGlobalSection
20+
GlobalSection(MonoDevelopProperties) = preSolution
21+
StartupItem = Assembly-CSharp.csproj
22+
EndGlobalSection
2023
EndGlobal

0 commit comments

Comments
 (0)