Using SQLite on Unity for Android and iOS
Tags: android, ios, sql, rudderstack
Published: 2020-03-18 11:50:04

<!-- wp:paragraph -->
<p>While going through the <a href="https://github.com/rudderlabs/rudder-server">RudderStack</a> SDK roadmap, we decided to include Unity next in the list of the supported platforms. Why Unity? We found that despite heavily relying on data, the hosted customer data management solutions out there underserve the gaming industry. This dependency on data is the result of a number of factors. Games generate a high volume of data, especially telemetry data coming from mobile devices while gaming companies face great difficulty in the monetization of their products. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Background</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://rudderstack.com/">RudderStack</a> promises to capture user-generated events. We route these events to various destinations, such as cloud applications, data warehouses, and cloud file systems. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For the successful implementation of the Unity SDK, we had four main requirements:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The SDK should implement initialization and event collection mechanisms.</li><li>Events should be temporarily stored on persistent storage to ensure the delivery semantics that we want for the platform.</li><li>Events should be pushed to the RudderServer. These events will run in different environments.</li><li>Finally, there should be a mechanism to handle device-mode SDKs. Device-mode SDKs support a number of destinations that cannot be supported through the RudderStack server-to-server integration. Another intention for device-mode support was to leverage the power of Native SDKs specifically for push notifications, attributions, and more.</li></ol>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>The Challenges</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>Choosing the correct C# version for Unity</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The first challenge we encountered was choosing the correct C# version. Unity supports .Net 3.5, .Net 4.x, and .Net Standard. However, there are some major changes and incompatibilities between versions 3.5 and 4.x. Many popular games are built on version 3.5. Migrating to 4.x is such a painful process that studios prefer staying on older versions.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We started developing the Unity SDKs with .Net 4.x. Soon after the release, we realized that many games couldn’t use our SDK because of being developed in version 3.5. In the end, we ended up rewriting around 80 percent of the codebase to go back to version 3.5.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Lesson learned from this</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Always research well on the following BEFORE you start developing:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Which is the most adopted software version in the industry?</li><li>What’s the adoption of the  SDKs relevant to our project? Learn and prioritize by identifying what developers are using and why.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>These are important considerations, both from the product and engineering points of view.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Delivering Unity data securely and consistently to the chosen destination</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now came the main challenge, which is also the reason we wrote this blog post. Capturing and transmitting data over unreliable networks is a difficult task. It’s futile to capture data if you don’t manage to deliver it securely and consistently to the chosen destination. One way to deliver the data securely and consistently is to have some kind of caching mechanism to store the events. This mechanism stores the events until you receive the acknowledgment that the data is delivered correctly to the destination.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can store events on Unity using <a href="https://docs.unity3d.com/ScriptReference/PlayerPrefs.html">PlayerPrefs</a>. However, we had three major constraints:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>We should be able to store an event buffer of arbitrary size. The default buffer size we use in Rudder is 10K events. However, for this case, we wanted to be able to scale it up and down.</li><li>There should be the flexibility of defining and enforcing a schema on the events that are stored. PlayerPrefs is a simple key-value store supporting a very limited set of data types and no hierarchy.</li><li>Managing the buffer should not happen in the main thread. Working with PlayerPrefs happens in the main thread, which is not a viable option in most cases.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Instead of PlayerPrefs, we decided to use SQLite for this purpose. SQLite is a lightweight, embeddable, and highly reliable SQL database engine that works incredibly well for mobile devices. However, embedding SQLite in Unity is not straightforward.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>The Implementation</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>Getting started</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>.Net and Unity do not support  SQLite out of the box. We also wanted to minimize the number of third-party libraries used as much as possible to keep the SDK as lightweight as possible. Collecting events should be extremely lightweight without introducing bugs and a large number of dependencies.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Initially, we tried to build separate binaries for supporting SQLite and integrating with the Unity SDK. The problem here is that this design is painful to deploy and maintain in production as it is not thread-safe. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Unity plugins support for Android and iOS</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Thus, we finally decided to follow a popular Unity pattern that includes the exploitation of the excellent <a href="https://docs.unity3d.com/Manual/Plugins.html">plugins</a> supports that Unity offers.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Unity supports code for Android and iOS simultaneously using preprocessor directives. It controls the lines of code that will be executed depending on the platform. We built Android SDK and iOS plugins separately for Unity by exploiting this Unity offering.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, our iOS plugin for Unity has a method for initialization of the <code>RudderClient</code> that looks like the following code snippet:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>+ (void) _initiateInstance: (NSString*) _anonymousId
                  writeKey: (NSString*) _writeKey
               endPointUrl: (NSString*) _endPointUrl
            flushQueueSize: (int) _flushQueueSize
          dbCountThreshold: (int) _dbCountThreshold
              sleepTimeOut: (int) _sleepTimeout
                configRefreshInterval: (int) _configRefreshInterval
      trackLifecycleEvents: (BOOL) _trackLifecycleEvents
         recordScreenViews: (BOOL) _recordScreenViews
                  logLevel: (int) _logLevel;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This takes all the necessary parameters from the Unity bridge and initiates the <code>RudderClient</code>. We have a similar method for Android as follows:  </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code> public static void _initiateInstance(
            Context _context,
            String _anonymousId,
            String _writeKey,
            String _endPointUrl,
            int _flushQueueSize,
            int _dbCountThreshold,
            int _sleepTimeout,
            int _configRefreshInterval,
            boolean _trackLifecycleEvents,
            boolean _recordScreenViews,
            int _logLevel
    )</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now, when we want to call these methods from our C# code, we can control the calling of these methods as well as the platform using the preprocessor directives at run time. The code in C# looks like this: </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>#if UNITY_IPHONE
