Accessing StreamingAssets in Android or iOS

Posted on

StreamingAssets is useful if you want to access files that you need in your project and want to access it via a pathname, such as a game data, or a json data, video, or anything else.

Any files placed in a folder called StreamingAssets (case-sensitive) in a Unity project will be copied verbatim to a particular folder on the target machine. You can retrieve the folder using the Application.streamingAssetsPath property. It’s always best to use Application.streamingAssetsPath to get the location of the StreamingAssets folder, as it will always point to the correct location on the platform where the application is running.

The location of this folder varies per platform. Please note that these are case-sensitive:

On a desktop computer (Mac OS or Windows) the location of the files can be obtained with the following code:

 path = Application.dataPath + "/StreamingAssets";

On iOS, use:

 path = Application.dataPath + "/Raw";

On Android, use:

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

On Android, the files are placed inside a compressed jar file. Luckily, Unity have tools to retrieve these files using Unity’s WWW class, unless you want to use other third party or additional software to see what’s inside this jar file.

Here are some snippets that i use in one of my project using Unity 3D to access saved data on StreamingAssets.

    // example accessing data.json from hosting url and save it to StreamingAssets and then read it.    private string gameDataFileName = 'data.json'    private IEnumerator LoadGameDataFromUrl()    {        string url = http://YOUR_URL_PATH;        string fileUrlPath = Path.Combine(url, gameDataFileName);        UnityWebRequest www = UnityWebRequest.Get(fileUrlPath);        yield return www.SendWebRequest();        //Check if we failed to send request        if (string.IsNullOrEmpty(www.error))        {#if UNITY_EDITOR            Debug.Log("Success");            print(www.downloadHandler.text);#endif            // Make your own data model            GameData gameData = new GameData();                       // save json data to file            gameData = JsonUtility.FromJson<GameData>(www.downloadHandler.text);            string dataAsJson = JsonUtility.ToJson(gameData);            string filePath = Application.dataPath + gameDataProjectFilePath;            File.WriteAllText(filePath, dataAsJson);            // read data.json from StreamingAssets            StartCoroutine(LoadGameDataFromLocal());        }        else        {            UnityEngine.Debug.Log("Error: " + www.error);            StartCoroutine(LoadGameDataFromLocal());        }    }        private IEnumerator LoadGameDataFromLocal()    {        // Path.Combine combines strings into a file path        // Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build        string filePath = Path.Combine(Application.streamingAssetsPath, gameDataFileName);        UnityWebRequest www = UnityWebRequest.Get(filePath);        yield return www.SendWebRequest();        if (string.IsNullOrEmpty(www.error))            Debug.Log(www.error);        else        {#if UNITY_EDITOR            print(www.downloadHandler.text);#endif            string dataAsJson = www.downloadHandler.text;            // Pass the json to JsonUtility, and tell it to create a GameData object from it / your own model            GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);            // Retrieve the allRoundData property of loadedData            allRoundData = loadedData.allRoundData;                    }    

That’s it.

Loading...

Leave a Reply

Your email address will not be published. Required fields are marked *