From 68e74b17266b2473be6f109ebc08a4cace9948de Mon Sep 17 00:00:00 2001
From: LesserKnownThings <ursu.marius.laurentiu@gmail.com>
Date: Fri, 11 Sep 2020 13:29:49 -0400
Subject: [PATCH 1/4] Adding Object Pooling

---
 Assets/Scripts/ObjectPooling/IPoolActions.cs  | 10 +++
 .../ObjectPooling/IPoolActions.cs.meta        | 11 +++
 Assets/Scripts/ObjectPooling/Pool.cs          | 84 +++++++++++++++++++
 Assets/Scripts/ObjectPooling/Pool.cs.meta     | 11 +++
 Assets/Scripts/ObjectPooling/PoolObject.cs    | 22 +++++
 .../Scripts/ObjectPooling/PoolObject.cs.meta  | 11 +++
 6 files changed, 149 insertions(+)
 create mode 100644 Assets/Scripts/ObjectPooling/IPoolActions.cs
 create mode 100644 Assets/Scripts/ObjectPooling/IPoolActions.cs.meta
 create mode 100644 Assets/Scripts/ObjectPooling/Pool.cs
 create mode 100644 Assets/Scripts/ObjectPooling/Pool.cs.meta
 create mode 100644 Assets/Scripts/ObjectPooling/PoolObject.cs
 create mode 100644 Assets/Scripts/ObjectPooling/PoolObject.cs.meta

diff --git a/Assets/Scripts/ObjectPooling/IPoolActions.cs b/Assets/Scripts/ObjectPooling/IPoolActions.cs
new file mode 100644
index 0000000..846d211
--- /dev/null
+++ b/Assets/Scripts/ObjectPooling/IPoolActions.cs
@@ -0,0 +1,10 @@
+﻿namespace UnityHelper.Pooling
+{
+    /// <summary>
+    /// Interface to remind you to add the return to pool function
+    /// </summary>
+    public interface IPoolActions
+    {
+        void ReturnObjectToPool();
+    }
+}
diff --git a/Assets/Scripts/ObjectPooling/IPoolActions.cs.meta b/Assets/Scripts/ObjectPooling/IPoolActions.cs.meta
new file mode 100644
index 0000000..50206f2
--- /dev/null
+++ b/Assets/Scripts/ObjectPooling/IPoolActions.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 3e21db0c60302f24b927bcadd6fc6112
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Scripts/ObjectPooling/Pool.cs b/Assets/Scripts/ObjectPooling/Pool.cs
new file mode 100644
index 0000000..817ca60
--- /dev/null
+++ b/Assets/Scripts/ObjectPooling/Pool.cs
@@ -0,0 +1,84 @@
+﻿using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEditor;
+using UnityEngine;
+
+namespace UnityHelper.Pooling
+{
+    [Serializable]
+    public class Pool
+    {
+        /// <summary>
+        /// The objects that you want to add to the pool
+        /// </summary>
+        public List<PoolObject> objects_to_pool = new List<PoolObject>();
+
+        /// <summary>
+        /// All the objects that were added to the pool
+        /// </summary>
+        private List<GameObject> pool_objects = new List<GameObject>();
+
+        /// <summary>
+        /// Initializes the pool in your scene
+        /// </summary>
+        public void Start_Pool()
+        {
+            var pool = new GameObject("Pool");
+
+            foreach (var item in objects_to_pool)
+            {
+                var sub_parent = new GameObject(item.obj_to_pool.name);
+
+                for (int i = 0; i < item.amount_to_pool; i++)
+                {
+                    var _obj = UnityEngine.Object.Instantiate(item.obj_to_pool);
+                    _obj.transform.SetParent(sub_parent.transform, false);
+                    _obj.SetActive(false);
+                    pool_objects.Add(_obj);
+                }
+
+                sub_parent.transform.SetParent(pool.transform, false);
+            }
+        }
+
+        /// <summary>
+        /// Gets the item from the pool and spawns it at the required position
+        /// </summary>
+        /// <param name="type">The type of script in the pool that you need to spawn</param>
+        /// <param name="position">The position where you want the item to spawn</param>
+        /// <returns>A component of the needed type</returns>
+        public Component Spawn_Item(Type type, Vector3 position, Quaternion direction)
+        {
+            foreach (GameObject item in pool_objects)
+            {
+                Component comp = null;
+                item.TryGetComponent(type, out comp);
+
+                if (comp == null)
+                    continue;
+
+                if (item.activeSelf)
+                    continue;
+
+                item.transform.position = position;
+                item.transform.rotation = direction;
+                item.SetActive(true);
+                return comp;
+
+            }
+
+            return null;
+        }
+
+        /// <summary>
+        /// Brings the item back to the pool
+        /// </summary>
+        /// <param name="item">The item to bring back to the pool</param>
+        public void Return_Item(GameObject item)
+        {
+            item.SetActive(false);
+            item.transform.position = Vector3.zero;
+        }
+    }
+}
\ No newline at end of file
diff --git a/Assets/Scripts/ObjectPooling/Pool.cs.meta b/Assets/Scripts/ObjectPooling/Pool.cs.meta
new file mode 100644
index 0000000..ac6cbc2
--- /dev/null
+++ b/Assets/Scripts/ObjectPooling/Pool.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: fa158cddf415eb0449d203dbd26ec022
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Scripts/ObjectPooling/PoolObject.cs b/Assets/Scripts/ObjectPooling/PoolObject.cs
new file mode 100644
index 0000000..fa2cdf6
--- /dev/null
+++ b/Assets/Scripts/ObjectPooling/PoolObject.cs
@@ -0,0 +1,22 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using UnityEngine;
+
+namespace UnityHelper.Pooling
+{
+    [Serializable]
+    public class PoolObject
+    {
+        /// <summary>
+        /// The gameobject that you want to spawn
+        /// </summary>
+        public GameObject obj_to_pool;
+        /// <summary>
+        /// The amount of objects that you want to spawn
+        /// </summary>
+        public int amount_to_pool;
+    }
+}
\ No newline at end of file
diff --git a/Assets/Scripts/ObjectPooling/PoolObject.cs.meta b/Assets/Scripts/ObjectPooling/PoolObject.cs.meta
new file mode 100644
index 0000000..a654e61
--- /dev/null
+++ b/Assets/Scripts/ObjectPooling/PoolObject.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: b201e930468ede54195a559247b03027
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

