Last active
August 29, 2015 14:26
-
-
Save makochang/4232fc399e7c7014f8af to your computer and use it in GitHub Desktop.
Unityでテキストファイルを読み書きする静的クラス
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.Collections; | |
using System.IO; | |
using System; | |
/// <summary> | |
/// Unityでテキストファイルを読み書きする. | |
/// このファイルは静的クラスのため「Standard Assets」または「Plugins」フォルダーに配置する. | |
/// このファイルは静的クラスのため特定のオブジェクトにアタッチしてはいけない. | |
/// </summary> | |
public static class FileIO | |
{ | |
/// <summary> | |
/// ファイルを読み込む.ファイルが存在しない場合は作成する. | |
/// </summary> | |
private static void FileRead (string dataPath) | |
{ | |
string readData = ""; | |
Debug.Log ("ロードします"); | |
try { | |
using (StreamReader sr = new StreamReader (dataPath)) { | |
readData = sr.ReadToEnd (); | |
} | |
Debug.Log (dataPath + "から" + readData + "を読み込みました."); | |
} catch (Exception e) { | |
Debug.Log (e.Message); | |
} | |
} | |
/// <summary> | |
/// ファイルを書き込む. | |
/// </summary> | |
/// <param name="writeData">書き込むデータ.</param> | |
private static void FileWrite (string dataPath, string writeData) | |
{ | |
Debug.Log ("セーブします."); | |
try { | |
using (StreamWriter sw = new StreamWriter (dataPath)) { | |
sw.Write (writeData); | |
} | |
Debug.Log (dataPath + "に" + writeData + "を書き込みました."); | |
} catch (Exception e) { | |
Debug.Log (e.Message); | |
} | |
} | |
/// <summary> | |
/// ファイルを初期化する. | |
/// </summary> | |
public static void InitSaveData (string dataPath) | |
{ | |
Debug.Log ("セーブデータを初期化します."); | |
FileWrite (dataPath, ""); | |
} | |
/// <summary> | |
/// 指定したファイルを読み込む. | |
/// </summary> | |
/// <param name="dataPath">ファイルパス.</param> | |
public static void Read (string dataPath) | |
{ | |
FileRead (dataPath); | |
} | |
/// <summary> | |
/// 指定したファイルに書き込む. | |
/// </summary> | |
/// <param name="dataPath">ファイルパス.</param> | |
/// <param name="writeData">書き込むデータ(文字列).</param> | |
public static void Write (string dataPath, string writeData) | |
{ | |
FileWrite (dataPath, writeData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment