如何使用GVR池和PoolManager

时间:2017-10-25 14:50:06

标签: c# unity3d google-cardboard google-vr

我想使用多个GameObject pools所以我需要游泳池manager,但我不明白如何使用它,我也找不到任何文档。

我应该如何定义一个使用名为"敌人"例如?

我是否应该从GameObjectPool继承一个新的具体类?

1 个答案:

答案 0 :(得分:0)

池管理器具有在Awake上设置的静态实例。您应该将ObjectPoolManager脚本附加到场景中的某个GameObject。

enter image description here

然后,您可以为不同的GameObjects创建池:

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);
    }
}


}