public void Initialize(MapData newData) { tiles.Clear(); tileNum.x = newData.tileX; tileNum.y = newData.tileY; topPos = Vector3.zero; topPos += tileSpacingX * tileNum.x * -0.5f; topPos += tileSpacingY * tileNum.y * -0.5f; for (int y = 0; y < tileNum.y; ++y) { for (int x = 0; x < tileNum.x; ++x) { var i = newData.datas[tileNum.x * y + x]; #if UNITY_EDITOR GameObject obj = null; if (Application.isEditor) { obj = Instantiate(tilePrefabs[i]); } else { obj = ObjectPoolManager.instance.Pop(tilePrefabs[i]); } #else GameObject obj = ObjectPoolManager.instance.Pop(tilePrefabs[i]); #endif if (obj == null) { Debug.LogError("obj == null."); } // set game object properties obj.name = "tile_" + (tileNum.x * y + x) + "_" + y + "," + x; // name obj.transform.SetParent(transform); // parent obj.transform.position = topPos + (tileSpacingX * x) + (tileSpacingY * y); // position var tile = obj.GetComponent <Tile>(); tile.position.x = x; tile.position.y = y; var sr = obj.GetComponent <SpriteRenderer>(); sr.sortingOrder = tileNum.x * y + x; tiles.Add(tile); } } // add spawn point for (int i = 0; i < newData.SpawnPoints.Length; ++i) { #if UNITY_EDITOR GameObject obj = null; if (Application.isEditor) { obj = Instantiate(spawnPointPrefab); } else { obj = ObjectPoolManager.instance.Pop(spawnPointPrefab); } #else GameObject obj = ObjectPoolManager.instance.Pop(spawnPointPrefab); #endif if (obj == null) { Debug.LogError("obj == null."); } // set game object properties obj.name = "SpawnPoint_" + i + "_team" + newData.SpawnPoints[i].teamIndex; // name obj.transform.SetParent(transform); // parent obj.transform.position = topPos + (tileSpacingX * newData.SpawnPoints[i].tileX) + (tileSpacingY * newData.SpawnPoints[i].tileY); // position var spawnPoint = obj.GetComponent <SpawnPoint>(); spawnPoint.position.x = newData.SpawnPoints[i].tileX; spawnPoint.position.y = newData.SpawnPoints[i].tileY; spawnPoint.SetTeamIndex(newData.SpawnPoints[i].teamIndex); spawnPoints.Add(spawnPoint); } }
// initialize public void Initialize(string dataPath) { MapData data = DataController.LoadJson <MapData>(dataPath); Initialize(data); }