当前位置:网站首页>Unity3d multi platform method for reading text files in streamingasset directory

Unity3d multi platform method for reading text files in streamingasset directory

2022-06-12 06:04:00 VR technology Xiaoguang

because StreamingAsset The catalogue is in androway IOS Next , The way of packing is different , The file reading method is different .

The following functions , Tested repeatedly , You can read text files of all platforms .

//  Support multi platform text file reading 
public static string GetFileStr(string path)
    {
        string jsonStr = "";
#if UNITY_ANDROID || UNITY_IOS
        jsonStr = GetStreamingPathStr(path);
#else
        jsonStr = FileRead(path);
#endif
        return jsonStr;
    }

public static string GetStreamingPathStr(string path)
    {
        string fileStr = "";
        var uri = new System.Uri(path);
        var request = UnityWebRequest.Get(uri);

        var www = request.SendWebRequest();
        if (request.isNetworkError || request.isNetworkError)
        {
            MyLog.log.Debug(request.error);
        }
        else
        {
            while (true)
            {
                if (!request.isDone) continue;
                fileStr = request.downloadHandler.text;
                break;
            }
        }
        return fileStr;
    }

public static string FileRead(string path)
    {
        StreamReader sr = File.OpenText(path);

        // Read to end of file 
        string str = sr.ReadToEnd();
        sr.Close();
        sr.Dispose();
        return str;
    }

 

原网站

版权声明
本文为[VR technology Xiaoguang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010612515372.html