////////////////////////////////////////////
Unityでテキストファイルを外部領域へ書き込み
////////////////////////////////////////////
次に、
ファイルの書き込み。
練習は、WriteTxtObjという名前の
空オブジェクトを作る

AssetsにWriteTextという名前のtextファイルを作る
これは、書き込み用なので空の状態でOK

さらに
WriteTxtScrという、C#Scriptファイルを作る
生身は以下のようにした。

---------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 outputTextData = "WriteText.txt";
void Start()
{
guitxt = "ファイルの書き込み\n";
WriteFile(guitxt);
}
void OnGUI()
{
GUI.TextArea(new Rect(5, 155, Screen.width, 30), guitxt);
}
void WriteFile(string txt)
{
FileInfo fi = new FileInfo(Application.dataPath + "/" + outputTextData);
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine(txt);
}
}
}
---------WriteTxtScr.cs---------
画面に

さらに、空にしておいたファイルには、
ファイルの書き込み
と、入っている。
- 関連記事
-
YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
- 2017/02/24(金) 11:14:52|
- Unity
-
| トラックバック:0
-
| コメント:0