4

I have doubt regarding certain concepts between these two. For Application.streamingAssetsPath, I should create a StreamingAssets folder in my project, so that I can save files to it and later reload it. So what is the role of Application.persistentDataPath and the role of Application.streamingAssetsPath?

If I have assets and data (Position, health etc) to be saved and later reload them in mobile devices (Android and IOS) and PC. Which is the best option?

Below I save using Application.streamingAssetsPath

using (FileStream fs = new FileStream(Application.streamingAssetsPath + "/Position.json", FileMode.Create))
    {
        BinaryWriter filewriter = new BinaryWriter(fs);
        filewriter.Write(JsonString);
        fs.Close();


    }
IARI
  • 1,217
  • 1
  • 18
  • 35
zyonneo
  • 1,319
  • 5
  • 25
  • 63
  • StreamingAssets are built into the apk/ipa and the files cannot be modified. You also have to take extra care on Android to avoid having them compressed because they are very slow to load that way. The PersistentDataPath is where any files you save on the device are stored. You can add/delete/change files here, and on iOS these files are saved to iCloud backups if you don't flag them to not be backed up. – Retired Ninja Dec 31 '18 at 08:28
  • @RetiredNinja For IOS and Android the paths are different.I have to write two paths in my code .eg -if #UNITY_IOS Application.persistentDatapath and if #UNITY_ANDROID Application.persistentDatapath. – zyonneo Dec 31 '18 at 08:46
  • Not sure what you're saying. Yes, the contents of persistentDataPath are different on different platforms, but Unity handles that, there's nothing you need to do about it. – Retired Ninja Dec 31 '18 at 09:06
  • @RetiredNinja I mean if i want my game to work both in android and IOS ...and save games in both versions.what should I do – zyonneo Dec 31 '18 at 09:09
  • In short: use persistentDataPath if you need access to the files from outside the App (e.g. for changing settings in in an XML file via Texteditor etc). You can read in the [docs](https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html) where to find that persistentDataPath on the device for different plattforms. If you don't need that external access you can just go for streamingAssets. Unity handles the paths on all platforms so the code stays the same for both Android and iOs .. that's the whole idea of using Unity ;) – derHugo Dec 31 '18 at 11:09
  • Possible duplicate of [Location of Application.persistentDataPath in a build](https://stackoverflow.com/questions/44419658/location-of-application-persistentdatapath-in-a-build) – derHugo Dec 31 '18 at 11:13

1 Answers1

7

Generally, use Application.persistentDataPath for data that was not available during build time and will be modified after being distributed (and should never be modified by a game update), and use Application.streamingAssetsPath for game data that exists before your build that you want to be able to read with IO systems during the game (and might be modified in a game update). For example, player save data likely should be placed in Application.persistentDataPath and dialogue files might be placed in Application.streamingAssetsPath.

The biggest technical difference is that usually Application.persistentDataPath can be saved in a location separate from the game data, so that an uninstall or update of the game will not cause a player to lose their data. Most of the difference is in intent, in that Application.persistentDataPath is intended for saving data between runs of a game, and Application.streamingAssetsPath is intended to allow developers to have game files that can be accessed by path name.


If you are storing the current position, current health, and current status of a character that you are keeping track of, you will want Application.persistentDataPath. If you are storing the data for the starting position, maximum health, and other stats of a character that you will use for initialization, Application.streamingAssetsPath would be a better pick.

adisib
  • 197
  • 7
  • `If you are storing the data for the starting position, maximum health, and other stats of a character that you will use for initialization, Application.streamingAssetsPath would be a better pick.` you would do this rather in the classes directly .. no need to store those in a Textfile ... and you only would save it there if you don't want to be able to change them later .. then you would again rather place it in `persitentDataPath` – derHugo Jan 02 '19 at 08:28
  • That may be true, but the question seemed to imply that they wanted it to be in a separate file for whatever reason. I try to take on the implied context rather than assume my own. For example, they could have an external program generating character data, in which case they would want it generating files, rather than code. – adisib Jan 02 '19 at 20:09
  • What is the benefit of using Application.streamingAssetsPath over Application.dataPath? – pete Feb 03 '22 at 22:04