//Display a Random Prefab and position it at initialPosition return the x coordinate for the right bound private BackGroundElement DisplayRandomPrefab(Vector3 initialPosition, BackGroundInsertionDirection direction) { int randomIndex = 0; if (prefabsPool.Count == 0) { Debug.LogError("You need at least one prefab in the elements list."); } else if (prefabsPool.Count > 1) { randomIndex = Mathf.Abs(Random.Range(0, prefabsPool.Count)); } //The element that will be rendered BackGroundElement renderedObject = prefabsPool[randomIndex].GetAvailableObject(); renderedObject.DisplayAtPosition(initialPosition, direction); if (direction == BackGroundInsertionDirection.Right) { elementsOnScreen.Add(renderedObject); } else { elementsOnScreen.Insert(0, renderedObject); } return(renderedObject); }
public BackGroundElement GetAvailableObject() { BackGroundElement backgroundElem = null; foreach (BackGroundElement obj in gameObjectPool) { if (obj.gameObject.activeSelf == false) { backgroundElem = obj; break; } } //if we don't have any available gameObject we need to instansiate a new one if (backgroundElem == null) { GameObject gameObject = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject; backgroundElem = new BackGroundElement(gameObject); gameObjectPool.Add(backgroundElem); } return(backgroundElem); }
public BackGroundElement GetAvailableObject() { BackGroundElement backgroundElem =null; foreach (BackGroundElement obj in gameObjectPool) { if(obj.gameObject.activeSelf==false){ backgroundElem = obj; break; } } //if we don't have any available gameObject we need to instansiate a new one if(backgroundElem==null){ GameObject gameObject = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject; backgroundElem = new BackGroundElement(gameObject); gameObjectPool.Add(backgroundElem); } return backgroundElem; }