GetPoolableObject









How do I use Get Poolable Object
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: Enemy.cs Copy
99     public void Die()
100     {
101         GameObject bonus = pools.GetPoolableObject("bonus");
102         if (bonus != null)
103         {
104             bonus.SetActive(true);
105             bonus.GetComponent().Activation(transform.position);
106         }
107         if (enemyType == 4)
108             uiForScore.Win();
109         else
110             uiForScore.IncreaseScore(20);
111
112         transform.gameObject.SetActive(false);
113     }
File name: Enemy.cs Copy
136     void Enemy_01_ShootCircle()
137     {
138         int bulletSpeed = 2;
139
140         GameObject bullet = pools.GetPoolableObject("e_bullet");
141
142         Vector3 direction = new Vector3(0,0,-1);
143
144         //
145         for (int i = 0; i < 32; i++)
146         {
147             bullet = pools.GetPoolableObject("e_bullet");
148             if (bullet == null)
149             {
150                 continue;
151             }
152
153             bullet.SetActive(true);
154             bullet.GetComponent().ShootMe(transform.position, direction.normalized, bulletSpeed);
155
156             //creat circle of bullets
157             if (direction.x < 1 && direction.z == -1)
158                 direction.x += 0.25f;
159             else if (direction.z < 1 && direction.x == 1)
160                 direction.z += 0.25f;
161             else if (direction.x > -1 && direction.z == 1)
162                 direction.x -= 0.25f;
163             else if (direction.x == -1 && direction.z > -1)
164                 direction.z -= 0.25f;
165         }
166     }
File name: Enemy.cs Copy
169     IEnumerator ShootingRoutine_02()
170     {
171         while (true)
172         {
173             int bulletSpeed = 7;
174
175             yield return new WaitForSeconds(2f);
176
177             GameObject bullet = pools.GetPoolableObject("e_bullet");
178
179             if (bullet != null)
180             {
181                 bullet.SetActive(true);
182                 bullet.GetComponent().ShootMe(transform.position, new Vector3(0, 0, -1).normalized, bulletSpeed);
183             }
184         }
185     }
File name: Enemy.cs Copy
188     IEnumerator ShootingRoutine_03()
189     {
190         GameObject tempPlayer = GameObject.FindGameObjectWithTag("Player");
191
192         //if player didnt destroyed
193         if (tempPlayer != null)
194             player = tempPlayer.GetComponent();
195         else
196             player = transform;
197
198         GameObject bullet = null;
199         int bulletSpeed = 9;
200
201         while (true)
202         {
203             yield return new WaitForSeconds(1.5f);
204
205             bullet = pools.GetPoolableObject("e_bullet");
206
207             if (bullet != null)
208             {
209                 bullet.SetActive(true);
210                 bullet.GetComponent().ShootMe(transform.position, (player.position - transform.position).normalized, bulletSpeed);
211             }
212         }
213     }
File name: Enemy.cs Copy
216     IEnumerator ShootingRoutine_04()
217     {
218         yield return new WaitForSeconds(1f);
219         int bulletSpeed = 4;
220
221         GameObject bullet = pools.GetPoolableObject("e_bullet");
222
223         Vector3 direction = new Vector3(0,0,0);
224         int rCount = 1;
225
226         while (true)
227         {
228             if (rCount == 1)
229             {
230                 StartCoroutine("ShootingRoutine_01");
231                 rCount = 0;
232             }
233             else
234                 rCount++;
235
236             yield return new WaitForSeconds(0.3f);
237             for (int i = 0; i < 64; i++)
238             {
239                 bullet = pools.GetPoolableObject("e_bullet");
240
241                 if (bullet == null)
242                 {
243                     continue;
244                 }
245
246                 yield return new WaitForSeconds(0.1f);
247
248                 direction = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f));
249
250                 bullet.SetActive(true);
251                 bullet.GetComponent().ShootMe(transform.position, direction.normalized, bulletSpeed);
252
253             }
254         }
255     }
File name: Pools.cs Copy
50     public GameObject GetPoolableObject(string objName)
51     {
52         GameObject returningObj = null;
53         GameObject[] allObjects = null;
54
55         switch (objName)
56         {
57             case "p_bullet": allObjects = bulletsPlayer; break;
58             case "e_bullet": allObjects = bulletsEnemy; break;
59             case "enemy_01": allObjects = enemies_01; break;
60             case "enemy_02": allObjects = enemies_02; break;
61             case "enemy_03": allObjects = enemies_03; break;
62             case "bonus": allObjects = bonuses; break;
63         }
64
65         for (int i = 0; i < allObjects.Length; i++)
66         {
67             if (!allObjects[i].activeInHierarchy)
68             {
69                 returningObj = allObjects[i];
70             }
71         }
72
73         return returningObj;
74     }
File name: SpawnManager.cs Copy
42     IEnumerator SpawningRoutine()
43     {
44         yield return new WaitForSeconds(3);
45
46         while (!UIgame.scoreAchived)
47         {
48             GameObject enemy = null;
49
50             //FIRST STAGE OF SPAWNING
51             //CHOOSE ENEMY FOR SPAWN
52             int r = rand.Randomization_1(0, 10, 80, 99);
53             if (r == 0)
54             {
55                 enemy = pools.GetPoolableObject("enemy_01");
56             }
57             else if (r == 1)
58             {
59                 enemy = pools.GetPoolableObject("enemy_02");
60             }
61             else
62             {
63                 enemy = pools.GetPoolableObject("enemy_03");
64             }
65
66             //SECOND STAGE OF SPAWNING
67             //SPAWN CHOSEN ENEMY IN RANDOM POINT ON TOP SCREEN SIDE
68             r = Random.Range(0, pointsPositions.Length);
69
70             if (enemy != null)
71             {
72                 enemy.SetActive(true);
73                 enemy.GetComponent().Activation(new Vector3(pointsPositions[r], 2, strtSpawn.position.z),
74                     new Vector3(0,0,-1));
75             }
76
77             int spawnDelay;
78             if (SystemScr.difficultyIsHard)
79                 spawnDelay = 1;
80             else
81                 spawnDelay = Random.Range(3,5);
82
83             yield return new WaitForSeconds(spawnDelay);
84         }
85
86         //BOSS ACTIVATION
87         pools.DeleteEnemiesAndBullets();
88         CameraMotor.speedScreen = 0;
89         Camera.main.GetComponent().city_1.gameObject.SetActive(false);//remove city1
90         Camera.main.GetComponent().city_2.gameObject.SetActive(false);//remove city2
91         Camera.main.backgroundColor = Color.black;
92         GameObject.FindObjectOfType().BossMusicOn();
93
94         yield return new WaitForSeconds(6);
95
96         pools.AcivateBoss();
97     }
File name: PlayerControl.cs Copy
49     void ShootOrPauseControls()
50     {
51         if (!SystemScr.paused)
52         {
53             if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
54             {
55                 GameObject p_bullet = pools.GetPoolableObject("p_bullet");
56
57                 if (p_bullet != null && maxBulletsOnScreen > 0)
58                 {
59                     sounds.PlaySoundsPlayer(0);
60                     p_bullet.SetActive(true);
61                     p_bullet.GetComponent().ShootMe(transform.position);
62                 }
63             }
64         }
65         if (Input.GetKeyDown(KeyCode.Escape))
66         {
67             ui.Pause_btn();
68         }
69     }

GetPoolableObject 162 lượt xem

Gõ tìm kiếm nhanh...