Unity PC和Android端的数据存储和读取-PC端读取:

时间:2024-07-09 07:15:04
    static public T LoadJsonOnWinPath<T>(PathType path, string fileName)
    {
        string url = "";
        if (path == PathType.StreamingAssetsPath)
        {
            url = Application.streamingAssetsPath + "/" + fileName;
            Debug.Log("我是StreamingAssetsPath读取\n" + url);
        }
        else
        {
            url = Application.dataPath + "/" + fileName;
            Debug.Log("我是NormalPath读取\n" + url);
        }
        if (!File.Exists(url))
        {
            Debug.LogWarning("path is null !     " + fileName);
            return default(T);
        }
        StreamReader sr = new StreamReader(url);
        string json = sr.ReadToEnd();
        Debug.Log("json: " + json);
        T result = default(T);
        if (!string.IsNullOrEmpty(json))
        {
            result = JsonUtility.FromJson<T>(json);
        }
        sr.Close();
        return result;
    }