/// <summary> /// Adds the specified count of prefab instances to the pool. /// </summary> public void Allocate(int count) { if (HardLimit && Pool.Count + count > Limit) { count = Limit - Pool.Count; } for (int n = 0; n < count; n++) { var go = Object.Instantiate(Prefab.gameObject, m_root.transform, true); //go.name = go.name + n.ToString(); PoolObject poolObj = go.GetComponent <PoolObject>(); poolObj.PrefabName = Prefab.PrefabName; poolObj.Init(); Pool.Push(go); } }
public Rigidbody SpawnFromPool(string tag, Vector3 position, Quaternion rotation) { if (!poolDictionary.ContainsKey(tag)) { Debug.LogWarning("Pool with tag " + tag + " doesn't exsist"); } PoolObject objectToSpawn = poolDictionary[tag].Dequeue(); if (tag == "Human") { ObjectData objectData = humanSetting[Random.Range(0, humanSetting.Count)]; objectToSpawn.Init(objectData); } objectToSpawn.gameObject.SetActive(true); objectToSpawn.transform.position = position; //objectToSpawn.Rigidbody.velocity = transform.forward * objectToSpawn.Speed; poolDictionary[tag].Enqueue(objectToSpawn); return(objectToSpawn.Rigidbody); }
/// <summary> /// Spawns the object with desired position and rotation. Note that the Awake() and Start() will be called only once. OnDisable() and OnEnable() will be called insted, /// since the method calls SetActive(true) on spawned Object; /// </summary> /// <param name="position">Position where the object shoul be spawned</param> /// <param name="rotation">Rotation of the spawned object</param> /// <returns></returns> public T Spawn <T>(Vector3 position, Quaternion rotation) where T : Component { GameObject go; // object to return if (m_inactiveCount == 0) { if (m_objectsHolder == null) { go = GameObject.Instantiate(m_poolObject, position, rotation); } else { go = GameObject.Instantiate(m_poolObject, position, rotation, m_objectsHolder); } PoolObject po = go.GetComponent <PoolObject>(); if (po == null) { po = go.AddComponent <PoolObject>(); } po.Init(this); } else { go = m_poolStack[--m_inactiveCount]; } // set position and rotation go.transform.position = position; go.transform.rotation = rotation; go.gameObject.SetActive(true); // increment active count m_activeCount++; return(go.GetComponent <T>()); }
private void SetTemplateToRoot() { Prefab.Init(); Prefab.transform.SetParent(m_root.transform); }