public class LevelLoader { private static LevelHolder levelHolder = new LevelHolder(); public static IEnumerator LoadLevel(int number, LevelScript prefabProvider, float fieldWidth, float fieldHeight, System.Action callback) { // TODO use gravity and surface LevelHolder.LevelData level = levelHolder.GetLevel(number); WaitForFixedUpdate wait = new WaitForFixedUpdate() ; // TODO Coordinates ? WTF int nPixelWidth = 720 / level.levelWidth; // Affects how much items on the screen. Should be a variable int nPixelHeight = 390 / level.levelHeight; // Affects how much items on the screen. Should be a variable int bubblesCount = 0; for (int x = 0; x < level.levelWidth; x++) { for (int y = 0; y < level.levelHeight; y++) { Debug.Log ("Spawning object"); int item = level.items[((y * 2) * 20) + (x * 2)]; int itemData = level.items[(((y * 2) * 20) + (x * 2)) + 1]; float xCoordinate = 0 + ((x * nPixelWidth) + (nPixelWidth / 2)) * 2; // 0 is offset float yCoordinate = 50 + ((y * nPixelHeight) + (nPixelHeight/ 2)) * 2; // 0 is offset Vector3 coordinate = new Vector3 (xCoordinate / 100f, yCoordinate / 100f); if (item < 1000 && item > 0) { GameObject bubble = (GameObject)GameObject.Instantiate (prefabProvider.bubblePrefab, coordinate, Quaternion.identity); bubble.GetComponent ().SetValue (item); bubble.gameObject.name = "Bubble"; bubblesCount++; } else if (item == 1000) { GameObject ball = (GameObject)GameObject.Instantiate (prefabProvider.ballPrefab, coordinate, Quaternion.identity); ball.GetComponent ().level = prefabProvider; } else if (item == 2000) { GameObject.Instantiate (prefabProvider.spedUpPrefab, coordinate, Quaternion.identity); } else if (item == 2010) { GameObject.Instantiate (prefabProvider.spedDownPrefab, coordinate, Quaternion.identity); } else if (item == 3000) { GameObject death = (GameObject)GameObject.Instantiate (prefabProvider.deathPrefab, coordinate, Quaternion.identity); death.gameObject.name = "Death"; } else if (item >= 4100 && item < 4200) { GameObject bubble = (GameObject)GameObject.Instantiate (prefabProvider.bubblePrefab, coordinate, Quaternion.identity); bubble.GetComponent ().SetValue (item); bubble.gameObject.name = "Bubble"; bubble.GetComponent().SetMovement(3, Mathf.Round(((float)itemData) * 15f)); bubblesCount++; } else if (item >= 4200 && item < 4300) { GameObject bubble = (GameObject)GameObject.Instantiate (prefabProvider.bubblePrefab, coordinate, Quaternion.identity); bubble.GetComponent ().SetValue (item); bubble.gameObject.name = "Bubble"; bubble.GetComponent().SetMovement(3, Mathf.Round(((float)itemData) * 15f)); bubblesCount++; } else if (item >= 4300 && item < 4400) { GameObject bubble = (GameObject)GameObject.Instantiate (prefabProvider.bubblePrefab, coordinate, Quaternion.identity); bubble.GetComponent ().SetValue (item); bubble.gameObject.name = "Bubble"; bubble.GetComponent().SetMovement(3, itemData); bubblesCount++; } else if (item == 5000) { GameObject.Instantiate (prefabProvider.pillsPrefab, coordinate, Quaternion.identity); } else if (item == 6000) { GameObject.Instantiate (prefabProvider.bombPrefab, coordinate, Quaternion.identity); } else if (item == 7100) { GameObject.Instantiate (prefabProvider.plusPrefab, coordinate, Quaternion.identity); } else if (item == 7200) { GameObject.Instantiate (prefabProvider.minusPrefab, coordinate, Quaternion.identity); } else if (item == 7300) { GameObject.Instantiate (prefabProvider.popPrefab, coordinate, Quaternion.identity); } else if (item == 7400) { GameObject.Instantiate (prefabProvider.popPrefab, coordinate, Quaternion.identity); } Debug.Log ("Waitinf before spawning"); yield return wait; } } callback (bubblesCount); } }