forked from hellogavin/UnityUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalGameObjectPool.cs
125 lines (101 loc) · 2.72 KB
/
GlobalGameObjectPool.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using X;
public class GlobalGameObjectPool : SingletonMonoBehaviour<GlobalGameObjectPool>
{
GameObjectPool Pool;
static GlobalGameObjectPool GetInstance()
{
if (Instance == null)
{
var go = new GameObject("GlobalGameObjectPool");
go.AddComponent<GlobalGameObjectPool>();
var goRecycleBin = new GameObject("RecycleBin");
goRecycleBin.SetActive(false);
var transformRecycleBin = goRecycleBin.transform;
transformRecycleBin.SetParent(go.transform, false);
Instance.Pool = new GameObjectPool(transformRecycleBin);
}
return Instance;
}
public static Transform GetTransform()
{
var pool = GetInstance();
return pool.transform;
}
public static GameObject Instantiate(GameObject original, Vector3 position, Quaternion rotation)
{
var pool = GetInstance();
return pool.Pool.Instantiate(original, position, rotation);
}
public static GameObject Instantiate(GameObject original)
{
var pool = GetInstance();
return pool.Pool.Instantiate(original);
}
public static void Destroy(GameObject go)
{
if (Instance == null)
{
GameObject.Destroy(go);
return;
}
Instance.Pool.Destroy(go);
}
public static void Destroy(GameObject go, float delay)
{
if (Instance == null)
{
GameObject.Destroy(go, delay);
return;
}
Instance.StartCoroutine(Instance.DestroyCoroutine(go, delay));
}
IEnumerator DestroyCoroutine(GameObject go, float time)
{
yield return new WaitForSeconds(time);
if (go != null)
Pool.Destroy(go);
}
}
public class PoolObjectBehaviour : MonoBehaviour
{
private bool PoolObjectInitFlag;
private bool PoolDestoryFlag;
protected virtual void OnDestroy()
{
if (!PoolDestoryFlag)
OnPoolDestroy();
}
protected virtual void OnSpawned()
{
PoolObjectInitFlag = false;
PoolDestoryFlag = false;
}
protected virtual void OnDespawned()
{
PoolDestoryFlag = true;
OnPoolDestroy();
}
protected virtual void Update()
{
if (!PoolObjectInitFlag)
{
OnPoolStart();
PoolObjectInitFlag = true;
}
OnPoolUpdate();
}
// 推荐
public virtual void OnPoolStart()
{
}
public virtual void OnPoolUpdate()
{
}
public virtual void OnPoolDestroy()
{
}
}