Pools









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

Featured Snippets


File name: GenerateGrounds.cs Copy
32  void Start () {
33
34   groundWidth = new float[theObjectPools.Length];
35
36   for(int i = 0; i < theObjectPools.Length; i++) {
37    groundWidth [i] = theObjectPools [i].pooledObject.GetComponent ().size.x;
38   }
39
40   minHeight = transform.position.y;
41   maxHeight = maxHeightPoint.position.y;
42
43   coinGenerator = FindObjectOfType ();
44   cratesGenerator = FindObjectOfType ();
45  }
File name: GenerateGrounds.cs Copy
48  void Update () {
49   if(transform.position.x < generatePoint.position.x) {
50
51    distance = Random.Range (distanceBetweenMin, distanceBetweenMax);
52
53    groundSelector = Random.Range (0, theObjectPools.Length);
54
55    heightChange = transform.position.y + Random.Range (maxHeightChange, -maxHeightChange);
56
57    if (heightChange > maxHeight) {
58     heightChange = maxHeight;
59    } else if (heightChange < minHeight) {
60     heightChange = minHeight;
61    }
62
63    transform.position = new Vector3 (transform.position.x + (groundWidth[groundSelector] / 2) + distance, heightChange, transform.position.z);
64
65    GameObject newPlatform = theObjectPools[groundSelector].GetPooledObject ();
66    newPlatform.transform.position = transform.position;
67    newPlatform.transform.rotation = transform.rotation;
68    newPlatform.SetActive (true);
69
70    if(Random.Range(0f, 100f) < randomCoins) {
71     coinGenerator.spawnCoins (new Vector3 (transform.position.x, transform.position.y + 3f, transform.position.z));
72    }
73
74    if(Random.Range(0f, 100f) < randomCrates) {
75     cratesGenerator.spawnCrates (new Vector3 (transform.position.x, transform.position.y + 1.3f, transform.position.z));
76    }
77
78    transform.position = new Vector3 (transform.position.x + (groundWidth[groundSelector] / 2) + distance, transform.position.y, transform.position.z);
79
80   }
81  }
File name: Enemy.cs Copy
22  void Awake ()
23     {
24         pools = GameObject.FindObjectOfType();
25         rb = GetComponent();
26         sounds = GameObject.FindObjectOfType();
27         uiForScore = GameObject.FindObjectOfType();
28  }
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: SpawnManager.cs Copy
11     void Awake()
12     {
13         pools = GameObject.FindObjectOfType();
14         rand = new Random();
15     }
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     }

Download file with original file name:Pools

Pools 383 lượt xem

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