From 13215a516fd00dc70b72c8d6e2cac41270eda80f Mon Sep 17 00:00:00 2001
From: LesserKnownThings <ursu.marius.laurentiu@gmail.com>
Date: Fri, 11 Sep 2020 13:32:22 -0400
Subject: [PATCH 2/4] Adding Additional Files

---
 .../ObjectPooling/Images/Initialization.PNG      | Bin 0 -> 5171 bytes
 .../ObjectPooling/Images/PoolObjectLook.PNG      | Bin 0 -> 7692 bytes
 .../Scripts/ObjectPooling/Images/SpawnObject.PNG | Bin 0 -> 5356 bytes
 Assets/Scripts/ObjectPooling/README.md           |   4 ++++
 Assets/Scripts/ObjectPooling/README.md.meta      |   7 +++++++
 5 files changed, 11 insertions(+)
 create mode 100644 Assets/Scripts/ObjectPooling/Images/Initialization.PNG
 create mode 100644 Assets/Scripts/ObjectPooling/Images/PoolObjectLook.PNG
 create mode 100644 Assets/Scripts/ObjectPooling/Images/SpawnObject.PNG
 create mode 100644 Assets/Scripts/ObjectPooling/README.md
 create mode 100644 Assets/Scripts/ObjectPooling/README.md.meta

