1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unityで二次元配列を苦し紛れにJSONファイルに書き込む方法

Posted at

Unityで二次元配列をJSONに入れたいと思ったことはありませんか今回はマップを例に二次元配列をJSONファイルに保存する方法を紹介します。

結論

classを2つに分けて保存する

using System;

[Serializable]
public class YValue
{
    public int[] xValue = new int[23];
}
[Serializable]
public class SavingData
{
    public YValue[] yValue = new YValue[10];
}

JSONファイルに取り込む

using System.IO
public void saveData_save(SavingData saver)
{
    StreamWriter writer;
    string jsonstr = JsonUtility.ToJson(saver);
    writer = new StreamWriter(Application.dataPath + "/save/savedataJO.json", false);
    writer.Write(jsonstr);
    writer.Flush();
    writer.Close();
}
public SavingData loadsaveData()
{
    string datastr = "";
    StreamReader reader;
    reader = new StreamReader(Application.dataPath+"/save/savedataJO.json");
    datastr = reader.ReadToEnd();
    reader.Close();
    
    //return JsonUtility.FromJson<StageDate>(datastr);
    try
    {
    return JsonUtility.FromJson<SavingData>(datastr);
    }
    catch (ArgumentException e)
    {
        Debug.LogError("Failed to deserialize JSON: " + e.Message);
        return null;
    }

この2点を押さえればOK

詳しいやり方/例

今回はマップ情報(二次元)をJSONで保存します。

()・・・今回のclassのこと

保存するためのclass(savingData)を作る

保存先(TrapController)で値を代入

Jsonの書き込み用class(SaveManager)で入れた値を書き込む

❶保存用classを作る

jsonは二次元配列を直接書くことができないなのでYValueクラスSavingDataクラスの2つに分けてSavingDataクラスでYValueの配列YValueクラスでintの配列を作ることで擬似的な二次元配列を作る

using System;

[Serializable]
public class YValue
{
    public int[] xValue = new int[23];
}
[Serializable]
public class SavingData
{
    public YValue[] yValue = new YValue[10];
}

❷保存先で値を代入する

(SavingData)クラスをインスタンスとして関数を

//save
public SaveMapping saveMapping = new SaveMapping();
public GameObject sM_trap;

void Start()
{
    // save Setting
    saveMapping.saveX = transform.position.x;
    saveMapping.saveY = transform.position.y;

    int changeX = (int)saveMapping.saveX;
    int changeY = (int)saveMapping.saveY;

    // 連番変換
    if(changeX < 0)
    {
        changeX = 11 + changeX;
        
    }else if(changeX >= 0)
    {
        changeX += 11;
    }
    if(changeY < 0)
    {
        changeY = (changeY * -1) + 4;
    }else if (changeY >= 0)
    {
        changeY = 4 - changeY;
    }

    saveMapping.saveCode = 3;
    sM_trap = GameObject.Find("SaveManager");
    saveMapping.sM = sM_trap.GetComponent<SaveManager>();
    saveMapping.sM.InputTo(changeY, changeX,saveMapping.saveCode);        
    //save End

}

❸Jsonに書きこむ

StreamWriter 変数[writer]を用意string型の変数に保存用のクラスをJsonUnity.ToJsonでJSON型変換する[writer]にStreamWriter(パス+ファイル名,false)インスタンスを入れる[writer]のStreamWriterインスタンスのWrite、Flushを使用してJSONの中身を書き込む書き終わったらwriterの書き込みを終了させる

using System.IO
//save activety
public void saveData_save(SavingData saver)
{
    StreamWriter writer;
    string jsonstr = JsonUtility.ToJson(saver);
    writer = new StreamWriter(Application.dataPath + "/save/savedataJO.json", false);
    writer.Write(jsonstr);
    writer.Flush();
    writer.Close();
}

以上です、ご覧いただきありがとうございました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?