With an Example Of WWW Use Case For Reading JSON

I realized that the tutorial for localization on Unity’s website doesn’t work on Android so I started digging Unity forums and StackOverflow for the solution. Finally, I managed to put together what I found on the net and got a solution for myself. My problem was about reading files from the StreamingAssets folder on Android. In this post, I’m gonna talk about how I handled this situation using www API. Hope it helps someone.

You can see tutorial related to this post at:

https://unity3d.com/learn/tutorials/topics/scripting/overview-and-goals?playlist=17117

In my project, I’m using the same localization system as the tutorial which uses keys as input and returns localized values. I placed my two different language JSON files in the StreamingAssets folder. You can read the documentation of this folder but simply its purpose is providing a particular folder for all platforms. We can read it by calling Application.streamingAssetsPath. In this way, our application will use different paths depending on which platform it’s working on.

Paths are defined below:

On a desktop computer (Mac OS or Windows)

path = Application.dataPath + “/StreamingAssets”;

On iOS:

path = Application.dataPath + “/Raw”;

On Android:

path = “jar:file://” + Application.dataPath + “!/assets/”;


File.Exists is not working properly because Unity creating an .APK file and it’s an archive. As you may know, the FileInfo class does not support archives like this. So we can use www instead.

if (File.Exists(filePath)) //Problematic part 
{
  string dataAsJson = File.ReadAllText(filePath);
  LocalizationData loadedData = JsonUtility.FromJson < LocalizationData > (dataAsJson);

  for (int i = 0; i < loadedData.items.Length; i++) {
    localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
  }
} else {
  Debug.LogError(“Cannot find the file!");
  }

This code will print “Cannot find the file!” no matter file exist or not. Fortunately, we’ve got a solution for this. Of course, www has downsides like memory duplication issues and asynchronous behavior but you can get over them with a little bit more coding effort. Check the working version of the code below:


This code will print “Cannot find the file!” no matter file exist or not. Fortunately, we’ve got a solution for this. Of course, www has downsides like memory duplication issues and asynchronous behavior but you can get over them with a little bit more coding effort. Check the working version of the code below:

IEnumerator LoadLocalizedTextOnAndroid(string fileName) {
    localizedText = new Dictionary < string, string > ();
    string filePath;
    filePath = Path.Combine(Application.streamingAssetsPath + “/”, fileName);
        string dataAsJson;
        if (filePath.Contains(“: //”) || filePath.Contains (“:///”)) 
            {
              UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(filePath);
              yield
              return www.Send();
              dataAsJson = www.downloadHandler.text;
            }
            else {
              dataAsJson = File.ReadAllText(filePath);
            }
            LocalizationData loadedData = JsonUtility.FromJson < LocalizationData > (dataAsJson);

            for (int i = 0; i < loadedData.items.Length; i++) {
              localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
              Debug.Log(“KEYS: ”+loadedData.items[i].key);
            }

          }

Here you can see my working localization system’s demonstration. It’s getting values from JSON files for each language and refreshing scene.

There are plugins like Better Streaming Assets in Asset Store if you did not satisfied with www.