Unity 3D: Integrating Smaad SDK „More Games“ Android Activity in a Unity Android game

Tutorial on how to integrate Smaad SDK in a Unity Android game

This is a tutorial on how to implement Smaad SDK in a Unity Android project. Smaad SDK is some sort of a Japan-only equivalent of Tapjoy and also displays a full screen Activity with a list of „other apps“ for the user to check out. I’m using Smaad SDK for this example because I had to implement this for a customer project.

Prerequisites

I assume that you have a working installation of Eclipse with the Android plugin on your computer. If not, please follow this tutorial to get your workspace ready: Download the Android SDK.

The Eclipse/Android part

  • Register for an account at the Smaad website and download the Smaad JAR file.
  • Create a new Android project. Make sure that the namespace (the package name) is identical to the namespace of your Unity Android project. You can set the namespace in Unity through Build Settings > Player Settings > Android tab > Other Settings > „Bundle Identifier“. In my example I am using my.android.game.
  • Copy the Smaad JAR file (the name of it might be MoreApps.jar) into the /libs folder of the project (you might have to create that folder manually).
  • Search inside of your Unity installation directory for the file classes.jar. Copy this file also into the /libs folder of your Eclipse project.
  • To test if the ads are being displayed we first create a regular Android class called SmaadActivity.java. The content of this class looks like this:

    <br />
    package my.android.game;</p>
    <p>import jp.gmotech.MoreApps.MoreAppsActivity;<br />
    import android.app.Activity;<br />
    import android.content.Intent;<br />
    import android.os.Bundle;</p>
    <p>public class SmaadActivity extends Activity {</p>
    <p>	@Override<br />
    	public void onCreate(Bundle savedInstanceState) {<br />
    		super.onCreate(savedInstanceState);</p>
    <p>		Intent i = new Intent(getApplication(), MoreAppsActivity.class);<br />
    		i.putExtra("MoreAppsZoneId", "11111111"); // YOUR ZONEID HERE<br />
    		startActivity(i);<br />
    	}</p>
    <p>}<br />
    

    Make sure to change the value of „MoreAppsZoneId“ to your own ZoneId you received from the Smaad website.

  • Update the AndroidManifest.xml file that it looks like this:

    <br />
    <?xml version="1.0" encoding="utf-8"?><br />
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="my.android.game"
        android:versionCode="1"
        android:versionName="1.0" ></p>
    <p>    <uses-sdk android:minSdkVersion="7" /></p>
    <p>    <uses-permission android:name="android.permission.INTERNET" /><br />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><br />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /></p>
    <p>    <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" ></p>
    <p>        <activity
                android:name="my.android.game.SmaadActivity"
                android:label="@string/app_name" ><br />
                <intent-filter><br />
                    <action android:name="android.intent.action.MAIN" /></p>
    <p>                <category android:name="android.intent.category.LAUNCHER" /><br />
                </intent-filter><br />
            </activity><br />
            <activity
                android:name="jp.gmotech.MoreApps.MoreAppsActivity"
                android:configChanges="orientation"
                android:theme="@android:style/Theme.NoTitleBar" ><br />
                <intent-filter><br />
                    <action android:name="android.intent.action.VIEW" /><br />
                    <category android:name="android.intent.category.DEFAULT" /><br />
                    <category android:name="android.intent.category.BROWSABLE" /><br />
                </intent-filter><br />
            </activity></p>
    <p>		<service android:name="jp.gmotech.MoreApps.MoreAppsIntentService" /><br />
        </application></p>
    <p></manifest><br />
    

    It’s important that you also add the activity „jp.gmotech.MoreApps.MoreAppsActivity“ to the manifest file since this is being used by Smaad.

  • Build and run the app on your Android phone and you should see the Smaad „more apps“ being displayed. If you don’t see this please refer to the official Smaad tutorials on how to implement this view into the Android app. Once this works continue with this tutorial.
  • Create a class called SmaadUnityActivity.java. You can use your previously created SmaadActivity.java class as a base. The class will look like this:

    <br />
    package my.android.game;</p>
    <p>import jp.gmotech.MoreApps.MoreAppsActivity;<br />
    import android.content.Intent;<br />
    import android.os.Bundle;</p>
    <p>import com.unity3d.player.UnityPlayer;<br />
    import com.unity3d.player.UnityPlayerActivity;</p>
    <p>public class SmaadUnityActivity extends UnityPlayerActivity {</p>
    <p>	@Override<br />
    	public void onCreate(Bundle savedInstanceState) {<br />
    		super.onCreate(savedInstanceState);<br />
    	}</p>
    <p>	public static void showSmaadMoreGamesScreen() {<br />
    		UnityPlayer.currentActivity.runOnUiThread(new Runnable() {<br />
    			public void run() {<br />
    				Intent intent = new Intent(UnityPlayer.currentActivity.getApplicationContext(), MoreAppsActivity.class);<br />
    				intent.putExtra("MoreAppsZoneId", "11111111"); // YOUR ZONEID HERE<br />
    				UnityPlayer.currentActivity.startActivity(intent);<br />
    			}</p>
    <p>		});<br />
    	}</p>
    <p>}<br />
    

    Make sure to change the value of „MoreAppsZoneId“ to your own ZoneId you received from the Smaad website. This class extends UnityPlayerActivity instead of Activity in order to make it work inside Unity. Also we created the static function showSmaadMoreGamesScreen() and left the onCreate() function nearly empty. To make it work inside Unity we have to wrap the whole content of the function showSmaadMoreGamesScreen() into these lines of code:

    <br />
    UnityPlayer.currentActivity.runOnUiThread(new Runnable() {<br />
    	public void run() {<br />
    		...<br />
    	}<br />
    }<br />
    

    If we don’t do this we would get an error or even a crash in Unity when we call this function. Also some content inside that function is a bit different in order to make it work with Unity. Please have a careful look at the above code.

  • Not entirely sure if the following step is needed but just do it anyways: Add this line to the AndroidManifest.xml file inside the „application“ tags:
    <br />
    <activity android:name="my.android.game.SmaadUnityActivity" android:screenOrientation="portrait" ></activity><br />
    
  • Export the project to a JAR file. Click the right mouse button in the package explorer in Eclipse on your project and choose Export… > Java/JAR File > (standard settings) enter a name for the JAR file > Finish

Now you’re done with the part in Eclipse. Next we have to add the created plugin into Unity.

The Unity part

  • Copy the created JAR file inside your Unity Android project into the folder /Plugins/Android/
  • Also copy the Smaad JAR (MoreApps.jar) file into the same folder /Plugins/Android/
  • Complete the AndroidManifest.xml file inside your Unity Android project located at: \Assets\Plugins\Android\AndroidManifest.xml. The content of this file will look like this:
    <br />
    <?xml version="1.0" encoding="utf-8"?><br />
    <manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="my.android.game"
    	android:installLocation="preferExternal"
        android:versionCode="1"
        android:versionName="1.0"><br />
        <supports-screens
            android:smallScreens="true"
            android:normalScreens="true"
            android:largeScreens="true"
            android:xlargeScreens="true"
            android:anyDensity="true"/></p>
    <p>    <application
    		android:icon="@drawable/app_icon"
            android:label="@string/app_name"
            android:debuggable="true"></p>
    <p>        <activity android:name="com.unity3d.player.UnityPlayerProxyActivity"
                      android:label="@string/app_name"
                      android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"><br />
            </activity><br />
            <activity android:name="com.unity3d.player.UnityPlayerActivity"
                      android:label="@string/app_name"
                      android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"><br />
            </activity><br />
            <activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
                      android:label="@string/app_name"
                      android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"><br />
                <meta-data android:name="android.app.lib_name" android:value="unity" /><br />
                <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" /><br />
            </activity><br />
            <activity android:name="com.unity3d.player.VideoPlayer"
                      android:label="@string/app_name"
                      android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"><br />
            </activity>	</p>
    <p>        <activity android:name="my.android.game.SmaadUnityActivity"></activity></p>
    <p>		<activity android:name="jp.gmotech.MoreApps.MoreAppsActivity" android:configChanges="orientation" android:theme="@android:style/Theme.NoTitleBar" ><br />
                <intent-filter><br />
                    <action android:name="android.intent.action.VIEW" /><br />
                    <category android:name="android.intent.category.DEFAULT" /><br />
                    <category android:name="android.intent.category.BROWSABLE" /><br />
                </intent-filter><br />
            </activity></p>
    <p>        <service android:name="jp.gmotech.MoreApps.MoreAppsIntentService" /><br />
        </application></p>
    <p>	<!-- PERMISSIONS --><br />
    	<uses-permission android:name="android.permission.INTERNET"></uses-permission><br />
    	<uses-permission android:name="android.permission.GET_TASKS"></uses-permission><br />
    	<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission><br />
    	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><br />
    	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission></p>
    <p></manifest></p>
    <p>

    Make sure the package name (namespace) is correct and identical to the namespace in your Eclipse project. Note that if you are already using other Android plugins this manifest file might be looking a bit different than in my example.

  • Finally we need to create a C# script in Unity to access that Android plugin:
    <br />
    using UnityEngine;<br />
    using System.Collections;<br />
    using System.Collections.Generic;<br />
    using System.Runtime.InteropServices;<br />
    using System;</p>
    <p>public class Startup : MonoBehaviour<br />
    {<br />
    	public static AndroidJavaClass smaadJavaClass;</p>
    <p>	void Start() {<br />
    		if( Application.platform == RuntimePlatform.Android ) {<br />
    			// Initialize Smaad<br />
    			smaadJavaClass = new AndroidJavaClass("my.android.game.SmaadUnityActivity");<br />
    		}<br />
    	}</p>
    <p>	void OnGUI () {<br />
    		if( Application.platform == RuntimePlatform.Android ) {<br />
    			if(GUI.Button(new Rect(10, 300, 150, 120), "Show SMAAD Screen")){<br />
    				smaadJavaClass.CallStatic("showSmaadMoreGamesScreen");<br />
    			}<br />
    		}<br />
    	}<br />
    }<br />
    

    Smaad SDK "More Games" activity

    For testing purposes we created a button inside the OnGUI() Unity function. If you press that button the Smaad Activity will be displayed. Note that this code will only be executed on an actual Android device and the button will not show up in the Unity player! „showSmaadMoreGamesScreen“ has to have the same name like the function you were using inside your Eclipse project.

In the end the screen will look similar like in this screenshot on the right side.

Well that’s all! I hope the tutorial helped you getting Smaad working in your game! Please leave your comments and questions below.

About Dominik
Currently working as an Android/iOS developer for Cineman, Switzerland. Owner of http://www.ukon.ch and https://play.google.com/store/apps/details?id=com.cheatdatabase

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.