Last active
February 15, 2016 11:28
-
-
Save sugi-cho/896bfcea6394050b1bd9 to your computer and use it in GitHub Desktop.
クラスのデータをJsonにして、StreamingAssetフォルダに保存するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.IO; | |
using System.Collections; | |
public class SaveData : MonoBehaviour | |
{ | |
public int data1; | |
public float data2; | |
public string data3; | |
public Vector3[] points; | |
// Use this for initialization | |
void Start () | |
{ | |
LoadJsonFile (this); | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
if (Input.GetMouseButtonDown (0)) | |
SaveJsonFile (this); | |
} | |
public static void LoadJsonFile (Object overwriteTarget, string filePath = "appData.json") | |
{ | |
var path = Path.Combine (Application.streamingAssetsPath, filePath); | |
if (File.Exists (path)) | |
JsonUtility.FromJsonOverwrite (File.ReadAllText (path), overwriteTarget); | |
} | |
public static void SaveJsonFile<T> (T obj, string filePath = "appData.json") | |
{ | |
var json = JsonUtility.ToJson (obj); | |
var path = Path.Combine (Application.streamingAssetsPath, filePath); | |
var dPath = Path.GetDirectoryName (path); | |
if (!Directory.Exists (dPath)) | |
Directory.CreateDirectory (dPath); | |
using (var writer = new StreamWriter (path)) | |
writer.Write (json); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment