- Home /
How to use StreamingAssets folder on Android by using UnityWebRequest?
So I'm using a .json file inside StreamingAssets folder to store data about player's purchased skins. The code works excellent on Unity but doesn't work on my mobile device at all. Through some research I found, that to access StreamingAssets folder on Android I need to use UnityWebRequest. I tried to create that code by myself but nothing worked. Then I attempted to find some codes from the web, but all codes were for WWW(Which is currently outdated). Can somebody help me with that? I use the code below in Unity Editor:
public class gameDataSaver : MonoBehaviour
{
public Skins skins;
public void LoadData(){
skins = JsonUtility.FromJson<Skins>(File.ReadAllText(Application.streamingAssetsPath + "/SaveSkins.json"));
}
public void SaveData(){
File.WriteAllText(Application.streamingAssetsPath + "/SaveSkins.json", JsonUtility.ToJson(skins));
}
[System.Serializable]
public class Skins{
public bool[] isPurchased}
The code from below I tried to use in order to get my .json(from StreamingAssets folder) to work on Android, but nothing worked:
private void Start(){
var _path = Application.streamingAssetsPath + "/SaveSkins.json";
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(_path);
www.SendWebRequest();
while (!www.isDone)
{
}
String jsonString = www.downloadHandler.text;
}
Answer by xxmariofer · Jun 09, 2021 at 09:24 AM
you are missing a couple of this
change
void Start
to
IEnumerator Start
and then change
www.SendWebRequest
to
yield return www.SendWebRequest
here is a fully working code I have used for reading a json from the streaming assets
string path = Application.streamingAssetsPath;
path += "/json/language.json";
using (UnityWebRequest www = UnityWebRequest.Get(path))
{
yield return www.SendWebRequest();
lang = JsonUtility.FromJson<NewLanguage>(ASCIIEncoding.UTF7.GetString(www.downloadHandler.data));