我使用c#作爲主要編程語言在Unity中製作遊戲。如果它不存在於應用程序文件夾中,我嘗試創建一個新的save.txt和autosave.txt。它可以創建它,但它不能正常工作。這裏是我的代碼: 創建並寫入新的保存:在c#和Unity中創建和編寫txt文件時,它只創建它並且不使用它
void Start() {
if(!File.Exists(Application.dataPath.ToString() + "/Save.txt"))
{
File.CreateText(Application.dataPath.ToString() + @"/Save.txt");
saveFilePath = Application.dataPath.ToString() + @"/Save.txt";
TextWriter writer = new StreamWriter(saveFilePath, false);
writer.WriteLine("10:21:59", "13/06/2017", "1", "1", "-21", "20000", "100", "500", "50", "20", "500","2","1","1","5000", "10");
writer.Close();
}
if(!File.Exists(Application.dataPath.ToString() + "/AutoSave.txt"))
{
File.CreateText(Application.dataPath.ToString() + @"/Autosave.txt");
saveFilePath = Application.dataPath.ToString() + @"/Autosave.txt";
TextWriter writer = new StreamWriter(saveFilePath, false);
writer.WriteLine("00:00:00", "01/01/2017", "1", "1", "-21", "20000", "100", "500", "50", "20", "500", "2", "1", "1", "5000", "10");
writer.Close();
}
}
這是我寫的存在的.txt代碼:
public void OnSaveGame()
{
saveFilePath = Application.dataPath.ToString() + @"/Save.txt";
isNewGame = false;
TextWriter writer = new StreamWriter(saveFilePath, false);
theTime = System.DateTime.Now.ToString("hh:mm:ss"); theDate = System.DateTime.Now.ToString("dd/MM/yyyy");
string ZeroPart = theTime + "," + theDate + ",";
string FirstPart = income;
writer.WriteLine(ZeroPart + FirstPart);
writer.Close();
SavingPanel.SetActive(true);
Invoke("WaitTime",2);
}
我不知道我做錯了。 P.S.統一時,如果它有幫助,當我運行它時,它說它「IOException:在路徑C上共享衝突:* \ Assets \ Save.txt」