From 0ff651b4e66eef281fe8cdb625b2074a2afe1224 Mon Sep 17 00:00:00 2001
From: w4ffl35 <25737761+w4ffl35@users.noreply.github.com>
Date: Wed, 4 May 2022 21:52:34 -0600
Subject: [PATCH 1/3] Moves to namespaces which correspond to file locations
---
Assets/ObjectPool/Editor/PoolManagerEditor.cs | 1 +
Assets/ObjectPool/Example/Scripts/Benchmarker.cs | 2 +-
Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs | 3 ++-
Assets/ObjectPool/Example/Scripts/Shape.cs | 2 +-
Assets/ObjectPool/Example/Scripts/ShapeManager.cs | 3 ++-
Assets/ObjectPool/Scripts/Pool.cs | 4 ++--
Assets/ObjectPool/Scripts/PoolManager.cs | 4 ++--
Assets/ObjectPool/Scripts/PoolSettings.cs | 4 +---
Assets/ObjectPool/Scripts/PooledItem.cs | 2 +-
9 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/Assets/ObjectPool/Editor/PoolManagerEditor.cs b/Assets/ObjectPool/Editor/PoolManagerEditor.cs
index 1217963..9d24701 100644
--- a/Assets/ObjectPool/Editor/PoolManagerEditor.cs
+++ b/Assets/ObjectPool/Editor/PoolManagerEditor.cs
@@ -1,5 +1,6 @@
using UnityEngine;
using System.Collections;
+using ObjectPool.Scripts;
using UnityEditor;
namespace GameObjectPool
diff --git a/Assets/ObjectPool/Example/Scripts/Benchmarker.cs b/Assets/ObjectPool/Example/Scripts/Benchmarker.cs
index 7a6aef2..4746622 100644
--- a/Assets/ObjectPool/Example/Scripts/Benchmarker.cs
+++ b/Assets/ObjectPool/Example/Scripts/Benchmarker.cs
@@ -1,6 +1,6 @@
using UnityEngine;
-namespace GameObjectPool
+namespace ObjectPool.Example.Scripts
{
class Benchmarker : MonoBehaviour
{
diff --git a/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs b/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
index a55cd14..33c70fa 100644
--- a/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
+++ b/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
@@ -1,6 +1,7 @@
+using ObjectPool.Scripts;
using UnityEngine;
-namespace GameObjectPool
+namespace ObjectPool.Example.Scripts
{
class PoolDiagnostics : Benchmarker
{
diff --git a/Assets/ObjectPool/Example/Scripts/Shape.cs b/Assets/ObjectPool/Example/Scripts/Shape.cs
index 144435f..61d2d89 100644
--- a/Assets/ObjectPool/Example/Scripts/Shape.cs
+++ b/Assets/ObjectPool/Example/Scripts/Shape.cs
@@ -1,6 +1,6 @@
using UnityEngine;
-namespace GameObjectPool
+namespace ObjectPool.Example.Scripts
{
class Shape : MonoBehaviour
{
diff --git a/Assets/ObjectPool/Example/Scripts/ShapeManager.cs b/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
index 53b088e..b3664eb 100644
--- a/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
+++ b/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
@@ -1,6 +1,7 @@
+using ObjectPool.Scripts;
using UnityEngine;
-namespace GameObjectPool
+namespace ObjectPool.Example.Scripts
{
///
/// This class is meant to demonstrate how to use an object pool.
diff --git a/Assets/ObjectPool/Scripts/Pool.cs b/Assets/ObjectPool/Scripts/Pool.cs
index 120405d..10c90ae 100644
--- a/Assets/ObjectPool/Scripts/Pool.cs
+++ b/Assets/ObjectPool/Scripts/Pool.cs
@@ -1,7 +1,7 @@
-using UnityEngine;
using System.Collections.Generic;
+using UnityEngine;
-namespace GameObjectPool
+namespace ObjectPool.Scripts
{
public class Pool : Queue
{
diff --git a/Assets/ObjectPool/Scripts/PoolManager.cs b/Assets/ObjectPool/Scripts/PoolManager.cs
index 5178cda..54747d7 100644
--- a/Assets/ObjectPool/Scripts/PoolManager.cs
+++ b/Assets/ObjectPool/Scripts/PoolManager.cs
@@ -1,7 +1,7 @@
-using UnityEngine;
using System.Collections.Generic;
+using UnityEngine;
-namespace GameObjectPool
+namespace ObjectPool.Scripts
{
public class PoolManager : MonoBehaviour
{
diff --git a/Assets/ObjectPool/Scripts/PoolSettings.cs b/Assets/ObjectPool/Scripts/PoolSettings.cs
index c4a8923..f44e858 100644
--- a/Assets/ObjectPool/Scripts/PoolSettings.cs
+++ b/Assets/ObjectPool/Scripts/PoolSettings.cs
@@ -1,8 +1,6 @@
using UnityEngine;
-using System;
-using System.Collections;
-namespace GameObjectPool
+namespace ObjectPool.Scripts
{
[System.Serializable]
public class PoolSettings
diff --git a/Assets/ObjectPool/Scripts/PooledItem.cs b/Assets/ObjectPool/Scripts/PooledItem.cs
index e53d613..1d88c6c 100644
--- a/Assets/ObjectPool/Scripts/PooledItem.cs
+++ b/Assets/ObjectPool/Scripts/PooledItem.cs
@@ -1,6 +1,6 @@
using UnityEngine;
-namespace GameObjectPool
+namespace ObjectPool.Scripts
{
public class PooledItem : MonoBehaviour
{
From f8d3f2e34932170409ccb0dad0662f16860fa4d0 Mon Sep 17 00:00:00 2001
From: w4ffl35 <25737761+w4ffl35@users.noreply.github.com>
Date: Wed, 4 May 2022 21:57:08 -0600
Subject: [PATCH 2/3] Improvements to syntax and style
---
.../ObjectPool/Example/Scripts/Benchmarker.cs | 40 +++--
.../Example/Scripts/PoolDiagnostics.cs | 8 +-
Assets/ObjectPool/Example/Scripts/Shape.cs | 10 +-
.../Example/Scripts/ShapeManager.cs | 32 ++--
Assets/ObjectPool/Scripts/Pool.cs | 144 ++++++++---------
Assets/ObjectPool/Scripts/PoolManager.cs | 147 ++++++++----------
Assets/ObjectPool/Scripts/PoolSettings.cs | 7 +-
Assets/ObjectPool/Scripts/PooledItem.cs | 18 +--
8 files changed, 182 insertions(+), 224 deletions(-)
diff --git a/Assets/ObjectPool/Example/Scripts/Benchmarker.cs b/Assets/ObjectPool/Example/Scripts/Benchmarker.cs
index 4746622..d8f8ad8 100644
--- a/Assets/ObjectPool/Example/Scripts/Benchmarker.cs
+++ b/Assets/ObjectPool/Example/Scripts/Benchmarker.cs
@@ -2,19 +2,25 @@
namespace ObjectPool.Example.Scripts
{
- class Benchmarker : MonoBehaviour
+ internal class Benchmarker : MonoBehaviour
{
- bool runDiagnostics;
- float[] results;
- int totalRuns;
- int maxRuns;
- string report;
- float total;
+ private int maxRuns;
+ private string report;
+ private float[] results;
+ private bool runDiagnostics;
protected float runstart;
+ private float total;
+ private int totalRuns;
- protected bool RunDiagnostics
+ protected bool RunDiagnostics => totalRuns < maxRuns && runDiagnostics;
+
+ private void OnGUI()
{
- get { return totalRuns < maxRuns && runDiagnostics; }
+ if (report != null)
+ GUI.Label(
+ new Rect(10, 90, 600, 20),
+ report
+ );
}
protected void StartDiagnostics(int maxRuns)
@@ -29,21 +35,13 @@ protected void StartDiagnostics(int maxRuns)
protected void Run()
{
- float t = Time.realtimeSinceStartup - runstart;
+ var t = Time.realtimeSinceStartup - runstart;
results[totalRuns] = t;
total += results[totalRuns];
- report = "Average runtime (" + (totalRuns + 1) + " iterations): " + total / (float)totalRuns + " with a total runtime of " + total;
+ report = "Average runtime (" + (totalRuns + 1) + " iterations): " + total / totalRuns +
+ " with a total runtime of " + total;
totalRuns += 1;
if (totalRuns >= maxRuns) runDiagnostics = false;
}
-
- void OnGUI()
- {
- if (report != null)
- GUI.Label(
- new Rect(10, 90, 600, 20),
- report
- );
- }
}
-}
+}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs b/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
index 33c70fa..d82d146 100644
--- a/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
+++ b/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
@@ -3,17 +3,17 @@
namespace ObjectPool.Example.Scripts
{
- class PoolDiagnostics : Benchmarker
+ internal class PoolDiagnostics : Benchmarker
{
public string poolName = "";
- void Update()
+ private void Update()
{
if (Input.GetMouseButton(0)) StartDiagnostics(5000);
if (RunDiagnostics) Run();
}
- new protected void Run()
+ protected new void Run()
{
runstart = Time.realtimeSinceStartup;
var obj = PoolManager.Get(poolName);
@@ -26,4 +26,4 @@ void Update()
);
}
}
-}
+}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Example/Scripts/Shape.cs b/Assets/ObjectPool/Example/Scripts/Shape.cs
index 61d2d89..78bfb83 100644
--- a/Assets/ObjectPool/Example/Scripts/Shape.cs
+++ b/Assets/ObjectPool/Example/Scripts/Shape.cs
@@ -2,17 +2,17 @@
namespace ObjectPool.Example.Scripts
{
- class Shape : MonoBehaviour
+ internal class Shape : MonoBehaviour
{
public float timeOutSpeed = 0.2f;
- private float timeout = 0;
+ private float timeout;
- void Awake()
+ private void Awake()
{
timeout = 0;
}
- void Update()
+ private void Update()
{
timeout += Time.deltaTime * timeOutSpeed;
@@ -23,4 +23,4 @@ void Update()
}
}
}
-}
+}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Example/Scripts/ShapeManager.cs b/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
index b3664eb..939598f 100644
--- a/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
+++ b/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
@@ -4,13 +4,13 @@
namespace ObjectPool.Example.Scripts
{
///
- /// This class is meant to demonstrate how to use an object pool.
- /// Use the same techniques found in this class wherever you need to spawn a game object from the Object Pool.
+ /// This class is meant to demonstrate how to use an object pool.
+ /// Use the same techniques found in this class wherever you need to spawn a game object from the Object Pool.
///
- class ShapeManager : MonoBehaviour
+ internal class ShapeManager : MonoBehaviour
{
- const string cubePool = "CubePool";
- const string spherePool = "SpherePool";
+ private const string cubePool = "CubePool";
+ private const string spherePool = "SpherePool";
public GameObject spherePrefab;
// Use the following code to add a pool at run-time (not recommended)
@@ -30,29 +30,23 @@ private void Awake()
*/
///
- /// Listen for mouse clicks and get an object from the appropriate PoolManager.
- /// After an object is returned from the PoolManager, it is passed to the SetPosition method.
- /// These same techniques could be used for things such as FPS bullet spawners in an FPS game.
+ /// Listen for mouse clicks and get an object from the appropriate PoolManager.
+ /// After an object is returned from the PoolManager, it is passed to the SetPosition method.
+ /// These same techniques could be used for things such as FPS bullet spawners in an FPS game.
///
- void Update()
+ private void Update()
{
- Vector3 location = new Vector3(
+ var location = new Vector3(
Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
0
);
// On left mouse click, spawn a cube object from the cube pool.
- if (Input.GetMouseButton(0))
- {
- PoolManager.Get(cubePool, location, Random.rotation);
- }
+ if (Input.GetMouseButton(0)) PoolManager.Get(cubePool, location, Random.rotation);
// On right mouse click, spawn a sphere object from the sphere pool.
- if (Input.GetMouseButton(1))
- {
- PoolManager.Get(spherePool, location, Random.rotation);
- }
+ if (Input.GetMouseButton(1)) PoolManager.Get(spherePool, location, Random.rotation);
}
}
-}
+}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Scripts/Pool.cs b/Assets/ObjectPool/Scripts/Pool.cs
index 10c90ae..4c1eccb 100644
--- a/Assets/ObjectPool/Scripts/Pool.cs
+++ b/Assets/ObjectPool/Scripts/Pool.cs
@@ -5,77 +5,11 @@ namespace ObjectPool.Scripts
{
public class Pool : Queue
{
- PoolSettings settings;
- List activeItems;
+ private List activeItems;
- #region Properties
- List ActiveItems
- {
- get
- {
- if (activeItems == null) activeItems = new List();
- return activeItems;
- }
- }
-
- public PoolSettings Settings
- {
- get { return settings; }
- }
-
- public int TotalActive
- {
- get { return ActiveItems.Count; }
- }
-
- public int TotalInPool
- {
- get { return Count; }
- }
-
- bool GrowPool
- {
- get
- {
- return Count == 0 && (settings.allowUnrestrictedGrowth || TotalActive < settings.maxItemCount);
- }
- }
-
- bool CanDequeue => TotalInPool > 0;
-
- public GameObject Get
- {
- get
- {
- GameObject obj = null;
- if (CanDequeue) obj = Dequeue();
- if (obj == null) obj = (GrowPool) ? NewItem : null;
- if (obj != null)
- {
- ActiveItems.Add(obj);
- obj.SetActive(true);
- }
- return obj;
- }
- }
-
- private GameObject NewItem
- {
- get
- {
- var g = GameObject.Instantiate(settings.prefab);
- g.transform.SetParent(settings.parent);
- g.AddComponent();
- InitializePoolItem(g);
- g.SetActive(false);
- return g;
- }
- }
- #endregion
-
- public Pool(PoolSettings poolSettings) : base()
+ public Pool(PoolSettings poolSettings)
{
- settings = poolSettings;
+ Settings = poolSettings;
InitializePool();
}
@@ -83,35 +17,33 @@ private void InitializePoolItem(GameObject obj)
{
var pi = obj.GetComponent();
if (pi == null) return;
- pi.name = settings.name;
+ pi.name = Settings.name;
pi.Pool = this;
}
private bool GameObjectBelongsToThisPool(GameObject obj)
{
var pooledItem = obj.GetComponent();
- return pooledItem != null && pooledItem.name == settings.name;
+ return pooledItem != null && pooledItem.name == Settings.name;
}
private void FindExistingPoolItems()
{
- GameObject[] objects = Object.FindObjectsOfType(true);
+ var objects = Object.FindObjectsOfType(true);
foreach (var obj in objects)
- {
if (GameObjectBelongsToThisPool(obj))
{
InitializePoolItem(obj);
Insert(obj);
}
- }
}
public void InitializePool()
{
FindExistingPoolItems();
- var max = settings.startingItemCount;
- if (!settings.allowUnrestrictedGrowth && max > settings.maxItemCount) max = settings.maxItemCount;
- for (int i = TotalInPool; i < max; i++)
+ var max = Settings.startingItemCount;
+ if (!Settings.allowUnrestrictedGrowth && max > Settings.maxItemCount) max = Settings.maxItemCount;
+ for (var i = TotalInPool; i < max; i++)
Insert(NewItem);
}
@@ -122,7 +54,7 @@ private void Insert(GameObject obj)
public void DeactivateAll()
{
- foreach (GameObject obj in ActiveItems) obj.SetActive(false);
+ foreach (var obj in ActiveItems) obj.SetActive(false);
}
public void Deactivate(GameObject obj)
@@ -136,5 +68,59 @@ public void DestroyAll()
foreach (var obj in ActiveItems) Object.Destroy(obj);
foreach (var obj in this) Object.Destroy(obj);
}
+
+ #region Properties
+
+ private List ActiveItems
+ {
+ get
+ {
+ if (activeItems == null) activeItems = new List();
+ return activeItems;
+ }
+ }
+
+ public PoolSettings Settings { get; }
+
+ public int TotalActive => ActiveItems.Count;
+
+ public int TotalInPool => Count;
+
+ private bool GrowPool =>
+ Count == 0 && (Settings.allowUnrestrictedGrowth || TotalActive < Settings.maxItemCount);
+
+ private bool CanDequeue => TotalInPool > 0;
+
+ public GameObject Get
+ {
+ get
+ {
+ GameObject obj = null;
+ if (CanDequeue) obj = Dequeue();
+ if (obj == null) obj = GrowPool ? NewItem : null;
+ if (obj != null)
+ {
+ ActiveItems.Add(obj);
+ obj.SetActive(true);
+ }
+
+ return obj;
+ }
+ }
+
+ private GameObject NewItem
+ {
+ get
+ {
+ var g = Object.Instantiate(Settings.prefab);
+ g.transform.SetParent(Settings.parent);
+ g.AddComponent();
+ InitializePoolItem(g);
+ g.SetActive(false);
+ return g;
+ }
+ }
+
+ #endregion
}
-}
+}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Scripts/PoolManager.cs b/Assets/ObjectPool/Scripts/PoolManager.cs
index 54747d7..bd3acb7 100644
--- a/Assets/ObjectPool/Scripts/PoolManager.cs
+++ b/Assets/ObjectPool/Scripts/PoolManager.cs
@@ -5,37 +5,31 @@ namespace ObjectPool.Scripts
{
public class PoolManager : MonoBehaviour
{
- #region Fields
- const string ERROR_MISSING_MANAGER = "PoolManager script required.";
- public bool debug = true;
- public List m_pool = new List();
- public bool showAnalytics = false;
- private static PoolManager instance;
- private Dictionary pools;
- #endregion
-
- #region Properties
- public static List PoolSettings
- {
- get { return Instance.m_pool; }
- }
-
- private static PoolManager Instance
+ private void Start()
{
- get { return instance; }
- }
-
- public static Dictionary Pools
- {
- get => Instance.pools ?? (Instance.pools = new Dictionary());
- private set => Instance.pools = value;
+ if (Instance == null) Instance = this;
+ GeneratePools(false);
}
- #endregion
- void Start()
+ ///
+ /// Display information about a PoolManager
+ ///
+ private void OnGUI()
{
- if (instance == null) instance = this;
- GeneratePools(false);
+ if (showAnalytics)
+ for (var i = 0; i < m_pool.Count; i++)
+ if (m_pool[i].showAnalytics)
+ {
+ var name = m_pool[i].name;
+ GUI.Label(
+ new Rect(10, 10 + i * 40, 400, 20),
+ name
+ );
+ GUI.Label(
+ new Rect(10, 30 + i * 40, 400, 20),
+ "Available in Pool: " + TotalInPool(name) + " Active: " + TotalActive(name)
+ );
+ }
}
public static void AddPool(PoolSettings settings)
@@ -47,38 +41,34 @@ public static void AddPool(PoolSettings settings)
private static void PopulatePools()
{
foreach (var settings in PoolSettings)
- {
if (!Pools.ContainsKey(settings.name))
- {
Pools.Add(settings.name, new Pool(settings));
- }
- }
}
public static GameObject Get(string poolName, Vector3 position, Quaternion rotation, Transform parent)
{
- GameObject obj = Get(poolName, position, rotation);
+ var obj = Get(poolName, position, rotation);
if (obj) obj.transform.SetParent(parent);
return obj;
}
public static GameObject Get(string poolName, Vector3 position, Quaternion rotation)
{
- GameObject obj = Get(poolName, position);
+ var obj = Get(poolName, position);
if (obj) obj.transform.rotation = rotation;
return obj;
}
public static GameObject Get(string poolName, Vector3 position)
{
- GameObject obj = Get(poolName);
+ var obj = Get(poolName);
if (obj) obj.transform.position = position;
return obj;
}
-
+
public static GameObject Get(string poolName, Transform transform)
{
- GameObject obj = Get(poolName);
+ var obj = Get(poolName);
obj.transform.position = transform.position;
obj.transform.rotation = transform.rotation;
return obj;
@@ -86,24 +76,21 @@ public static GameObject Get(string poolName, Transform transform)
public static GameObject Get(string poolName)
{
- if (Pools.ContainsKey(poolName))
- {
- return Instance.GetPool(poolName).Get;
- }
+ if (Pools.ContainsKey(poolName)) return Instance.GetPool(poolName).Get;
return null;
}
public static GameObject[] Get(string poolName, int total)
{
- GameObject[] objects = new GameObject[total];
- for (int i = 0; i < total; i++)
- {
- objects[i] = Instance.GetPool(poolName).Get;
- }
+ var objects = new GameObject[total];
+ for (var i = 0; i < total; i++) objects[i] = Instance.GetPool(poolName).Get;
return objects;
}
- public Pool GetPool(string poolName) => Pools[poolName];
+ public Pool GetPool(string poolName)
+ {
+ return Pools[poolName];
+ }
public static int TotalInPool(string poolName)
{
@@ -119,52 +106,44 @@ public static int TotalActive(string poolName)
return 0;
}
- ///
- /// Display information about a PoolManager
- ///
- void OnGUI()
- {
- if (showAnalytics)
- {
- for (int i = 0; i < m_pool.Count; i++)
- {
- if (m_pool[i].showAnalytics)
- {
- string name = m_pool[i].name;
- GUI.Label(
- new Rect(10, 10 + (i * 40), 400, 20),
- name
- );
- GUI.Label(
- new Rect(10, 30 + (i * 40), 400, 20),
- "Available in Pool: " + TotalInPool(name) + " Active: " + TotalActive(name)
- );
- }
- }
- }
- }
-
public void GeneratePools(bool doDestroy = true)
{
- if (instance == null) instance = this;
-
- GameObject[] objects = FindObjectsOfType(true);
+ if (Instance == null) Instance = this;
+
+ var objects = FindObjectsOfType(true);
if (doDestroy)
- {
foreach (var obj in objects)
- {
- if (obj.GetComponent() != null) DestroyImmediate(obj);
- }
- }
+ if (obj.GetComponent() != null)
+ DestroyImmediate(obj);
Pools = new Dictionary();
foreach (var settings in m_pool)
- {
if (!Pools.ContainsKey(settings.name))
- {
Pools.Add(settings.name, new Pool(settings));
- }
- }
}
+
+ #region Fields
+
+ private const string ERROR_MISSING_MANAGER = "PoolManager script required.";
+ public bool debug = true;
+ public List m_pool = new();
+ public bool showAnalytics;
+ private Dictionary pools;
+
+ #endregion
+
+ #region Properties
+
+ public static List PoolSettings => Instance.m_pool;
+
+ private static PoolManager Instance { get; set; }
+
+ public static Dictionary Pools
+ {
+ get => Instance.pools ?? (Instance.pools = new Dictionary());
+ private set => Instance.pools = value;
+ }
+
+ #endregion
}
-}
+}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Scripts/PoolSettings.cs b/Assets/ObjectPool/Scripts/PoolSettings.cs
index f44e858..ae6bd18 100644
--- a/Assets/ObjectPool/Scripts/PoolSettings.cs
+++ b/Assets/ObjectPool/Scripts/PoolSettings.cs
@@ -1,8 +1,9 @@
+using System;
using UnityEngine;
namespace ObjectPool.Scripts
{
- [System.Serializable]
+ [Serializable]
public class PoolSettings
{
public string name;
@@ -10,7 +11,7 @@ public class PoolSettings
public int startingItemCount;
public int maxItemCount;
public Transform parent;
- public bool allowUnrestrictedGrowth = false;
+ public bool allowUnrestrictedGrowth;
public bool showAnalytics;
}
-}
+}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Scripts/PooledItem.cs b/Assets/ObjectPool/Scripts/PooledItem.cs
index 1d88c6c..40cae31 100644
--- a/Assets/ObjectPool/Scripts/PooledItem.cs
+++ b/Assets/ObjectPool/Scripts/PooledItem.cs
@@ -7,16 +7,10 @@ public class PooledItem : MonoBehaviour
public string name;
public Pool Pool;
-
- public Rigidbody Rigidbody { get; private set; }
- void OnDisable()
- {
- if (gameObject == null) return;
- Pool?.Deactivate(gameObject);
- }
+ public Rigidbody Rigidbody { get; private set; }
- void OnEnable()
+ private void OnEnable()
{
if (Rigidbody == null) Rigidbody = GetComponent();
if (Rigidbody != null)
@@ -25,5 +19,11 @@ void OnEnable()
Rigidbody.angularVelocity = Vector3.zero;
}
}
+
+ private void OnDisable()
+ {
+ if (gameObject == null) return;
+ Pool?.Deactivate(gameObject);
+ }
}
-}
+}
\ No newline at end of file
From dd18c79d954413c92e648c520ef1911054f5b5be Mon Sep 17 00:00:00 2001
From: w4ffl35 <25737761+w4ffl35@users.noreply.github.com>
Date: Wed, 4 May 2022 22:16:44 -0600
Subject: [PATCH 3/3] Syntax and style improvements to code
---
Assets/ObjectPool/Editor/PoolManagerEditor.cs | 3 +-
.../ObjectPool/Example/Scenes/Example.unity | 7 +-
.../ObjectPool/Example/Scripts/BenchMarker.cs | 49 ++++++++++
...enchmarker.cs.meta => BenchMarker.cs.meta} | 3 +-
.../ObjectPool/Example/Scripts/Benchmarker.cs | 47 ---------
.../Example/Scripts/PoolDiagnostics.cs | 17 ++--
Assets/ObjectPool/Example/Scripts/Shape.cs | 10 +-
.../Example/Scripts/ShapeManager.cs | 8 +-
Assets/ObjectPool/Scripts/Pool.cs | 96 ++++++++-----------
Assets/ObjectPool/Scripts/PoolManager.cs | 90 +++++++----------
Assets/ObjectPool/Scripts/PooledItem.cs | 10 +-
11 files changed, 149 insertions(+), 191 deletions(-)
create mode 100644 Assets/ObjectPool/Example/Scripts/BenchMarker.cs
rename Assets/ObjectPool/Example/Scripts/{Benchmarker.cs.meta => BenchMarker.cs.meta} (84%)
delete mode 100644 Assets/ObjectPool/Example/Scripts/Benchmarker.cs
diff --git a/Assets/ObjectPool/Editor/PoolManagerEditor.cs b/Assets/ObjectPool/Editor/PoolManagerEditor.cs
index 9d24701..979cedc 100644
--- a/Assets/ObjectPool/Editor/PoolManagerEditor.cs
+++ b/Assets/ObjectPool/Editor/PoolManagerEditor.cs
@@ -1,5 +1,4 @@
using UnityEngine;
-using System.Collections;
using ObjectPool.Scripts;
using UnityEditor;
@@ -15,7 +14,7 @@ public override void OnInspectorGUI()
if (GUILayout.Button("Add Pool"))
{
- poolManager.m_pool.Add(new PoolSettings());
+ poolManager.mPool.Add(new PoolSettings());
}
if (GUILayout.Button("Regenerate pools"))
diff --git a/Assets/ObjectPool/Example/Scenes/Example.unity b/Assets/ObjectPool/Example/Scenes/Example.unity
index 1e1f680..d97796d 100644
--- a/Assets/ObjectPool/Example/Scenes/Example.unity
+++ b/Assets/ObjectPool/Example/Scenes/Example.unity
@@ -2817,20 +2817,19 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0dda56d895cffe44dadc9670de012a1b, type: 3}
m_Name:
m_EditorClassIdentifier:
- debug: 1
- m_pool:
+ mPool:
- name: SpherePool
prefab: {fileID: 1019533563834558, guid: a190bad6138ae9444a900486a95d3d30, type: 3}
startingItemCount: 10
maxItemCount: 10
- parent: {fileID: 1906366004}
+ parent: {fileID: 0}
allowUnrestrictedGrowth: 0
showAnalytics: 0
- name: CubePool
prefab: {fileID: 1512229642624436, guid: 7aa29bc7804e6b94bb43136a04e2f70e, type: 3}
startingItemCount: 10
maxItemCount: 10
- parent: {fileID: 1906366004}
+ parent: {fileID: 0}
allowUnrestrictedGrowth: 1
showAnalytics: 0
showAnalytics: 0
diff --git a/Assets/ObjectPool/Example/Scripts/BenchMarker.cs b/Assets/ObjectPool/Example/Scripts/BenchMarker.cs
new file mode 100644
index 0000000..3fef307
--- /dev/null
+++ b/Assets/ObjectPool/Example/Scripts/BenchMarker.cs
@@ -0,0 +1,49 @@
+using UnityEngine;
+
+namespace ObjectPool.Example.Scripts
+{
+ internal class BenchMarker : MonoBehaviour
+ {
+ #region Fields
+ private int _maxRuns;
+ private string _report;
+ private float[] _results;
+ private bool _runDiagnostics;
+ protected float runStart;
+ private float _total;
+ private int _totalRuns;
+ #endregion
+
+ protected bool RunDiagnostics => _totalRuns < _maxRuns && _runDiagnostics;
+
+ private void OnGUI()
+ {
+ if (_report != null)
+ GUI.Label(
+ new Rect(10, 90, 600, 20),
+ _report
+ );
+ }
+
+ protected void StartDiagnostics(int maxRuns)
+ {
+ _maxRuns = maxRuns;
+ _runDiagnostics = true;
+ _results = new float[maxRuns];
+ _totalRuns = 0;
+ _total = 0;
+ }
+
+
+ protected void Run()
+ {
+ var t = Time.realtimeSinceStartup - runStart;
+ _results[_totalRuns] = t;
+ _total += _results[_totalRuns];
+ _report = "Average runtime (" + (_totalRuns + 1) + " iterations): " + _total / _totalRuns +
+ " with a total runtime of " + _total;
+ _totalRuns += 1;
+ if (_totalRuns >= _maxRuns) _runDiagnostics = false;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Example/Scripts/Benchmarker.cs.meta b/Assets/ObjectPool/Example/Scripts/BenchMarker.cs.meta
similarity index 84%
rename from Assets/ObjectPool/Example/Scripts/Benchmarker.cs.meta
rename to Assets/ObjectPool/Example/Scripts/BenchMarker.cs.meta
index f1780a9..97ff5a0 100644
--- a/Assets/ObjectPool/Example/Scripts/Benchmarker.cs.meta
+++ b/Assets/ObjectPool/Example/Scripts/BenchMarker.cs.meta
@@ -1,8 +1,7 @@
fileFormatVersion: 2
guid: 6b9ad2937fa2db4489af5863f635956b
-timeCreated: 1522248767
-licenseType: Free
MonoImporter:
+ externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
diff --git a/Assets/ObjectPool/Example/Scripts/Benchmarker.cs b/Assets/ObjectPool/Example/Scripts/Benchmarker.cs
deleted file mode 100644
index d8f8ad8..0000000
--- a/Assets/ObjectPool/Example/Scripts/Benchmarker.cs
+++ /dev/null
@@ -1,47 +0,0 @@
-using UnityEngine;
-
-namespace ObjectPool.Example.Scripts
-{
- internal class Benchmarker : MonoBehaviour
- {
- private int maxRuns;
- private string report;
- private float[] results;
- private bool runDiagnostics;
- protected float runstart;
- private float total;
- private int totalRuns;
-
- protected bool RunDiagnostics => totalRuns < maxRuns && runDiagnostics;
-
- private void OnGUI()
- {
- if (report != null)
- GUI.Label(
- new Rect(10, 90, 600, 20),
- report
- );
- }
-
- protected void StartDiagnostics(int maxRuns)
- {
- this.maxRuns = maxRuns;
- runDiagnostics = true;
- results = new float[maxRuns];
- totalRuns = 0;
- total = 0;
- }
-
-
- protected void Run()
- {
- var t = Time.realtimeSinceStartup - runstart;
- results[totalRuns] = t;
- total += results[totalRuns];
- report = "Average runtime (" + (totalRuns + 1) + " iterations): " + total / totalRuns +
- " with a total runtime of " + total;
- totalRuns += 1;
- if (totalRuns >= maxRuns) runDiagnostics = false;
- }
- }
-}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs b/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
index d82d146..525b29b 100644
--- a/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
+++ b/Assets/ObjectPool/Example/Scripts/PoolDiagnostics.cs
@@ -3,7 +3,7 @@
namespace ObjectPool.Example.Scripts
{
- internal class PoolDiagnostics : Benchmarker
+ internal class PoolDiagnostics : BenchMarker
{
public string poolName = "";
@@ -13,17 +13,18 @@ private void Update()
if (RunDiagnostics) Run();
}
- protected new void Run()
+ private new void Run()
{
- runstart = Time.realtimeSinceStartup;
+ runStart = Time.realtimeSinceStartup;
var obj = PoolManager.Get(poolName);
base.Run();
if (obj == null) return;
- obj.transform.position = new Vector3(
- Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
- Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
- 0
- );
+ if (Camera.main != null)
+ obj.transform.position = new Vector3(
+ Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
+ Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
+ 0
+ );
}
}
}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Example/Scripts/Shape.cs b/Assets/ObjectPool/Example/Scripts/Shape.cs
index 78bfb83..c72df36 100644
--- a/Assets/ObjectPool/Example/Scripts/Shape.cs
+++ b/Assets/ObjectPool/Example/Scripts/Shape.cs
@@ -5,20 +5,20 @@ namespace ObjectPool.Example.Scripts
internal class Shape : MonoBehaviour
{
public float timeOutSpeed = 0.2f;
- private float timeout;
+ private float _timeout;
private void Awake()
{
- timeout = 0;
+ _timeout = 0;
}
private void Update()
{
- timeout += Time.deltaTime * timeOutSpeed;
+ _timeout += Time.deltaTime * timeOutSpeed;
- if (timeout >= 1f)
+ if (_timeout >= 1f)
{
- timeout = 0;
+ _timeout = 0;
transform.gameObject.SetActive(false);
}
}
diff --git a/Assets/ObjectPool/Example/Scripts/ShapeManager.cs b/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
index 939598f..3ab5b1d 100644
--- a/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
+++ b/Assets/ObjectPool/Example/Scripts/ShapeManager.cs
@@ -9,8 +9,8 @@ namespace ObjectPool.Example.Scripts
///
internal class ShapeManager : MonoBehaviour
{
- private const string cubePool = "CubePool";
- private const string spherePool = "SpherePool";
+ private const string CubePool = "CubePool";
+ private const string SpherePool = "SpherePool";
public GameObject spherePrefab;
// Use the following code to add a pool at run-time (not recommended)
@@ -43,10 +43,10 @@ private void Update()
);
// On left mouse click, spawn a cube object from the cube pool.
- if (Input.GetMouseButton(0)) PoolManager.Get(cubePool, location, Random.rotation);
+ if (Input.GetMouseButton(0)) PoolManager.Get(CubePool, location, Random.rotation);
// On right mouse click, spawn a sphere object from the sphere pool.
- if (Input.GetMouseButton(1)) PoolManager.Get(spherePool, location, Random.rotation);
+ if (Input.GetMouseButton(1)) PoolManager.Get(SpherePool, location, Random.rotation);
}
}
}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Scripts/Pool.cs b/Assets/ObjectPool/Scripts/Pool.cs
index 4c1eccb..a3c930e 100644
--- a/Assets/ObjectPool/Scripts/Pool.cs
+++ b/Assets/ObjectPool/Scripts/Pool.cs
@@ -5,7 +5,43 @@ namespace ObjectPool.Scripts
{
public class Pool : Queue
{
- private List activeItems;
+ private List _activeItems;
+
+ #region Properties
+ private List ActiveItems => _activeItems ??= new List();
+ private PoolSettings Settings { get; }
+ public int TotalActive => ActiveItems.Count;
+ public int TotalInPool => Count;
+ private bool GrowPool =>
+ Count == 0 && (Settings.allowUnrestrictedGrowth || TotalActive < Settings.maxItemCount);
+ private bool CanDequeue => TotalInPool > 0;
+
+ public GameObject Get
+ {
+ get
+ {
+ GameObject obj = null;
+ if (CanDequeue) obj = Dequeue();
+ if (obj == null) obj = GrowPool ? NewItem : null;
+ if (obj == null) return obj;
+ ActiveItems.Add(obj);
+ obj.SetActive(true);
+ return obj;
+ }
+ }
+
+ private GameObject NewItem
+ {
+ get
+ {
+ var g = Object.Instantiate(Settings.prefab, Settings.parent, true);
+ g.AddComponent();
+ InitializePoolItem(g);
+ g.SetActive(false);
+ return g;
+ }
+ }
+ #endregion
public Pool(PoolSettings poolSettings)
{
@@ -18,7 +54,7 @@ private void InitializePoolItem(GameObject obj)
var pi = obj.GetComponent();
if (pi == null) return;
pi.name = Settings.name;
- pi.Pool = this;
+ pi.pool = this;
}
private bool GameObjectBelongsToThisPool(GameObject obj)
@@ -38,7 +74,7 @@ private void FindExistingPoolItems()
}
}
- public void InitializePool()
+ private void InitializePool()
{
FindExistingPoolItems();
var max = Settings.startingItemCount;
@@ -68,59 +104,5 @@ public void DestroyAll()
foreach (var obj in ActiveItems) Object.Destroy(obj);
foreach (var obj in this) Object.Destroy(obj);
}
-
- #region Properties
-
- private List ActiveItems
- {
- get
- {
- if (activeItems == null) activeItems = new List();
- return activeItems;
- }
- }
-
- public PoolSettings Settings { get; }
-
- public int TotalActive => ActiveItems.Count;
-
- public int TotalInPool => Count;
-
- private bool GrowPool =>
- Count == 0 && (Settings.allowUnrestrictedGrowth || TotalActive < Settings.maxItemCount);
-
- private bool CanDequeue => TotalInPool > 0;
-
- public GameObject Get
- {
- get
- {
- GameObject obj = null;
- if (CanDequeue) obj = Dequeue();
- if (obj == null) obj = GrowPool ? NewItem : null;
- if (obj != null)
- {
- ActiveItems.Add(obj);
- obj.SetActive(true);
- }
-
- return obj;
- }
- }
-
- private GameObject NewItem
- {
- get
- {
- var g = Object.Instantiate(Settings.prefab);
- g.transform.SetParent(Settings.parent);
- g.AddComponent();
- InitializePoolItem(g);
- g.SetActive(false);
- return g;
- }
- }
-
- #endregion
}
}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Scripts/PoolManager.cs b/Assets/ObjectPool/Scripts/PoolManager.cs
index bd3acb7..7f7b8d6 100644
--- a/Assets/ObjectPool/Scripts/PoolManager.cs
+++ b/Assets/ObjectPool/Scripts/PoolManager.cs
@@ -5,6 +5,21 @@ namespace ObjectPool.Scripts
{
public class PoolManager : MonoBehaviour
{
+ #region Fields
+ public List mPool = new();
+ public bool showAnalytics;
+ private Dictionary _pools;
+ #endregion
+
+ #region Properties
+ private static PoolManager Instance { get; set; }
+ private static Dictionary Pools
+ {
+ get => Instance._pools ?? (Instance._pools = new Dictionary());
+ set => Instance._pools = value;
+ }
+ #endregion
+
private void Start()
{
if (Instance == null) Instance = this;
@@ -16,33 +31,20 @@ private void Start()
///
private void OnGUI()
{
- if (showAnalytics)
- for (var i = 0; i < m_pool.Count; i++)
- if (m_pool[i].showAnalytics)
- {
- var name = m_pool[i].name;
- GUI.Label(
- new Rect(10, 10 + i * 40, 400, 20),
- name
- );
- GUI.Label(
- new Rect(10, 30 + i * 40, 400, 20),
- "Available in Pool: " + TotalInPool(name) + " Active: " + TotalActive(name)
- );
- }
- }
-
- public static void AddPool(PoolSettings settings)
- {
- PoolSettings.Add(settings);
- Pools.Add(settings.name, new Pool(settings));
- }
-
- private static void PopulatePools()
- {
- foreach (var settings in PoolSettings)
- if (!Pools.ContainsKey(settings.name))
- Pools.Add(settings.name, new Pool(settings));
+ if (!showAnalytics) return;
+ for (var i = 0; i < mPool.Count; i++)
+ {
+ if (!mPool[i].showAnalytics) continue;
+ var poolName = mPool[i].name;
+ GUI.Label(
+ new Rect(10, 10 + i * 40, 400, 20),
+ poolName
+ );
+ GUI.Label(
+ new Rect(10, 30 + i * 40, 400, 20),
+ "Available in Pool: " + TotalInPool(poolName) + " Active: " + TotalActive(poolName)
+ );
+ }
}
public static GameObject Get(string poolName, Vector3 position, Quaternion rotation, Transform parent)
@@ -59,7 +61,7 @@ public static GameObject Get(string poolName, Vector3 position, Quaternion rotat
return obj;
}
- public static GameObject Get(string poolName, Vector3 position)
+ private static GameObject Get(string poolName, Vector3 position)
{
var obj = Get(poolName);
if (obj) obj.transform.position = position;
@@ -87,19 +89,19 @@ public static GameObject[] Get(string poolName, int total)
return objects;
}
- public Pool GetPool(string poolName)
+ private Pool GetPool(string poolName)
{
return Pools[poolName];
}
- public static int TotalInPool(string poolName)
+ private static int TotalInPool(string poolName)
{
var pool = Instance.GetPool(poolName);
if (pool != null) return pool.TotalInPool;
return 0;
}
- public static int TotalActive(string poolName)
+ private static int TotalActive(string poolName)
{
var pool = Instance.GetPool(poolName);
if (pool != null) return pool.TotalActive;
@@ -117,33 +119,9 @@ public void GeneratePools(bool doDestroy = true)
DestroyImmediate(obj);
Pools = new Dictionary();
- foreach (var settings in m_pool)
+ foreach (var settings in mPool)
if (!Pools.ContainsKey(settings.name))
Pools.Add(settings.name, new Pool(settings));
}
-
- #region Fields
-
- private const string ERROR_MISSING_MANAGER = "PoolManager script required.";
- public bool debug = true;
- public List m_pool = new();
- public bool showAnalytics;
- private Dictionary pools;
-
- #endregion
-
- #region Properties
-
- public static List PoolSettings => Instance.m_pool;
-
- private static PoolManager Instance { get; set; }
-
- public static Dictionary Pools
- {
- get => Instance.pools ?? (Instance.pools = new Dictionary());
- private set => Instance.pools = value;
- }
-
- #endregion
}
}
\ No newline at end of file
diff --git a/Assets/ObjectPool/Scripts/PooledItem.cs b/Assets/ObjectPool/Scripts/PooledItem.cs
index 40cae31..b50ac76 100644
--- a/Assets/ObjectPool/Scripts/PooledItem.cs
+++ b/Assets/ObjectPool/Scripts/PooledItem.cs
@@ -4,11 +4,9 @@ namespace ObjectPool.Scripts
{
public class PooledItem : MonoBehaviour
{
- public string name;
-
- public Pool Pool;
-
- public Rigidbody Rigidbody { get; private set; }
+ public new string name;
+ public Pool pool;
+ private Rigidbody Rigidbody { get; set; }
private void OnEnable()
{
@@ -23,7 +21,7 @@ private void OnEnable()
private void OnDisable()
{
if (gameObject == null) return;
- Pool?.Deactivate(gameObject);
+ pool?.Deactivate(gameObject);
}
}
}
\ No newline at end of file