|
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/blo ... 762011112810251960/
Application.dataPath 所在位置为:
编辑器模式下 为 Assets文件夹
Windows build 模式下为 Data 文件夹
Mac build 模式下为 Contents文件夹 |
|