Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using System.IO;
- public class ObstacleController : MonoBehaviour
- {
- public float speed = 3.0f;
- public GameObject pipe;
- public GameObject rock;
- public GameObject win;
- public GameObject tree;
- public GameObject coin;
- public TextAsset JSON;
- public LevelController _levelController;
- [System.Serializable]
- public class Obstacle{
- public string name {get; set;}
- public float x {get; set;}
- public float y {get; set;}
- }
- [System.Serializable]
- public class ObstacleList{
- public List<Obstacle> Obstacle { get; set;}
- }
- void Start()
- {
- _levelController.CreateJsonList();
- JSON = _levelController.JsonList[_levelController.levelIndex];
- Debug.Log(JSON.name);
- ReadFromJson();
- }
- void Update()
- {
- Roller();
- }
- private void Roller()
- {
- transform.position += Vector3.left * speed * Time.deltaTime;
- }
- public void ReadFromJson()
- {
- //string jsonPath = $"{Application.dataPath}/JSONs/Values.json";
- //string json = File.ReadAllText(jsonPath);
- var obstacleList = JsonConvert.DeserializeObject<ObstacleList>(JSON.text);
- foreach (var item in obstacleList.Obstacle)
- {
- switch (item.name)
- {
- case "Win":
- Instantiate(win, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
- break;
- case "Coin":
- Instantiate(coin, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
- break;
- case "Rock":
- Instantiate(rock, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
- break;
- case "Tree":
- Instantiate(tree, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
- break;
- case "Pipe":
- Instantiate(pipe, new Vector3(item.x, item.y, 0), Quaternion.identity, gameObject.transform);
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment