Mailing List

Friday, July 3, 2015

File IO in Unity C# for iOS

Hi all,

This is going to be a pretty technical oriented blog post for me. I rarely do these, but the more I work with Unity, the more I feel that I want to share as much about my experience in hopes to limit the hardship of others who use it in the future.

If you may recall, the Android version of Burgal's Bounty uses SQLite for game save information. I chose SQLite largely because SQL in general is really easy to use. There was native support in Android for it. And it took me very little time to implement it and get everything up and running. I also know that iOS supports SQLite natively as well. I assumed that a lot of the code would be portable, because if both Android and iOS supported SQLite, Unity must have some support for it too...right...right?

Well... Unity actually does support SQLite. However, I ran into a ton of issues when compiling it to Xcode. It also took me forever to get Unity to support SQLite (I had to download a few extra DLLs and import them into my Assets\Plugins folder among a few other things). I found that much of the Unity community forums lacked complete information. All I was able to find was tidbits here and there. Unfortunately, I will not be talking about implementing SQLite in Unity for iOS today. I decided that it was going to take too long to do, so I abandoned that. As a result I needed a new approach for data saving for the iOS version of Burgal's Bounty. I chose simple file streams. Not even binary files - straight up textual file streams. Easy enough.

I have never made an iOS app or game before. I did not know much about the iOS file system structure and where the iOS lets you save data (in hindsight, I also do not know much about Android, but perhaps more than I do iOS). Anyway, you can only save data to your application specific Documents folder. The Unity documentation seems fairly sparse on this. You can find it in Apple documentation, but not necessarily how to "browse" to the appropriate folder using Unity. In short, to cut through the chatter on Unity's forums and find exactly where you can save your files to with low effort - simply set your file read and write path to this location:
Application.persistentDataPath.
You will be able to read and write files to this location on iOS. I do not know if this will work for Android. I hope this saves you some time during your development. I know it would have saved me some time if I could find consistent information in one place with Unity development. :)

Blair

No comments:

Post a Comment