{"id":25986,"date":"2025-05-06T06:23:46","date_gmt":"2025-05-05T21:23:46","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25986"},"modified":"2025-05-06T06:23:47","modified_gmt":"2025-05-05T21:23:47","slug":"unity-c-savemanager-cs","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25986","title":{"rendered":"Unity C# SaveManager.cs"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\nusing System.IO;\npublic class SaveManager\n{\n    const string location = \"Assets\/Resources\/Data\";\n\n    public static void SaveData&lt;T>(string fileName, T template)\n    {\n        if (!Directory.Exists(location))\n        {\n            Directory.CreateDirectory(location);\n        }\n        string data = JsonUtility.ToJson(template);\n        string path = Path.Combine(location, fileName);\n        using (FileStream stream = new FileStream(path, FileMode.Create))\n        {\n            using (StreamWriter writer = new StreamWriter(stream))\n            {\n                writer.Write(data);\n            }\n            stream.Close();\n        }\n        Debug.Log(\"game saved\");\n    }\n\n    public static T LoadData&lt;T>(string fileName) where T : class\n    {\n        T load = null;\n        string read = \"\";\n        string path = Path.Combine(location, fileName);\n        if(!File.Exists(path))\n        {\n            return null;\n        }\n\n        using (FileStream stream=new FileStream(path,FileMode.Open))\n        {\n            using (StreamReader reader=new StreamReader(stream))\n            {\n                read = reader.ReadToEnd();\n            }\n            stream.Close();\n        }\n        load = JsonUtility.FromJson&lt;T>(read);\n            return load;\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[33],"tags":[35,3],"class_list":["post-25986","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-programming"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25986"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25986\/revisions"}],"predecessor-version":[{"id":25987,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25986\/revisions\/25987"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25986"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}