----------------- 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");
WriteFile(guitxt);
}
void OnGUI()
{
GUI.TextArea(new Rect(5, 155, 200, 60), guitxt);
if (GUI.Button(new Rect(300, 155, 100, 40), "書き込みテスト"))
{
//StartCoroutine("LoadText2");
ReadLine();
}
GUI.TextArea(new Rect(5, 200, 250, 100), guitxt2);
}
void WriteFile(string txt)
{
string path = "";
//guitxt = "書き込み\n";
if (Application.platform == RuntimePlatform.Android)
{
// Android
path = Application.persistentDataPath + "/" + outputTextData;
FileInfo fi = new FileInfo(path);
guitxt += " - and\n";
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(guitxt);
}
}
else if (Application.platform == RuntimePlatform.WindowsEditor)
{
//Unity
path = Application.dataPath + "/" + outputTextData;
FileInfo fi = new FileInfo(path);
guitxt += " - uni\n";
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(guitxt);
}
}
else if (Application.platform == RuntimePlatform.WindowsPlayer)
{
// Windows
path = Application.dataPath + "/" + outputTextData;
FileInfo fi = new FileInfo(path);
guitxt += " - win\n";
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(guitxt);
}
}
else
{
//other
path = Application.dataPath + "/" + outputTextData;
FileInfo fi = new FileInfo(path);
guitxt += " - oth\n";
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(guitxt);
}
}
}
void WriteFile2(string txt)
{
string path = "";
//guitxt = "書き込み\n";
#if UNITY_EDITOR
path = Application.dataPath + "/" + outputTextData;
FileInfo fi = new FileInfo(path);
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(guitxt);
}
#elif UNITY_ANDROID
path = Application.persistentDataPath + "/" + outputTextData;
FileInfo fi = new FileInfo(path);
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(guitxt);
}
#endif
}
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
}
void ReadLine()
{
string txtBuffer = "";
string path = "";
if (Application.platform == RuntimePlatform.Android)
{
// Android
path = Application.persistentDataPath + "/" + outputTextData;
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
txtReader = new StreamReader(file);
guitxt2 = "1ANDROID\r\n";
}
else if (Application.platform == RuntimePlatform.WindowsEditor)
{
// Unity
path = Application.dataPath + "/" + outputTextData;
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
txtReader = new StreamReader(file);
guitxt2 = "1Unity\r\n";
}
else if (Application.platform == RuntimePlatform.WindowsPlayer)
{
// Windows
path = Application.dataPath + "/" + outputTextData;
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
txtReader = new StreamReader(file);
guitxt2 = "1WINDOWS\r\n";
}
else
{
}
while ((txtBuffer = txtReader.ReadLine()) != null)
{
guitxt2 = guitxt2 + txtBuffer + "\r\n";
}
}
void ReadLine2()
{
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 = "2WINDOWS\r\n";
#elif UNITY_ANDROID
path = Application.persistentDataPath + "/" + outputTextData;
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
txtReader = new StreamReader(file);
guitxt2 = "2ANDROID\r\n";
#endif
while ((txtBuffer = txtReader.ReadLine()) != null)
{
guitxt2 = guitxt2 + txtBuffer + "\r\n";
}
}
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/19(日) 17:42:12|
- Unity
-
| トラックバック:0
-
| コメント:0