ãUnityãJsonUtilityã§Jsonã¸ã·ãªã¢ã©ã¤ãºãããã¼ã¿ããã¼ã«ã«ã«ä¿åãã
Unity 2019.2.0f1 Personal(2019å¹´8æ)
ä»åã¯Jsonããã¹ãããã¼ã«ã«ã«ä¿åãã¦ããã¾ãã
JsonUtilityã§ã¯ã©ã¹ãJsonããã¹ãã«å¤æããã¼ã«ã«ã«ä¿åãããã
éã«èªã¿åºãã¦ä½¿ç¨ããããã¾ãã
â»iOSãAndroidã§ã¯ãªãUnity Editorç°å¢ã§ã®ä¿åã§ãã
Saveæ©è½
ã¾ãã¯ãã¼ã¿ã®ä¿åãSaveæ©è½ãä½æãã¦ããã¾ãã
使ããããstaticã¯ã©ã¹ã§ä½æãã¦ããã¾ãã
using System; using System.IO; using System.Collections; using System.Collections.Generic; using UnityEngine; public static class SaveDataManager { const string SAVE_FILE = "save_data.json"; const string DATA_DIR = "Assets/StreamingAssets/data/"; static string saveDataPath; public static SaveData saveData; public static void Init() { saveDataPath = Path.Combine(DATA_DIR + SAVE_FILE); } public static void Save() { if (!Directory.Exists(DATA_DIR)) { Directory.CreateDirectory(DATA_DIR); } var json = JsonUtility.ToJson(saveData); StreamWriter writer = new StreamWriter(saveDataPath, false); writer.WriteLine(json); writer.Flush(); writer.Close(); } [Serializable] public class SaveData { [SerializeField] private string name; public string Name { get { return name; } private set { name = value; } } [SerializeField] private int level; public int Level { get { return level; } private set { level = value; } } [SerializeField] private long money; public long Money { get { return money; } private set { money = value; } } public void SetName(string name) { Name = name; } public void SetLevel(int level) { Level = level; } public void SetMoney(long money) { Money = money; } public void Init() { Name = "åç¡ã"; Level = 1; Money = 500; } } }
SaveDataManager.cs
Initã¡ã½ããã®Path.Combineã¯ããã¼ã¿ä¿åå
ã®Pathã¨ãã¡ã¤ã«åãçµåãã¦ããã¾ãã
saveDataPathã®ä¸èº«ã¯"Assets/StreamingAssets/data/save_data.json"ã¨ãªãã¾ãã
ä¸çªä¸ã«ããSaveDataã¯ã©ã¹ãJsonã«ã·ãªã¢ã©ã¤ãºããä¿åããã¾ãã
[SerializeField]ã®ã¤ããå¤æ°name, level, moneyãJsonã¨ãªãã
ããããã£Name, Level, Moneyã¯Jsonã«ã¯ãªãã¾ããã
ããããã£ã®setã¢ã¯ã»ãµã¯privateã¨ãªã£ã¦ãããã
ãã¼ã¿è¨å®ã«ã¯Setç³»ã®ã¡ã½ãããçµç±ããå¿
è¦ãããã¾ãã
æå¾ã«Saveã¡ã½ããã§ãã
DATA_DIRï¼"Assets/StreamingAssets/data/"ï¼ã®ãã£ã¬ã¯ããªãåå¨ããªãå ´åãCreateDirectoryã§ä½æãã¦ããã¾ãã
JsonUtility.ToJsonã§ãã¡ã³ãå¤æ°ã«ããSaveDataã¯ã©ã¹ã®ãã¼ã¿ãJsonã«å¤æãã¾ãã
ãã¼ã¿ãç¨æãããã®ã§ä¿åãã¦ããã¾ãã
StreamWriterãªãã¸ã§ã¯ãã¯ããã¹ãã®å
容ãä¿åããæã«ä½¿ããã¾ãã
第ä¸å¼æ°ã«ä¿åå
ã®Pathãããã¹ãåã¾ã§å
¥ãã¦æå®ãã¾ãã
第äºå¼æ°ï¼boolï¼ã¯appendï¼ä¸æ¸ãï¼ãããã§ãtrueã ã¨è¿½è¨ãfalseã ã¨ä¸æ¸ãã¨ãªãã¾ãã
ã»ã¼ããããã³ã«æ¬¡ã®è¡ã¸è¿½è¨ããã¦ãã£ã¦ãä»æ¹ããªãã®ã§ãfalseã«ãã¾ãã
ç¶ãã¦StreamWriterã®WriteLineã¡ã½ããã§å®éã«ãã¼ã¿ã®æ¸ãåºããè¡ãã¾ãã
WriteLineã®æç¹ã§ã¯ãã¼ã¿ããããã¡ã«ç§»ããã ãã§ããããé 次ãã¡ã¤ã«ã«æ¸ãåºãã¦ããã¾ãã
StreamWriterã®Flushã¡ã½ãããå®è¡ãããã¨ã§ããããã¡ã«æ®ã£ã¦ãããã¼ã¿ãå
¨ã¦æ¸ãåºãã¾ãã
æå¾ã«Closeã¡ã½ããã§StreamWriterãéãã¾ãã
Streamç³»ã®ãªãã¸ã§ã¯ãã¯ã¡ããã¨Closeããªãã¨ãã¡ã¤ã«ãæ£å¸¸ã«ä¿åãããªãã£ããããã®ã§æ³¨æã
Loadæ©è½
ç¶ãã¦ä¿åãããã¼ã¿ãå¼ã³åºãLoadæ©è½ã§ãã
using System; using System.IO; using System.Collections; using System.Collections.Generic; using UnityEngine; public static class SaveDataManager { const string SAVE_FILE = "save_data.json"; const string DATA_DIR = "Assets/StreamingAssets/data/"; static string saveDataPath; public static SaveData saveData; public static void Init() { saveDataPath = Path.Combine(DATA_DIR + SAVE_FILE); // ***** éå§ ***** Load(); // ***** çµäº ***** } // ***** éå§ ***** public static void Load() { if (File.Exists(saveDataPath)) { FileStream stream = File.Open(saveDataPath, FileMode.Open); StreamReader reader = new StreamReader(stream); var json = reader.ReadToEnd(); reader.Close(); stream.Close(); saveData = JsonUtility.FromJson<SaveData>(json); } else { saveData = new SaveData(); saveData.Init(); Save(); } } // ***** çµäº ***** public static void Save() { if (!Directory.Exists(DATA_DIR)) { Directory.CreateDirectory(DATA_DIR); } var json = JsonUtility.ToJson(saveData); StreamWriter writer = new StreamWriter(saveDataPath, false); writer.WriteLine(json); writer.Flush(); writer.Close(); } [Serializable] public class SaveData { [SerializeField] private string name; public string Name { get { return name; } private set { name = value; } } [SerializeField] private int level; public int Level { get { return level; } private set { level = value; } } [SerializeField] private long money; public long Money { get { return money; } private set { money = value; } } public void SetName(string name) { Name = name; } public void SetLevel(int level) { Level = level; } public void SetMoney(long money) { Money = money; } public void Init() { Name = "åç¡ã"; Level = 1; Money = 500; } } }
SaveDataManager.cs
Loadã¡ã½ããã®æåã§ã¯saveDataPathã«ãã¡ã¤ã«ãåå¨ãããå¤å®ãã¦ãã¾ãã
åå¨ãã¦ããããã¡ã¤ã«ãèªã¿åºããã
åå¨ãã¦ããªããã°SaveDataã¯ã©ã¹ãnewãåæå¤ãè¨å®ããå
ã»ã©ã®Saveã¡ã½ããã§ä¿åãã¾ãã
FileStreamã¯ãã¡ã¤ã«ãèªã¿è¾¼ãéã«ä½¿ããªãã¸ã§ã¯ãã§ãã
FileStreamã¯File.Openã¡ã½ããã§ä½æãã¾ãã
èªã¿è¾¼ã¿ãã¡ã¤ã«ã®Pathã第ä¸å¼æ°ã«ãFileModeã第äºå¼æ°ã«ã¨ãã¾ãã
FileMode.Openã¯æ¢åã®ãã¡ã¤ã«ãéãéã«æå®ãã¾ããä»ã«ã¯ãã¡ã¤ã«ãæ°è¦ä½æãã¦éãFileMode.Createãªã©ããã¾ãã
ç¶ãã¦ä½æããFileStreamãå¼æ°ã«ã¨ãStreamReaderãªãã¸ã§ã¯ããä½æãã
StreamReaderã®ReadToEndã¡ã½ããã«ããsaveDataPathã«ãããã¡ã¤ã«ã®ãã¼ã¿ãå
¨ã¦èªã¿åºãã¾ãã
使ç¨ããã¹ããªã¼ã ç³»ãªãã¸ã§ã¯ãï¼FileStreamã¨StreamReaderï¼ã¯Closeãã¾ãã
æå¾ã«JsonUtility.FromJsonã«ãããJsonããã¹ãããã¯ã©ã¹ã¸å¤æãã¾ãã
ã¸ã§ããªã¯ã¹
Initã¡ã½ããã«ä»åä½æããLoadã¡ã½ããã追è¨ãã¦ããã¾ãã
å®è¡
å¥ãªãã¸ã§ã¯ããä½æãSaveDataManagerã®Initã¡ã½ãããå®è¡ãããã¨ã§ã
å®éã«save_data.jsonãã¡ã¤ã«ãä½æããã¼ã¿ã®ã»ã¼ã/ãã¼ããè¡ãã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Manager : MonoBehaviour { void Start() { SaveDataManager.Init(); CheckStatus(); SaveDataManager.saveData.SetName("ããã"); SaveDataManager.saveData.SetLevel(100); SaveDataManager.saveData.SetMoney(10000000); SaveDataManager.Save(); CheckStatus(); } void CheckStatus() { Debug.Log("name:" + SaveDataManager.saveData.Name + " level:" + SaveDataManager.saveData.Level + " money:" + SaveDataManager.saveData.Money); } }
Manager.cs
SaveDataManagerã®Initã¡ã½ãããå®è¡ããã¨Loadã¡ã½ãããå®è¡ããã¾ãã
ã¾ã save_data.jsonãã¡ã¤ã«ã¯åå¨ããªãã®ã§ã
SaveDataã¯ã©ã¹ãçæãSaveã¡ã½ãããå®è¡ãã¾ãã
Saveã¡ã½ãããéãã¦ãã£ã¬ã¯ããªãä½æããããã¼ã«ã«ã«ä¿åããã¾ãã
ã»ã¼ããã¼ã¿ã®æ´æ°ã¯SaveDataManager.saveDataï¼SaveDataã¯ã©ã¹ï¼ãéãã¦è¡ãã¾ãã
å
·ä½çã«ã¯SaveDataã¯ã©ã¹ã®Setç³»ã¡ã½ããã§ãã
æ´æ°ããå¤ã®ã»ãããå®äºã£ãããSaveã¡ã½ãããå®è¡ãããã¼ã¿ãä¿åãã¾ãã
ã²ã¼ã ã¢ã¼ããçµäºãã¦ãjsonãã¡ã¤ã«ãæ®ã£ã¦ãããã¨ã確èªã§ãã¾ãã
以ä¸ãJsonUtilityã使ã£ããã¼ã¿ã®ä¿åã«ã¤ãã¦ã§ããã
ãããã¨ããããã¾ããã