Skip to content

Instantly share code, notes, and snippets.

@amowu
Created December 25, 2013 08:24
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save amowu/8121334 to your computer and use it in GitHub Desktop.
Save amowu/8121334 to your computer and use it in GitHub Desktop.
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");
}
@ginxx009
Copy link

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

@AWiesbrock
Copy link

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

@livein21st
Copy link

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

The code work for iOS but not in Android!!

Any Idea?

@ram1505
Copy link

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
Copy link

Thanks !

@nouratalla
Copy link

Thankkkkkkkkk you verrrryyyyyyy muccccchhhhhhhh ;DDDDDDDD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment