I’m currently trying to port my Monk application to Unity3d in order to publish it to several stores using the same code.

My previous attempt was to use Xamarin and that’s why I started to use MvvmCross and getting it ready to start with Xamarin Android and iOS. I have to say that the app is already published in Windows Phone and Windows Stores using MvvmCross (which I highly recommend) but, just by chance, when I started facing other platforms development I stumbled uppon Unity3d and automatically fell in love with it. After trying some bits of it I decided to discard completely Xamarin and give Unity3d a try.

So far so good, but now problems arise… 😀

Monk uses  SQLite as a database and I’ve found it very difficult to find some good tutorial on the internet about how to deal with SQLite and Unity3d. The majority of them are focused on using Javascript but I’m writing my code in C#. Even more, I wanted to use the wonderful library sqlite-net from Praeclarum and, as I din’t find anything on this matter, I decided that I was going to describe to you how I managed to make it work .

In order to use sqlite-net in iOS, Android & Windows Phone I’ve used part of the  MvvmCross SQLite plugin’s code and created a new dll that you can find in my GitHub repository SQLite4Unity3d.

There, you will be able not only to find the dll but all the code used to create it and a working Unity Example. So, if you need to modify something you’re free to go.

If you want to take the fast track follow these steps

  1.  Download this zip and copy the resulting folder to your Assets/Plugins folder. It contains the dlls that Unity3d will need to access sqlite.
  2. Don’t forget to copy your SQLite database file in your Assets/StreamingAssets folder if you’re shipping one.
  3. You’re done! Now you can get access to your database using sqlite-net.  ;P

I hope it works for you! Easy, don’t you think?

Let me know if you have some comments or find any inconveniences. I’ll be happy to help you.

IMPORTANT FOR iOS
As you may know iOS doesn’t allow JIT compilation so, generally speaking, you will have to be very careful when you use reflection in your code. For a more deeper explanation of this issue please refer to Google but making it short: you cannot create dynamic objects using reflection. How is this affecting SQLite4Unity3d? Well, you can’t use the linq syntax when making queries to SQLite and you will have to use the old sql syntax.