forked from nhathuy7996/ReUseScript_Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectPooling_base.cs
42 lines (35 loc) · 1002 Bytes
/
ObjectPooling_base.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 ObjectPooling_base<T,L> : MonoBehaviour where T : MonoBehaviour where L: MonoBehaviour
{
private static T instant = null;
public static T Instant{
get{
if(instant ==null){
T[] Ts = FindObjectsOfType<T>();
if(Ts.Length > 0)
instant = Ts[0];
}
return instant;
}
}
void Awake(){
Destroy(this);
}
[SerializeField]
GameObject Obj_prefab;
List<L> List_obj = new List<L>();
protected L Get_Obj(){
foreach(L G in List_obj){
if( G.gameObject.activeSelf){
continue;
}
return G;
}
L G2 = Instantiate(Obj_prefab,this.transform.position,Quaternion.identity,this.transform).GetComponent<L>();
List_obj.Add(G2);
G2.gameObject.SetActive(false);
return G2;
}
}