-
Notifications
You must be signed in to change notification settings - Fork 0
/
Objectpool.cs
42 lines (37 loc) · 999 Bytes
/
Objectpool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Objectpool : MonoBehaviour
{
// Start is called before the first frame update
public static Objectpool SharedInstance;
public List<GameObject> pooledobjects;
public GameObject ObjectTopool;
public int amountToPool;
private void Awake()
{
SharedInstance = this;
}
void Start()
{
pooledobjects = new List<GameObject>();
for(int i=0; i<amountToPool; i++){
GameObject obj = (GameObject)Instantiate(ObjectTopool);
obj.SetActive(false);
pooledobjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
for (int i=0;i<pooledobjects.Count; i++){
if(!pooledobjects[i].activeInHierarchy){
return pooledobjects[i];
}
}
return null;
}
// Update is called once per frame
void Update()
{
}
}