C# 生成后未找到Json文件

C# 生成后未找到Json文件,c#,json,visual-studio,unity3d,C#,Json,Visual Studio,Unity3d,我想找到json文件保存的位置。在编辑器模式下,我可以找到它。然而,当我构建游戏时,我找不到它 我正在使用此脚本保存游戏: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class DataManager : MonoBehaviour { public GameHandler data; private string

我想找到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;
    }
}

伙计们,我怎么才能找到它?我可以指定其路径吗?

来自
persistentDataPath
afaik的文件将自动打包到内置应用程序中

但是
流化资产
不可用

构建的应用程序可以在此地址加载资产

所以我通常在编辑器中使用。这与简单地将文件放入文件夹
Assets/StreamingAssets
相同

我更喜欢这样,因为我不希望我的开发应用程序将一些文件膨胀到Windows PC上隐藏的任何文件夹中,而是在相应项目的
资产
文件夹中

然后,在生成后的运行时,此文件夹是只读的,在设备上不可见/不可访问

因此,我检查
persistentDataPath
中是否存在相应的文件

  • 如果是的话,我从那里读
  • 如果没有,则从
    StreamingAssetPath
    读取一次,并将其复制到
    PersistentDataPath

另一个要点是:

切勿对文件路径使用
+“/”+

而是使用
Path.Combine
,它会自动使用正确的平台特定路径分隔符符号


代码取决于自

无法访问WebGL和Android平台上的
StreamingAssets
文件夹。WebGL上没有可用的文件访问权限。Android使用压缩的.apk文件。这些平台返回一个URL。使用
UnityWebRequest
类访问资产

我将只添加独立代码(UWP),因为我对Android没有太多经验,但使用协同程序和
UnityWebRequest
读取
StreamingAssetPath

因此,在代码中,它可能类似于

#if UNITY_EDITOR
using UnityEditor;
#endif

...

private string DataFolder
{
#if UNITY_EDITOR
    return Application.streamingAssetsPath;
#else
    return Application.persitentDataPath;
#endif
}

private string GetFilePath(string fileName)
{
    return Path.Combine(DataFolder, fileName);
}

//                  | Little typo here btw
//                  v
private void WriteTopFile(string fileName, string json)
{
    string filePath = GetFilePath(fileName);

    // Create folder if not exists
    if(!Directory.Exists(DataFolder)
    {
        Directory.CreateDirectory(DataFolder);
    }

    using(var fileStream = new FileStream(filePath, FileMode.Create))
    {
        using (StreamWriter writer = new StreamWriter(fileStream))
        {
            writer.Write(json);
        }
    } 

#if UNITY_EDITOR
    AssetDataBase.Refresh();
#endif
}
现在,如前所述进行阅读时,请检查文件是否存在永久路径:

private string ReadFromFile(string fileName)
{
    var filePath = GetFilePath(fileName);
    var output = "";

    Debug.Log($"Trying to read file {filePath}");
    if (File.Exists(filePath))
    {
        Debug.Log($"File found! Reading from {filePath}");
        using(StreamReader reader = new StreamReader(filePath))
        {
            output = reader.ReadToEnd();
        }
        return output;
    }

    // Otherwise rather try to read from streaming assets
    var altFilePath = Path.Combine(Application.streamingAssetsPath, fileName);
    Debug.LogWarning($"File not found! Instead trying to read file {altFilePath}");

    if(File.Exists(altFilePath)
    {
        // read from here but directly also write it back to persistentDataPath
        Debug.Log($"File found. Reading from {altFilePath}");

        // As mentioned for Android you would now do the rest in a Coroutine 
        // using a UnityWebRequest.Get              

        using(StreamReader reader = new StreamReader(altFilePath))
        {
            output = reader.ReadToEnd();
        }

        // Now before returning also write it to persistentDataPath
        WriteTopFile(fileName, output);
        return output;
    }

    // If this also fails you didn't put such a file to `Assets/StreamingAssets` before the build
    Debug.LogError($"File also not found in {altFilePath}/n/nRemember to play it in 'Assets/StreamingAssets' before building!");
    return output;
}


注意:目前我只打电话,所以没有保修,但我希望这个想法已经很清楚:)

我的直觉告诉我C:\Users\USER\AppData中的某个地方。但我希望它在build文件夹中