c# 对象池的简单书写

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//Object Pool ( http://unitypatterns.com/resource/objectpool/ ) is licensed under:
/* The MIT License (MIT)
Copyright (c) 2013 UnityPatterns
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in the
Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */


using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public sealed class ObjectPool : MonoBehaviour
{
static ObjectPool _instance;

Dictionary<Component, List<Component>> objectLookup = new Dictionary<Component, List<Component>>();
Dictionary<Component, Component> prefabLookup = new Dictionary<Component, Component>();

public static void Clear()
{

instance.objectLookup.Clear();
instance.prefabLookup.Clear();
}

public static void CreatePool<T>(T prefab) where T : Component
{
if(prefab==null)
return;
if (!instance.objectLookup.ContainsKey(prefab))
instance.objectLookup.Add(prefab, new List<Component>());
}

public static T Spawn<T>(T prefab, Vector3 position, Quaternion rotation) where T : Component
{
if(prefab==null)
return null;
if (instance.objectLookup.ContainsKey(prefab))
{
T obj = null;
var list = instance.objectLookup[prefab];
if (list.Count > 0)
{
while (obj == null && list.Count > 0)
{
obj = list[0] as T;
list.RemoveAt(0);
}
if (obj != null)
{
obj.transform.parent = null;
obj.transform.localPosition = position;
obj.transform.localRotation = rotation;
obj.gameObject.SetActive(true);
instance.prefabLookup.Add(obj, prefab);
obj.SendMessage("OnSpawned", null, SendMessageOptions.DontRequireReceiver);
return (T)obj;
}
}
obj = (T)Object.Instantiate(prefab, position, rotation);
obj.name = prefab.name;
instance.prefabLookup.Add(obj, prefab);
obj.SendMessage("OnSpawned", null, SendMessageOptions.DontRequireReceiver);
return (T)obj;
}
else {
T obj = null;
obj = (T)Object.Instantiate(prefab, position, rotation);
obj.name=prefab.name;
return (T)obj;
}
}
public static T Spawn<T>(T prefab, Vector3 position) where T : Component
{
return Spawn(prefab, position, Quaternion.identity);
}
public static T Spawn<T>(T prefab) where T : Component
{
return Spawn(prefab, Vector3.zero, Quaternion.identity);
}

public static void Recycle<T>(T obj) where T : Component
{
if (instance.prefabLookup.ContainsKey(obj))
{
instance.objectLookup[instance.prefabLookup[obj]].Add(obj);
instance.prefabLookup.Remove(obj);
obj.transform.parent = instance.transform;
obj.gameObject.SetActive(false);
}
else
Object.Destroy(obj.gameObject);
}

public static int Count<T>(T prefab) where T : Component
{
if (instance.objectLookup.ContainsKey(prefab))
return instance.objectLookup[prefab].Count;
else
return 0;
}

public static ObjectPool instance
{
get
{
if (_instance != null)
return _instance;
var obj = new GameObject("_ObjectPool");
obj.transform.localPosition = Vector3.zero;
_instance = obj.AddComponent<ObjectPool>();
return _instance;
}
}
}

public static class ObjectPoolExtensions
{
public static void CreatePool<T>(this T prefab) where T : Component
{
ObjectPool.CreatePool(prefab);
}

public static T Spawn<T>(this T prefab, Vector3 position, Quaternion rotation) where T : Component
{
return ObjectPool.Spawn(prefab, position, rotation);
}
public static T Spawn<T>(this T prefab, Vector3 position) where T : Component
{
return ObjectPool.Spawn(prefab, position, Quaternion.identity);
}
public static T Spawn<T>(this T prefab) where T : Component
{
return ObjectPool.Spawn(prefab, Vector3.zero, Quaternion.identity);
}

public static void Recycle<T>(this T obj) where T : Component
{
ObjectPool.Recycle(obj);
}

public static int Count<T>(T prefab) where T : Component
{
return ObjectPool.Count(prefab);
}
}