Android Asset Path

I just started to have a look at an android version of our game, and I ran into some problems.

I’ve just setup an editor tool where I create inventory items for our game. The item data is serialized saved inside a folder called GameData(inside the assets folder.)

I load this file using

public static string LoadFile(string fileName)
{
     var sr = new StreamReader(Application.dataPath + "/GameData/" + fileName);
     var fileContents = sr.ReadToEnd();
     sr.Close();
     return fileContents;
}

This works fine in the editor on iOS devices, but it seems like I get the wrong path on android.
At least the method does not return anything.

So, is the Application.dataPath not the same on android ios?

Tips 'n hints would be welcome.

Thanks!
Kjetil

I’m not sure what you are after but if you need some assets to be loaded runtime, this might be helpful info:

If you look at the documentation you will see that it does not return anything for the android device:

http://docs.unity3d.com/Documentation/ScriptReference/Application-dataPath.html

RAigex: Well, I was wondering if it was missing information or something, because the it does return a path, it just doesn’t seem to be the same path that ios/editor returns. I mean, that the contents of this path is not the same.

JFo: Thanks for the tip, I’ll have a look at that.

And yes, I have various game data which is created by an editor tool. It’s various inventory items. I load save the files using streamreader/writer.

At one point, I would like to replace these datafiles runtime by updating them from a server.
I do see now that files located in the AssetsFolder are read - only. So, is there another place I can place files, and read/write to them at runtime?
Playerprefs is not an option as I need to create these files in the editor and have them copied to the device.

Thanks guys for the feedback!

Kjetil

If it does return the path what path does it return. Because as far as i know all data is kept inside the APK which you have to go though some playing around to get into.

To load AND save in runtime, we use “Application.persistentDataPath” in all platforms.

And in Editor, we use Application.dataPath as a prefix to point our data when run in editor.

The selection is simply switch/case based on “RuntimePlatform” variable.

Yes, I was looking at the persitentDataPath. So you are saying that Application.dataPath in editor = Application.persistentDataPath on other platforms?

What I currently do is that I have a unityeditor extension which works as an item tool. I can add edit items to a list, and I serialize and save this list as a string.
Then I load this file, and deserialize it in the game. The main reason for having the items in external data was to be able to update and edit the items without recompiling, ie. I would like to download the files from a server replace if the current file is older than the one on the server.

In the Editor, Application.persistentDataPath points to the folder …/Library/Caches/my_id/name_of_the_game

Are files in this folder copied to the device when building to Android/iOS? (I guess not?)

So, I’m currently toying with the idea of having the item tool create an assetbundle for each platform, which would either be read from file, or downloaded from web if it’s newer.

One trick we use on occasion, if you need to read a raw file from android, you have to remember that the dataPath is actually included inside the APK which is a jar/zip file. You could put whatever file you want to access into your Assets/StreamingAssets, and unzip them out of the APK using a lib like sharpziplib, then stage them into your persistantDataPath, or process the stream, depending on your need.

No, I didn’t mean that the Application.dataPath in editor = Application.persistentDataPath. In editor, the “dataPath” points to a project’s Assets dir. You can make your own “data” folder at the same level as the “Assets” and access it using something like Application.dataPath + “/…/MyOwnDataFolder”. Data in there are not copied automatically to the build, so for that you have to use “StreamingAssets”. You can of course copy data in runtime from that “StreamingAssets” folder to persistentDataPath if you need some initial setup of files… Of course in Android, you can mount the device as a MediaDevice and copy files directly to the “persistentDataPath” (just make Debug.Log to print out the actual path if you don’t know where to find it…)

Hope I made it clear this time :wink:

Thanks a lot guys, it’s all a lot more clear now, and I see there are quite a few options.

I pondered to put the files under StreamingAssets, but I decided to simply let the editortools write datafiles into a “GameData” folder under resources, and then load them again through Resources.Load as a TextAsset.

This works fine. Then if I later want to update this data, I can check if a certain assetbundle exist, and use the data from this assetbundle instead. So, assume that this textdata is v 1.0 of the file, and all bundles are newer.
I don’t actually need to write anything to the device, except savegame data which is currently serialized into a big string and saved in playerprefs. This works alright, and it’s just a few entries.

Anyway, lots of good tips here. Very appreciated! If any of you see any potential flaws with the procedure I sketched above, I’d be happy to hear it :slight_smile:

Thanks!

Kjetil

Hello,

I am trying to write a png file to the asset/resources/myfolder on android but I am unable to find the exact path to do so …

System.IO.File.WriteAllBytes(Application.persistentDataPath + “/…/Assets/Resources/myfolder” + name + “_large.png”, bytes);

But this returns following error:

IsolatedStorageException: Could not find a part of the path “/mnt/sdcard/Android/data/com.xxx.games/Assets/Resources/myfolder/CustomImage_1_icon.png”

Not sure what is the exact path to the asset folder. Can you please help?

Thanks!

Neha