Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Unity StreamingAssets file path with Android and iOS.
// Put your file to "YOUR_UNITY_PROJ/Assets/StreamingAssets"
// example: "YOUR_UNITY_PROJ/Assets/StreamingAssets/db.bytes"
string dbPath = "";
if (Application.platform == RuntimePlatform.Android)
{
// Android
string oriPath = System.IO.Path.Combine(Application.streamingAssetsPath, "db.bytes");
// Android only use WWW to read file
WWW reader = new WWW(oriPath);
while ( ! reader.isDone) {}
realPath = Application.persistentDataPath + "/db";
System.IO.File.WriteAllBytes(realPath, reader.bytes);
dbPath = realPath;
}
else
{
// iOS
dbPath = System.IO.Path.Combine(Application.streamingAssetsPath, "db.bytes");
}
@saied63

This comment has been minimized.

Copy link

@saied63 saied63 commented Nov 5, 2015

Thank you very much for this code .

@SoyuzTechnocracy

This comment has been minimized.

Copy link

@SoyuzTechnocracy SoyuzTechnocracy commented Feb 22, 2016

Thanks a lot for sharing this!

@omavi

This comment has been minimized.

Copy link

@omavi omavi commented Mar 21, 2016

YOU ARE A GOD. Only place on the webz that I could find a solution to this problem, including searching on Unity's site. Thanks so much!

@madTeddy

This comment has been minimized.

Copy link

@madTeddy madTeddy commented Jul 11, 2016

OOooO my gooood. TY for that script!

@sahsubodh

This comment has been minimized.

Copy link

@sahsubodh sahsubodh commented Oct 10, 2016

this worked. thanks!

@AliAkbarMontazeri

This comment has been minimized.

Copy link

@AliAkbarMontazeri AliAkbarMontazeri commented Oct 11, 2016

Looks like everyone found this code work. Great ! I tried it to retrieve the .json file using his code and it didn't work. Anyone know how to use that code for .json file ?

@dnorth

This comment has been minimized.

Copy link

@dnorth dnorth commented Dec 29, 2016

This really is the only place to find a simple solution for this problem... can't believe it. But thanks!

@AliAkbarMontazeri I hope you've already found a solution for this, but just in case... I used this for my .json code in this way

        string filePath = Application.streamingAssetsPath + "/file.json";
        string jsonString;

        if(Application.platform == RuntimePlatform.Android) //Need to extract file from apk first
        {
            WWW reader = new WWW(filePath);
            while (!reader.isDone) { }

            jsonString= reader.text;
        }
        else
        {
            jsonString= File.ReadAllText(filePath);
        }
        
       Board deserializedJsonObject = JsonUtility.FromJson<Board>(jsonString);
@vmp1r3

This comment has been minimized.

Copy link

@vmp1r3 vmp1r3 commented Mar 14, 2017

Is this the decompressing of file from StreamingAssets?

@onildoaguiar

This comment has been minimized.

Copy link

@onildoaguiar onildoaguiar commented Mar 22, 2017

Thanks for share this code. @dnorth I used this same solution for my project. Thanks!

@rimagahwa

This comment has been minimized.

Copy link

@rimagahwa rimagahwa commented Mar 27, 2017

Thank you for the code , finally found a snippet I could use .

Also @dnorth for the JSON file handling !

@doggan

This comment has been minimized.

Copy link

@doggan doggan commented Jun 8, 2017

If your file is too large (> 2MB in my case), waiting for www.isDone in a while loop will hang the app.

Unity explicitly discourages this in the docs: You should not write loops that spin until download is done; use coroutines instead., because depending on the platform, the www download may take place on the main thread, which will hang up your app.

@ginxx009

This comment has been minimized.

Copy link

@ginxx009 ginxx009 commented Feb 28, 2018

Could someone help me please with this . I'm still not getting the .json data from android.

@AWiesbrock

This comment has been minimized.

Copy link

@AWiesbrock AWiesbrock commented Apr 9, 2018

I also had trouble getting the json to read. what is in Boards class from @dnorth that we don't see?

@livein21st

This comment has been minimized.

Copy link

@livein21st livein21st commented Apr 23, 2018

I have an XML file stored in the streamingAssets/XML/file.xml.

The code work for iOS but not in Android!!

Any Idea?

@ram1505

This comment has been minimized.

Copy link

@ram1505 ram1505 commented Jul 23, 2018

I'm extremely a newbie to Unity. I've a JSON file from the web. The JSON file contents loads well in Unity Desktop. However, when I build APK and load it from Phone, the JSON contents doesn't load. How can I get it to work?

Here is my code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using SimpleJSON;

public class DataLoader : MonoBehaviour
{
    string JsonDataString;
    public string OriginalJsonSite;
    public Text Temp;
    public Text Humid;

    IEnumerator Start()
    {
        while (true)
        {
            WWW readingsite = new WWW(OriginalJsonSite);
            yield return readingsite;

            if (string.IsNullOrEmpty(readingsite.error))
            {
                JsonDataString = readingsite.text;
            }

            JSONNode jsonNode = SimpleJSON.JSON.Parse(JsonDataString);

            Temp.text = jsonNode["temp"].ToString().ToUpper();
            Debug.Log(jsonNode["temp"]);

            Humid.text = jsonNode["humid"].ToString().ToUpper();
            Debug.Log(jsonNode["humid"]);
            yield return new WaitForSeconds(2);
        }

    }
}
@DivyanshK86

This comment has been minimized.

Copy link

@DivyanshK86 DivyanshK86 commented Jul 30, 2019

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.