LoginSignup
0
0

More than 3 years have passed since last update.

Unity 2D #2 jsonファイルの読み書き

Last updated at Posted at 2020-11-11

unityでjsonファイルをデータベースのように使いたいと思います。

データの保存について

データの保存はクラス内容の保存という形で実行されます。例として、以下のクラスを考えます。

[System.Serializable]
public class CharacterTable{
    public List<CharacterIndividual> characters;
}

[System.Serializable]
public class CircleIndividual {
    [SerializeField] public int Id;
    [SerializeField] public string Name;
    [SerializeField] public int Width;
    [SerializeField] public int Height;
    [SerializeField]public int[] Points;
    [SerializeField]public string[] ComboType;
    [SerializeField]public float[] ComboEffect;

    public CircleIndividual(int id, string name, int width, int height, int[] points, string[] comboType, float[] comboEffect){
        this.Id = id;
        this.Name = name;
        this.Width = width;
        this.Height = height;
        this.Points = points;
        this.ComboType = comboType;
        this.ComboEffect = comboEffect;
    }
}

ここで、CharacterTableはCharacterIndividualをまとめているだけで不要に感じるかもしれませんが、データの保存のためには、CharacterTableのようにクラスを包含する一回り大きいクラスが必要となります。
また、詳しい説明は避けますが、[System.Serializable]も上のように書いてある必要があります。(publicである必要もある)
そのため、試したい方は注意してください。

string characters_path = "/Characters.json";
int Characterindex = 0;
CharacterTable characterTable = new CharacterTable();

characterTable.characters = new List<CharacterIndividual>();
AddCharacter("火の魔法使い", 100, 50, new string[] {"atkup1"}, new float[] {50});
AddCharacter("水の魔法使い", 120, 40, new string[] {"defup1", "atkup1"}, new float[] {20, 20});
AddCharacter("雷の魔法使い", 80, 60, new string[] {"defup1"}, new float[] {30});

string characters_jsonToSave = JsonUtility.ToJson(characterTable);

// Save
StreamWriter characters_writer;
characters_writer = new StreamWriter(Application.dataPath + characters_path, false);
characters_writer.Write(characters_jsonToSave);
characters_writer.Flush();
characters_writer.Close();
void AddCharacter(string name, float hp, float attack, string[] skillType, float[] skillEffect){
    // id, name, hp, attack, skillType, skillEffect
    CharacterIndividual chara = new 
    CharacterIndividual(Characterindex, name, hp, attack, skillType, skillEffect);
    characterTable.characters.Add(chara);
    Characterindex++;
}

上のコードのようにデータを作成し、内容を保存しました。
Application.dataPathはunityでのパスを表すもので、何も設定することなく使用可能です。上のコードだとAssetフォルダ直下にjsonファイルが保存されるはずです。
完成したjsonファイルは以下の通りです.

{"characters":[
  {"Id":0,"Name":"火の魔法使い","HP":100.0,"Attack":50.0,"SkillType":["atkup1"],"SkillEffect":[50.0]},
  {"Id":1,"Name":"水の魔法使い","HP":120.0,"Attack":40.0,"SkillType":["defup1","atkup1"],"SkillEffect":[20.0,20.0]},
  {"Id":2,"Name":"雷の魔法使い","HP":80.0,"Attack":60.0,"SkillType":["defup1"],"SkillEffect":[30.0]}
]}

データの読み込みについて

保存したjsonファイルの中身は以下のように読み込みます。

StreamReader reader = new StreamReader(Application.dataPath+DataHandring.characters_path);
string JsonText = reader.ReadToEnd();
reader.Close();
CharacterTable ReadCharacterTable = JsonUtility.FromJson<CharacterTable>(JsonText);

// ReadCharacterTable.characters[0].Name
// 火の魔法使い

また、c#スクリプトの上部に

using System;
using UnityEngine;

を記載するのを忘れないようにしてください。(他にも必要だったかもしれない)

0
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
0
0