2016年12月25日日曜日

Unityでファイルの読み書きについて2

json形式のデータを読み書きする。
暗号化はしていないので生データを扱いたくない場合は暗号化する必要があります。
暗号化については今後書くかもです。
今回はsimplejsonではなくLitJsonの方を使用しました。

以下のurlから
http://lbv.github.io/litjson/

Latest release: vX.X.X · source code · dll
のdllのクリックしてダウンロードします。

ダウンロードしたdllをUnityのprojectへドラッグアンドロップするだけで使えるようになります。

デフォルトのjsonデータ、以下のデータをAssets\Dataフォルダに入れました。UTF-8 BOMなし で作成しました。
test_data.json
{
  "id": 0,
  "name":"名前"
}

jsonを変換するクラスの作成
TestData.cs
public class TestData
{
    public int id;
    public string name;
}

ファイルの管理をするクラス
機能としてはWriteTextメソッドを実行するとStreamingAssetへ保存する。
ReadFileTextメソッドを実行すると、StreamingAssetにファイルがある場合そこから読み込み、ない場合 _folda_path のフォルダ(今回はDataフォルダのパスにする)から読み込みます。
FIleManager.cs
using UnityEngine;
using System.IO;
using System.Text;

public class FIleManager
{

// StreamingAssetのパス
#if UNITY_EDITOR
    public static string path = Application.dataPath + "/StreamingAssets";
#elif UNITY_ANDROID
    // パス(Android)
    public static string path = "jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
    // パス(iOS)
    public static string path = path = Application.dataPath + "/Raw";
#else
    public static string path = Application.dataPath + "/StreamingAssets";
#endif

    // テキスト読み込み関数
    public static string ReadFileText (string _folda_path, string _file_name)
    {
        string result = string.Empty;

        FileInfo file;

        // ファイルがある場合
        if (File.Exists(path + _file_name))
        {
            file = new FileInfo(path + _file_name);
        }
        // ファイルがない場合
        else
        {
            file = new FileInfo(Application.dataPath + _folda_path + _file_name);
        }
        
        using (StreamReader sr = new StreamReader(file.OpenRead(), Encoding.UTF8))
        {
            result = sr.ReadToEnd();
        }

        return result;
    }

    // jsonファイルを上書きする
    public static void WriteText (string _file_name, string _contents)
    {
        StreamWriter sw;
        FileInfo fi;
        
        fi = new FileInfo(path +  _file_name);

        // ファイルがある場合
        if (File.Exists(path + _file_name))
        {
            // 削除
            fi.Delete();
        }

        sw = fi.AppendText();        // StreamWriter を作成
        sw.WriteLine(_contents);     // 書き込み
        sw.Flush();                  // バッファ書き込み
        sw.Close();                  // 閉じる
    }
}
以下、test_data.jsonのデータを読み込んで
id = 1, name = "名前1"
でStreamingAssetsへ保存する。
JsonTest.cs
using UnityEngine;

public class JsonTest : MonoBehaviour
{
    // フォルダ
    const string folda_path = "/Data";

    // ファイル名
    const string file_name = "/test_data.json";


    void Start ()
    {
        // jsonの文字列の取得
        string json_text = FIleManager.ReadFileText(folda_path, file_name);

        // TestDataクラスに変換して読み込む
        TestData data = LitJson.JsonMapper.ToObject(json_text);

        data.id = 1;
        data.name = "名前1";

        // dataを文字列として取得する。
        json_text = LitJson.JsonMapper.ToJson(data);

        // jsonテキストファイルを書き込む
        FIleManager.WriteText(file_name, json_text);
    }
}


Unityでファイルの読み書き3 に続く

0 件のコメント :

コメントを投稿

【早い者勝ち!】 あなたのお名前、残ってる?

シャドウバースにPC版が誕生