NOTE/Unity
[Unity] 데이터 저장(펌)
by DevAthena
2017. 1. 18.
유니티 게임 저장방법에 대한 간단 정리
http://m.blog.naver.com/yoohee2018/220724426783
Json save / read 간단 정리
http://unitytutorial.tistory.com/20
using UnityEngine; using System.Collections; using LitJson; using System.IO; public class WriteJson : MonoBehaviour { public Character player = new Character (0, "Austin The Wizard", 1337, false, new int[] {7,4,8,21,12,1556666}); JsonData playerJson; // Use this for initialization void Start () { playerJson = JsonMapper.ToJson (player); Debug.Log (playerJson); File.WriteAllText (Application.dataPath + "/Player.json", playerJson.ToString ()); } // Update is called once per frame void Update () { } } public class Character{ public int id; public string name; public int health; public bool aggressive; public int[] stats; public Character(int id, string name, int health, bool aggressive, int[] stats) { this.id = id; this.name = name; this.health = health; this.aggressive = aggressive; this.stats = stats; } } | cs |
using UnityEngine; using System.Collections; using System.IO; using LitJson; public class ReadJson : MonoBehaviour { private string jsonString; private JsonData itemData; // Use this for initialization void Start() { jsonString = File.ReadAllText(Application.dataPath + "/Resources/Items.json"); itemData = JsonMapper.ToObject(jsonString); // Example //Debug.Log (Application.dataPath); //Debug.Log (jsonString); //Debug.Log (itemData["Weapons"][1]["name"].ToString()); //Debug.Log(GetItem("Light Rifle", "Weapons")["power"]); //Debug.Log(GetItem("rocket_pistol", "Weapons")["power"]); } JsonData GetItem(string name, string type) { for (int i = 0; i < itemData[type].Count; i++) { if (itemData[type][i]["name"].ToString() == name || itemData[type][i]["slug"].ToString() == name) return itemData[type][i]; } return null; } } | cs |