// put an idle object into the object pool public void PutIdleObject(GameObject o) { //Initialize(); if (o != null) { //Debug.Log("Putting object " + ObjectManager.GetObjectClass(o.tag).ToString() + " to the pool"); o.SetActive(false); // for the sake of productivity ObjectManager.ObjectClass objectClass = ObjectManager.GetObjectClass(o.tag); int objectIndex = objectPool.IndexOf(o); if (objectIndex < 0) { //Debug.Log("Adding object to pool"); objectIndex = objectPool.Count; objectPool.Add(o); } else { if (idleObjectsDictionary[objectClass].Contains(objectIndex)) { //Debug.Log("Object is already registered in the pool"); return; } } //Debug.Log("Object is in the pool, registering"); idleObjectsDictionary[objectClass].Push(objectIndex); } }
// utility method which creates an inactive pool object of the specified class public GameObject Prebuild(ObjectManager.ObjectClass objectClass) { //Initialize(); //Debug.Log("Building pool object " + objectClass.ToString()); ObjectManager.ObjectPrefabDeclaration decl = GlobalManager.MObject.GetObjectDecl(objectClass); if (decl != null) { //Debug.Log("Found object declaration: " + GlobalManager.Instance.MLanguage.Entry(decl.descriptionId)); GameObject o = Instantiate(decl.prefab); // prebuilt objects are immediately set inactive o.SetActive(false); return(o); } //Debug.Log("Could not find declaration for " + objectClass.ToString()); return(null); }
// get an idle object of a class if available public GameObject GetIdleObject(ObjectManager.ObjectClass objectClass, bool forceCreate = true) { //Initialize(); if (idleObjectsDictionary[objectClass].Count > 0) { //Debug.Log("Popping idle object " + objectClass.ToString()); GameObject o = objectPool[idleObjectsDictionary[objectClass].Pop()]; // an object from the pool should be always set active o.SetActive(true); return(o); } else if (forceCreate) { //Debug.Log("Creating idle object " + objectClass.ToString()); GameObject o = Prebuild(objectClass); if (o != null) { o.SetActive(true); return(o); } } return(null); }