【发布时间】:2019-11-09 18:03:02
【问题描述】:
我想查找 json 文件的保存位置。在编辑器模式下我可以找到它。但是,当我制作游戏的构建时,我找不到它。
我正在使用这个脚本来保存游戏:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class DataManager : MonoBehaviour
{
public GameHandler data;
private string file = "player.txt";
public void Save()
{
string json = JsonUtility.ToJson(data);
WriteTopFile(file, json);
}
public void Load()
{
data = new GameHandler();
string json = ReadFromFile(file);
JsonUtility.FromJsonOverwrite(json, data);
}
private void WriteTopFile(string fileName, string json)
{
string path = GetFilePath(fileName);
FileStream fileStream = new FileStream(path, FileMode.Create);
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.Write(json);
}
}
private string ReadFromFile(string fileName)
{
string path = GetFilePath(fileName);
if (File.Exists(path))
{
print(path);
using(StreamReader reader = new StreamReader(path))
{
string json = reader.ReadToEnd();
return json;
}
}
else
{
Debug.LogError("File not found");
return "";
}
}
private string GetFilePath(string fileName)
{
return Application.persistentDataPath + "/" + fileName;
}
}
我怎样才能找到它呢?我可以指定它的路径吗?
【问题讨论】:
-
我的直觉告诉我在 C:\Users\USER\AppData 的某个地方。
-
但我希望它在构建文件夹中
标签: c# json visual-studio unity3d