ただファイルが保存できているか疑問だったので
書き込まれたファイルを読み込むスクリプトも作った
----------- WriteTxtScr.cs -------------
using UnityEngine;
using System.Collections;
using System.IO; //System.IO.FileInfo, System.IO.StreamReader, System.IO.StreamWriter//File.ReadAllLines
public class WriteTxtScr : MonoBehaviour
{
private string guitxt = "";
private string guitxt2 = "";
private string outputTextData = "WriteText.txt";
TextReader txtReader;
void Start()
{
guitxt = "ファイルの書き込み";
StartCoroutine("SaveText");
}
void OnGUI()
{
GUI.TextArea(new Rect(5, 155, 200, 60), guitxt);
if (GUI.Button(new Rect(300, 155, 100, 40), "書き込みテスト"))
{
StartCoroutine("LoadText2");
}
GUI.TextArea(new Rect(5, 200, 250, 100), guitxt2);
}
IEnumerator SaveText()
{
string path = "";
guitxt = "書き込み\n";
#if UNITY_EDITOR
path = Application.dataPath + "/" + outputTextData;
FileInfo fi = new FileInfo(path);
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(guitxt);
}
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
path = Application.persistentDataPath + "/" + outputTextData;
FileInfo fi = new FileInfo(path);
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(guitxt);
}
yield return new WaitForSeconds(0f);
#endif
}
IEnumerator LoadText2()
{
string txtBuffer = "";
string path = "";
#if UNITY_EDITOR
path = Application.dataPath + "/" + outputTextData;
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
txtReader = new StreamReader(file);
guitxt2 = "WINDOWS\r\n";
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
path = Application.persistentDataPath + "/" + outputTextData;
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
txtReader = new StreamReader(file);
guitxt2 = "ANDROID\r\n";
yield return new WaitForSeconds(0f);
#endif
while ((txtBuffer = txtReader.ReadLine()) != null)
{
guitxt2 = guitxt2 + txtBuffer + "\r\n";
}
}
}
----------- WriteTxtScr.cs -------------
- 関連記事
-
YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
- 2017/03/08(水) 22:51:30|
- Unity
-
| トラックバック:0
-
| コメント:0