Unity 对象池
源码 (https://github.com/Unity-Technologies/game-programming-patterns-demo/tree/main/Assets/7%20Object%20Pool)
https://unity.com/how-to
https://unity.com/how-to/
https://blog.unity.com/use-object-pooling-boost-performance-c-scripts-unity https://unity.com/how-to/develop-modular-flexible-codebase-state-programming-pattern https://unity.com/how-to/build-modular-codebase-mvc-and-mvp-programming-patterns
- 创建一个新脚本并命名为“Object Pool”
- 将脚本附加到您的游戏控制器。
- 打开脚本并在类定义中写入以下内容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static ObjectPool SharedInstance;
public List<GameObject> pooledObjects;
public GameObject objectToPool;
public int amoutToPool;
void Awake()
{
SharedInstance = this;
}
void Start()
{
pooledObjects = new List<GameObject>();
GameObject tmp;
for(int i = 0; i < amountToPool; i++)
{
tmp = Instantiate(objectToPool);
tmp.SetActive(false);
pooledObjects.Add(tmp);
}
}