1+ using System ;
2+ using System . Collections ;
3+ using System . Collections . Generic ;
4+ using JetBrains . Annotations ;
5+ using UnityEngine ;
6+ using UnityEngine . PlayerLoop ;
7+
8+ public class ObjectPooler : MonoBehaviour
9+ {
10+ public bool debug = false ;
11+ [ Header ( "GameObjects to be Pooled" ) ] public GameObjectToBePooled [ ] gameObjectsToBePooled ;
12+
13+ private List < GameObject > pooledGameObjects = new List < GameObject > ( ) ;
14+ private float timer ;
15+
16+ #region Singleton
17+
18+ //Singleton Instantiation
19+ public static ObjectPooler Instance { get ; private set ; }
20+
21+ private void Awake ( )
22+ {
23+ if ( Instance != null && Instance != this )
24+ Destroy ( this . gameObject ) ;
25+ else
26+ Instance = this ;
27+
28+ DontDestroyOnLoad ( this ) ;
29+ }
30+
31+ #endregion
32+
33+ void Start ( )
34+ {
35+ Initialize ( ) ;
36+ //StartCoroutine(Test());
37+ }
38+
39+ IEnumerator Test ( )
40+ {
41+ yield return new WaitForSeconds ( 0.5f ) ;
42+ print ( GetGameObject ( 0 ) . name ) ;
43+ yield return new WaitForSeconds ( 0.5f ) ;
44+ print ( GetGameObject ( 1 ) . name ) ;
45+
46+ yield return null ;
47+ }
48+ public void Initialize ( )
49+ {
50+ if ( debug ) { print ( "Pooling has started" ) ; timer = Time . realtimeSinceStartup ; }
51+
52+ for ( int i = 0 ; i < gameObjectsToBePooled . Length ; i ++ )
53+ {
54+ for ( int j = 0 ; j < gameObjectsToBePooled [ i ] . amountToBePooled ; j ++ )
55+ {
56+ GameObject go = Instantiate ( gameObjectsToBePooled [ i ] . gameObjectToBePooled ) ;
57+ go . SetActive ( false ) ;
58+ go . transform . SetParent ( transform ) ;
59+
60+ pooledGameObjects . Add ( go ) ;
61+ }
62+ }
63+
64+ if ( debug ) { print ( "Pooling has ended, " + pooledGameObjects . Count + " Pooled Objects" ) ; print ( "Generating Pool Took " + ( Time . realtimeSinceStartup - timer ) ) ; }
65+ }
66+
67+ public GameObject GetGameObject ( int position )
68+ {
69+ int startPosInList = 0 ;
70+ for ( int i = 0 ; i < position ; i ++ )
71+ {
72+ startPosInList += gameObjectsToBePooled [ i ] . amountToBePooled ;
73+ }
74+
75+ if ( debug ) print ( "Start Position in List is " + startPosInList ) ;
76+
77+ for ( int i = startPosInList ; i < startPosInList + gameObjectsToBePooled [ position ] . amountToBePooled ; i ++ )
78+ {
79+ if ( ! pooledGameObjects [ i ] . activeSelf ) return pooledGameObjects [ i ] ;
80+ }
81+
82+ if ( debug ) print ( "No Objects Ready in Pool " + gameObjectsToBePooled [ position ] . name ) ;
83+
84+ if ( gameObjectsToBePooled [ position ] . loadMoreIfNoneLeft )
85+ {
86+ if ( debug ) print ( "Added Object in Pool " + gameObjectsToBePooled [ position ] . name ) ;
87+ pooledGameObjects . Insert ( startPosInList + gameObjectsToBePooled [ position ] . amountToBePooled , gameObjectsToBePooled [ position ] . gameObjectToBePooled ) ;
88+ gameObjectsToBePooled [ position ] . amountToBePooled ++ ;
89+ return pooledGameObjects [ startPosInList + gameObjectsToBePooled [ position ] . amountToBePooled - 1 ] ;
90+ }
91+
92+ return null ;
93+ }
94+
95+ public void ResetPool ( )
96+ {
97+ foreach ( var objectInPool in pooledGameObjects )
98+ {
99+ if ( objectInPool . activeSelf )
100+ {
101+ SetObjectInPool ( objectInPool ) ;
102+ }
103+ }
104+ }
105+
106+ public void SetObjectInPool ( GameObject go )
107+ {
108+ if ( debug ) print ( go . name + " Set in Pool" ) ;
109+
110+ go . SetActive ( false ) ;
111+ go . transform . position = Vector3 . zero ;
112+ }
113+ }
114+
115+ [ Serializable ]
116+ public struct GameObjectToBePooled
117+ {
118+ [ Header ( "Object Info" ) ]
119+ public string name ;
120+ public int amountToBePooled ;
121+ public GameObject gameObjectToBePooled ;
122+
123+ [ Header ( "Settings" ) ]
124+ public bool lazyInstantiation ;
125+ public bool loadMoreIfNoneLeft ;
126+ }
0 commit comments