diff --git a/Assets/Scripts/ObjectPooling/Images/Initialization.PNG b/Assets/Scripts/ObjectPooling/Images/Initialization.PNG
new file mode 100644
index 0000000000000000000000000000000000000000..62f31dc57010e447e9546bac0e7f39191299d94b
GIT binary patch
diff --git a/Assets/Scripts/ObjectPooling/Images/PoolObjectLook.PNG b/Assets/Scripts/ObjectPooling/Images/PoolObjectLook.PNG
new file mode 100644
index 0000000000000000000000000000000000000000..57ed2008b5b7b8d81545ee32142d25c497de90a4
GIT binary patch
diff --git a/Assets/Scripts/ObjectPooling/Images/SpawnObject.PNG b/Assets/Scripts/ObjectPooling/Images/SpawnObject.PNG
new file mode 100644
index 0000000000000000000000000000000000000000..8641e8d9351cd4c0f077327729068257b922c5ec
GIT binary patch
diff --git a/Assets/Scripts/ObjectPooling/README.md b/Assets/Scripts/ObjectPooling/README.md
new file mode 100644
index 0000000..9b848b0
--- /dev/null
+++ b/Assets/Scripts/ObjectPooling/README.md
@@ -0,0 +1,4 @@
+<h2> Object pooling helper class </h2>
+<br>
+<a> These scripts help you set up an object pooling system in your game. It's pretty simple to use, let me show you how </a>
+![alt text]("Images/Initialization.png);
\ No newline at end of file
diff --git a/Assets/Scripts/ObjectPooling/README.md.meta b/Assets/Scripts/ObjectPooling/README.md.meta
new file mode 100644
index 0000000..a7a1a90
--- /dev/null
+++ b/Assets/Scripts/ObjectPooling/README.md.meta
@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: 39978d1555c9ede47adb909257a1bb01
+TextScriptImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

From 0000f663c7ae09e746824f8d74c6d0c8a600bd94 Mon Sep 17 00:00:00 2001
From: LesserKnownThings <ursu.marius.laurentiu@gmail.com>
Date: Fri, 11 Sep 2020 14:11:33 -0400
Subject: [PATCH 3/4] Created readme

---
 .../ObjectPooling/Images/PoolObject.PNG       | Bin 0 -> 12614 bytes
 Assets/Scripts/ObjectPooling/README.md        |  39 ++++++++++++++++--
 2 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 Assets/Scripts/ObjectPooling/Images/PoolObject.PNG

diff --git a/Assets/Scripts/ObjectPooling/Images/PoolObject.PNG b/Assets/Scripts/ObjectPooling/Images/PoolObject.PNG
new file mode 100644
index 0000000000000000000000000000000000000000..f5302875f031ffdee6ab16668b0287b3548c3081
GIT binary patch
diff --git a/Assets/Scripts/ObjectPooling/README.md b/Assets/Scripts/ObjectPooling/README.md
index 9b848b0..fb3ca95 100644
--- a/Assets/Scripts/ObjectPooling/README.md
+++ b/Assets/Scripts/ObjectPooling/README.md
@@ -1,4 +1,37 @@
-<h2> Object pooling helper class </h2>
+<h1> Object pooling helper class </h1>
 <br>
-<a> These scripts help you set up an object pooling system in your game. It's pretty simple to use, let me show you how </a>
-![alt text]("Images/Initialization.png);
\ No newline at end of file
+<h3> These scripts help you set up an object pooling system in your game. It's pretty simple to use, let me show you how: </h3>
+
+<br>
+
+
+<h3> 1. Initializing the pool </h3>
+<a> As you can see in the image bellow, you can initialize the pool by creating a new pool and then calling it in the start. </a>
+
+![Init Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/Initialization.PNG)
+
+ You'll also have to add the namespace that the pool class is in **using UnityHelper.Pooling;**
+ 
+ <h3> 2. Before you start the game </h3>
+ You will have to add the objects that you want the pool to instantiate when it starts. As you can see in the image bellow, the pool object looks like this in the editor:
+ 
+ ![Look Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/PoolObjectLook.PNG)
+ 
+ The pool contains a List of objects that you want to instantiate in the scene. The objects are of type **PoolObject** which you can see here:
+ 
+  ![List Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/PoolObject.PNG)
+  
+  The **obj_to_pool** is the gameobject that you want to instantiate and the **amount_to_pool** is the amount of objects that you want to instantiate.
+  
+  <h3> 3. Spawning the objects in the scene </h3>
+  Spawning an object is very easy, the **Pool** class comes with a function called **Spawn_Item(Type type, Vector3 position, Quaternion direction)** it returns a **Component** so you can get direct reference to the object. Here's how you do it:
+  
+  ![Spawn Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/SpawnObject.PNG)
+  
+  In my example I have a custom Object created by me called **CubeObj** it doesn't do much, but it helps with the example. So when you spawn an item in the scene you have to give the function the following parameters:
+  
+  >**Type** the typeof object that the list needs to look for and spawn it in the scene
+  >**Position** the position where the item should spawn
+  >**Rotation** the rotation of the item
+  
+   One more thing to remember. Pool has a functio to bring back the objects to the pool, but it requires the object as a parameter. That's why I made an interface called **IPoolActions** that comes with **ReturnObjectToPool** void and will help you return the items to the pool. It's pretty easy, to return an item you simply have to **gameobject.SetActive(false)** and also reset its position **transform.position = Vector3.zero**. 
\ No newline at end of file

From 566ae0cf702e755241274fe092e244e6501c7d5b Mon Sep 17 00:00:00 2001
From: LesserKnownThings <ursu.marius.laurentiu@gmail.com>
Date: Fri, 11 Sep 2020 14:13:25 -0400
Subject: [PATCH 4/4] Update README.md

---
 Assets/Scripts/ObjectPooling/README.md | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Assets/Scripts/ObjectPooling/README.md b/Assets/Scripts/ObjectPooling/README.md
index fb3ca95..695b61d 100644
--- a/Assets/Scripts/ObjectPooling/README.md
+++ b/Assets/Scripts/ObjectPooling/README.md
@@ -31,7 +31,9 @@
   In my example I have a custom Object created by me called **CubeObj** it doesn't do much, but it helps with the example. So when you spawn an item in the scene you have to give the function the following parameters:
   
   >**Type** the typeof object that the list needs to look for and spawn it in the scene
+  
   >**Position** the position where the item should spawn
+  
   >**Rotation** the rotation of the item
   
-   One more thing to remember. Pool has a functio to bring back the objects to the pool, but it requires the object as a parameter. That's why I made an interface called **IPoolActions** that comes with **ReturnObjectToPool** void and will help you return the items to the pool. It's pretty easy, to return an item you simply have to **gameobject.SetActive(false)** and also reset its position **transform.position = Vector3.zero**. 
\ No newline at end of file
+   One more thing to remember. Pool has a function to bring back the objects to the pool, but it requires the object as a parameter. That's why I made an interface called **IPoolActions** that comes with **ReturnObjectToPool** void and will help you return the items to the pool. It's pretty easy, to return an item you simply have to **gameobject.SetActive(false)** and also reset its position **transform.position = Vector3.zero**. 
