Skip to content

Instantly share code, notes, and snippets.

@leducanhh
Created July 29, 2016 08:58
Show Gist options
  • Save leducanhh/18cffd4c26bf29d358a692bf5affff09 to your computer and use it in GitHub Desktop.
Save leducanhh/18cffd4c26bf29d358a692bf5affff09 to your computer and use it in GitHub Desktop.
[Unity] Get StreamingAssets file path with Android and iOS.
void GetStreamingAssetsPath(string fileName)
{
#if UNITY_EDITOR
dbPath = string.Format(@"Assets/StreamingAssets/{0}", fileName);
#else
// check if file exists in Application.persistentDataPath
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, fileName);
if (!File.Exists(filepath))
{
Debug.Log("Database not in Persistent path");
// if it doesn't ->
// open StreamingAssets directory and load the db ->
#if UNITY_ANDROID
var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + fileName); // this is the path to your StreamingAssets in android
while (!loadDb.isDone) { } // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDb.bytes);
#elif UNITY_IOS
var loadDb = Application.dataPath + "/Raw/" + fileName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#elif UNITY_WP8
var loadDb = Application.dataPath + "/StreamingAssets/" + fileName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#elif UNITY_WINRT
var loadDb = Application.dataPath + "/StreamingAssets/" + fileName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#else
var loadDb = Application.dataPath + "/StreamingAssets/" + fileName; // this is the path to your StreamingAssets in iOS
// then save to Application.persistentDataPath
File.Copy(loadDb, filepath);
#endif
Debug.Log("Database written");
}
dbPath = filepath;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment