//Create level base on level data void CreateLevel(LevelData levelData) { GameObject pinkBall = Instantiate(pinkBallPrefab, levelData.pinkBallPosition, Quaternion.identity); GameObject blueBall = Instantiate(blueBallPrefab, levelData.blueBallPosition, Quaternion.identity); pinkBallRigid = pinkBall.GetComponent <Rigidbody2D>(); pinkBallRigid.isKinematic = true; blueBallRigid = blueBall.GetComponent <Rigidbody2D>(); blueBallRigid.isKinematic = true; foreach (ObstacleData o in levelData.listObstacleData) { foreach (GameObject a in obstacleManager.obstacles) { if (a.name.Equals(o.id)) { GameObject obstacle = Instantiate(a, o.position, o.rotation) as GameObject; obstacle.transform.localScale = o.scale; ConveyorController cv = obstacle.GetComponent <ConveyorController>(); if (cv != null) { cv.rotateSpeed = o.rotatingSpeed; cv.rotateDirection = o.rotateDirection; } break; } } } }
string GetLevelData(int currentLevel) { BallController[] ballsController = FindObjectsOfType <BallController>(); GameObject blueBall = null; GameObject pinkBall = null; foreach (BallController o in ballsController) { if (o.gameObject.name.Split('(')[0].Trim().Equals("PinkBall")) { pinkBall = o.gameObject; } else { blueBall = o.gameObject; } } GameObject[] allObstacle = GameObject.FindGameObjectsWithTag("Obstacle"); GameObject hint = GameObject.FindGameObjectWithTag("Hint"); List <ObstacleData> listObstacleData = new List <ObstacleData>(); for (int i = 0; i < allObstacle.Length; i++) { ObstacleData obstacleData = new ObstacleData(); string id = allObstacle[i].name.Split('(')[0].Trim(); obstacleData.id = id; obstacleData.position = allObstacle[i].gameObject.transform.position; obstacleData.rotation = allObstacle[i].gameObject.transform.rotation; obstacleData.scale = allObstacle[i].gameObject.transform.localScale; ConveyorController carouselController = allObstacle[i].GetComponent <ConveyorController>(); if (carouselController != null) { obstacleData.rotateDirection = carouselController.rotateDirection; obstacleData.rotatingSpeed = carouselController.rotateSpeed; } listObstacleData.Add(obstacleData); } HintData hintData = new HintData(); hintData.position = hint.transform.position; hintData.rotation = hint.transform.rotation; hintData.scale = hint.transform.localScale; LevelData levelData = new LevelData(); levelData.levelNumber = currentLevel; levelData.blueBallPosition = blueBall.transform.position; levelData.pinkBallPosition = pinkBall.transform.position; levelData.listObstacleData = listObstacleData; levelData.hintData = hintData; string data = JsonUtility.ToJson(levelData); return(data); }
public static void LoadObject(int levelNumber, string[] data, GameObject pinkBallPrefab, GameObject blueBallPrefab, GameObject hintPrefab, ObstacleManager obstacleManager) { for (int i = 0; i < data.Length; i++) { LevelData levelData = JsonUtility.FromJson <LevelData>(data[i]); if (levelData.levelNumber == levelNumber) { //Create balls Instantiate(pinkBallPrefab, levelData.pinkBallPosition, Quaternion.identity); Instantiate(blueBallPrefab, levelData.blueBallPosition, Quaternion.identity); //On level editor mode if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Equals("LevelEditor")) { //Create hint GameObject hint = (GameObject)Instantiate(hintPrefab, levelData.hintData.position, levelData.hintData.rotation); hint.transform.localScale = levelData.hintData.scale; hint.GetComponent <SpriteRenderer>().sprite = Resources.Load <Sprite>("Hints/hint" + levelData.levelNumber.ToString()); } foreach (ObstacleData o in levelData.listObstacleData) { foreach (GameObject a in obstacleManager.obstacles) { if (o.id.Trim().Equals(a.name.Trim())) { //Create obstacles GameObject obstacle = Instantiate(a, o.position, o.rotation) as GameObject; obstacle.transform.localScale = o.scale; ConveyorController carouselController = obstacle.GetComponent <ConveyorController>(); if (carouselController != null) { carouselController.rotateDirection = o.rotateDirection; carouselController.rotateSpeed = o.rotatingSpeed; } break; } } } break; } } }