如何使用 GVR 矿池和 PoolManager
How to use GVR pools and PoolManager
我想使用几个 GameObject pools so I need the pool manager 但我不知道如何使用它,而且我找不到任何文档。
我应该如何定义一个使用名为 "enemy" 的游戏对象的新池?
我应该从 GameObjectPool 继承一个新的具体 class 吗?
池管理器具有设置为唤醒的静态实例。您应该将 ObjectPoolManager 脚本附加到场景中的某些游戏对象。
然后你可以为不同的游戏对象创建池:
public class GameManager : MonoBehaviour {
public GameObject enemyPrefab;
public GameObject itemPrefab;
public GameObject characterPrefab;
const int enemyCount = 100;
const int itemCount = 100;
private GameObjectPool EnemyPool {
get {
GameObjectPool pool = ObjectPoolManager.Instance.GetPool<GameObjectPool> (enemyPrefab.name);
if (pool == null) {
pool = new GameObjectPool(enemyPrefab, enemyCount);
ObjectPoolManager.Instance.AddPool(enemyPrefab.name, pool);
}
return pool;
}
}
private GameObjectPool ItemPool {
get {
GameObjectPool pool = ObjectPoolManager.Instance.GetPool<GameObjectPool> (itemPrefab.name);
if (pool == null) {
pool = new GameObjectPool(itemPrefab, itemCount);
ObjectPoolManager.Instance.AddPool(itemPrefab.name, pool);
}
return pool;
}
}
// Use this for initialization
void Start () {
StartCoroutine(SpawnEnemy (1f));
}
IEnumerator SpawnEnemy(float t) {
for (int i = 0; i < enemyCount; ++i) {
GameObject newEnemy = EnemyPool.Borrow ();
newEnemy.transform.position = Random.insideUnitSphere;
yield return new WaitForSeconds (t);
}
}
}
我想使用几个 GameObject pools so I need the pool manager 但我不知道如何使用它,而且我找不到任何文档。
我应该如何定义一个使用名为 "enemy" 的游戏对象的新池?
我应该从 GameObjectPool 继承一个新的具体 class 吗?
池管理器具有设置为唤醒的静态实例。您应该将 ObjectPoolManager 脚本附加到场景中的某些游戏对象。
然后你可以为不同的游戏对象创建池:
public class GameManager : MonoBehaviour {
public GameObject enemyPrefab;
public GameObject itemPrefab;
public GameObject characterPrefab;
const int enemyCount = 100;
const int itemCount = 100;
private GameObjectPool EnemyPool {
get {
GameObjectPool pool = ObjectPoolManager.Instance.GetPool<GameObjectPool> (enemyPrefab.name);
if (pool == null) {
pool = new GameObjectPool(enemyPrefab, enemyCount);
ObjectPoolManager.Instance.AddPool(enemyPrefab.name, pool);
}
return pool;
}
}
private GameObjectPool ItemPool {
get {
GameObjectPool pool = ObjectPoolManager.Instance.GetPool<GameObjectPool> (itemPrefab.name);
if (pool == null) {
pool = new GameObjectPool(itemPrefab, itemCount);
ObjectPoolManager.Instance.AddPool(itemPrefab.name, pool);
}
return pool;
}
}
// Use this for initialization
void Start () {
StartCoroutine(SpawnEnemy (1f));
}
IEnumerator SpawnEnemy(float t) {
for (int i = 0; i < enemyCount; ++i) {
GameObject newEnemy = EnemyPool.Borrow ();
newEnemy.transform.position = Random.insideUnitSphere;
yield return new WaitForSeconds (t);
}
}
}