Unity3D 读写txt 文件代码
1.在 unity3d 中新建一个javascript名为为 file.js
2.写入如下代码:
import System.IO;
var fileName;
function OnGUI() {
if (GUI.Button(Rect(10,10,100,50),"Write File")) {
//fileName = Application.persistentDataPath + "/" + "test.txt";
fileName = Application.dataPath + "/" + "test.txt";
WriteFile(fileName);
}
if (GUI.Button(Rect(10,110,100,50),"Read File")) {
//fileName = Application.persistentDataPath + "/" + "test.txt";
fileName = Application.dataPath + "/" + "test.txt";
ReadFile(fileName);
}
}
//写文件
function WriteFile(filepathIncludingFileName : String)
{
fileWriter = File.CreateText(filepathIncludingFileName);
fileWriter.WriteLine("Hello world");
fileWriter.Close();
}
//读取文件
function ReadFile(filepathIncludingFileName : String) {
sr = new File.OpenText(filepathIncludingFileName);
input = "";
while (true) {
input = sr.ReadLine();
if (input == null) { break; }
Debug.Log("line="+input);
}
sr.Close();
}
3.路径解释:
Application.persistentDataPath所在位置为:
C:/Documents and Settings/atlas/Local Settings/Application Data/你的用户名/files/
且 PC, Mac, iOS and Android指向的位置相同不需要针对每个平台去写
转自http://cl314413.blog.163.com/blog/static/1905079762011112810251960/
Application.dataPath 所在位置为:
编辑器模式下为 Assets文件夹
Windows build模式下为Data 文件夹
Mac build模式下为 Contents文件夹 看看 学习学习 很不错 好帖就是要顶 顶顶多好 真心顶 难得一见的好帖 很不错 顶顶多好 说的非常好
页:
[1]