[DllImport("__Internal")]
private static extern void _initiateInstance(
    string _anonymousId,
    string _writeKey,
    string _endPointUrl,
    int _flushQueueSize,
    int _dbCountThreshold,
    int _sleepTimeout,
    int _configRefreshInterval,
    bool _trackLifecycleEvents,
    bool _recordScreenViews,
    int _logLevel
);
#endif</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This code defines the method to be used for the iPhone environment. When we need to call a method, we can check the current runtime and call this method using the code snippet below: </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>#if UNITY_IPHONE
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
    _initiateInstance(
        RudderCache.GetAnonymousId(),
        _writeKey,
        _endPointUrl,
        _flushQueueSize,
        _dbCountThreshold,
        _sleepTimeout,
        _configRefreshInterval,
        _trackLifecycleEvents,
        _recordScreenViews,
        _logLevel
    );
}
#endif</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Similarly, for Android, we have to define an <code>AndroidJavaClass</code> object inside our Unity code so that we can access its methods in our code. To do this, please use the following code:  </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code> #if UNITY_ANDROID
private static readonly string androidClientName = "com.rudderstack.android.sdk.wrapper.RudderClientWrapper";

private static AndroidJavaClass androidClientClass;

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic&lt;AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call&lt;AndroidJavaObject>("getApplicationContext");
androidClientClass = new AndroidJavaClass(androidClientName);
#endif</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Finally, to call the <code>initiateInstance</code> method from our Android Plugin, we will check the runtime and call using the <code>callStatic</code> method of the <code>AndroidJavaClass</code> object as shown below: </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>#if UNITY_ANDROID
if (Application.platform == RuntimePlatform.Android)
{
    androidClientClass.CallStatic(
        "_initiateInstance",
        context,
        RudderCache.GetAnonymousId(),
        _writeKey,
        _endPointUrl,
        _flushQueueSize,
        _dbCountThreshold,
        _sleepTimeout,
        _configRefreshInterval,
        _trackLifecycleEvents,
        _recordScreenViews,
        _logLevel
    );
}
#endif</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3>Using the Unity Plugins directory</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In addition, Unity supports a special kind of directory under its <code>Assets</code> directory named <code>Plugins</code>. This directory holds all the platform-specific libraries required for the application. All it takes is to create two folders, one named <code>Android</code> and another one named <code>iOS</code> inside the <code>Plugins</code> folder. Unity will automatically add those files with the final product/build of your project for the corresponding platform.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We leveraged this mechanism and built plugins for both Android and iOS. We added these plugins to the project following the directory structure mentioned above. This way, Unity is able to interop with both JAVA for Android and Object-C for iOS. Due to this, we get a lot of flexibility in terms of the technologies we can interact with. In our case, we could embed SQLite for both Android and iOS in Unity.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Following this approach, we ended up having two plugins, one for each platform and a wrapper Unity app that interops with these plugins to expose the functionality we need.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>What just happened:</p><p>To quickly summarize, the wrapper is written in C# and it takes care of our first requirement of being able to manage the size of the event cache on the client-side. The plugins take care of the rest of the requirements we mentioned at the beginning of the post.</p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":3} -->
<h3>Adding new destinations</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Finally, we wanted to offer as many destinations as possible that are relevant to Unity and mobile development. The most relevant destinations for Unity are Adjust and Firebase. For these two, we support what we call device-mode SDKs. The device-mode SDKs allow events to be pushed to destinations without the need to go through the RadderStack Server.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We decided to build the <a href="https://github.com/rudderlabs/rudder-sdk-unity/blob/master/SDK/Rudder/RudderIntegrationManager.cs">Device-Mode mechanism</a> in Unity itself and use the Unity SDKs of the platforms. In this way,  you can use the platform-supported native code, which offers greater reliability and support for the long term.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Unity is a feature-rich and versatile game development platform. We wanted to offer gamer developers functionalities around data that are commonly accessible to mobile and server-side developers. Although supporting an embedded database is not trivial in Unity, the platform allows you to do it in an elegant way through the plugin system.<br>RudderStack and all the SDKs are open-source and available on <a href="https://github.com/rudderlabs">GitHub</a>. You can also find the detailed documentation for this Unity SDK <a href="https://docs.rudderstack.com/sdk-integration-guide/getting-started-with-unity-sdk">here</a>. We’d love to hear your feedback on how to improve our SDKs. </p>
<!-- /wp:paragraph -->