Unity 3D: Displaying a Fluct (Zucks AdNet) banner in a Unity Android game

Tutorial how to display an Fluct (Zucks AdNet) banner in an existing Unity Android project scene

Seems like using a simple Android JAR files inside a Unity Android project is not such a simple thing to do. I finally managed to get Zucks AdNet working in an existing Unity Android game. For this example I was using Unity for Windows version 3.5.2 and the latest Android plugin for Eclipse.

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 Fluct website and download the ZucksAdnetSDK.jar file. In my example I am using version 1.1.3 of Fluct/ZucksAdnet.
  • 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 ZucksAdnetSDK.jar file 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.
  • Create a class called ZucksUnityActivity.java. The class will look like this:

    package my.android.game;
    
    import net.zucks.zucksAdnet.sdk.ZucksAdnetUserInfo;
    import net.zucks.zucksAdnet.sdk.ZucksAdnetView;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.LinearLayout;
    
    import com.unity3d.player.UnityPlayer;
    import com.unity3d.player.UnityPlayerActivity;
    
    public class ZucksUnityActivity extends UnityPlayerActivity {
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    	}
    
    	public static void showBanner() {
    
    		UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
    			public void run() {
    
    				ZucksAdnetUserInfo userInfo = ZucksAdnetUserInfo.getInstance(UnityPlayer.currentActivity.getApplicationContext());
    				userInfo.setGender(0); // 0 = male, 1 = female
    				userInfo.setAge(28);
    
    				// And this is the same, but done programmatically
    				LinearLayout layout = new LinearLayout(UnityPlayer.currentActivity.getApplicationContext());
    				layout.setOrientation(LinearLayout.VERTICAL);
    				
    				// DEFINE HERE IF THE BANNER SHALL BE DISPLAYED ON TOP OR ON BOTTOM OF THE SCREEN
    				layout.setGravity(Gravity.BOTTOM);
    				UnityPlayer.currentActivity.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    
    				ZucksAdnetView zucksView = new ZucksAdnetView(UnityPlayer.currentActivity);
    				layout.addView(zucksView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    			}
    
    		});
    
    	}
    
    }
    

    It’s important that you are using the ZucksAdnetUserInfo object. When I tried it without using this class the banner did not show up. The class extends UnityPlayerActivity instead of Activity. Also we created the static function showBanner() and left the onCreate() function nearly empty. Also we have to wrap the whole content of this function into

    UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
    	public void run() {
    		...
    	}
    }
    

    otherwise we would get an error or a crash in Unity when we call the function. Also some content inside that function is a bit different in order to make it work with Unity.

  • 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. Now we have to add that plugin into Unity.

The Unity part

  • Copy the created JAR file inside your Unity Android project into the folder /Plugins/Android/
  • Also copy the ZucksAdnetSDK.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:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="my.android.game"
    	android:installLocation="preferExternal"
        android:versionCode="1"
        android:versionName="1.0">
    	
        <supports-screens
            android:smallScreens="true"
            android:normalScreens="true"
            android:largeScreens="true"
            android:xlargeScreens="true"
            android:anyDensity="true"/>
    
        <application
    		android:icon="@drawable/app_icon"
            android:label="@string/app_name"
            android:debuggable="true">
    		
    		<!-- FLUCT/ZUCKS SDK -->
    		<meta-data android:name="MEDIA_ID" android:value="000000999"  />
    						
            <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">
            </activity>
            <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">
            </activity>
            <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">
                <meta-data android:name="android.app.lib_name" android:value="unity" />
                <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
            </activity>
            <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">
            </activity>	
    
    		<activity android:name="my.android.game.ZucksUnityActivity"></activity>	
        </application>
    
    	<!-- PERMISSIONS -->
    	<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    	<uses-permission android:name="android.permission.GET_TASKS"></uses-permission>
    	<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    	
    </manifest>
    

    Replace the value of MEDIA_ID in the manifest file with the ID you received on the Fluct website when you registered. Also 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.

  • To finally display the ad banner inside a scene of your Unity Android game create or modify a C# script with the following content:
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System;
    
    public class Startup : MonoBehaviour
    {
    	public static AndroidJavaClass ZucksAdnetSDKJavaClass;
    	
    	void Start() {
    		
    		if( Application.platform == RuntimePlatform.Android ) {
    			// Initialize Fluct/ZucksAdnetSDK
    			ZucksAdnetSDKJavaClass = new AndroidJavaClass("my.android.game.ZucksUnityActivity");
    			
    			// ZucksAdnetSDK display banner
    			ZucksAdnetSDKJavaClass.CallStatic("showBanner");
    		}
    		
    	}
    }
    

    The banner will now be displayed either at the top or bottom of your phone’s screen depending on what you defined in the source code in the Eclipse project. Note that this code will only be executed on an actual Android device and the banner will not show up in the Unity player! „showBanner“ has to be identical to the function name you were using inside your Eclipse project.

  • If the Unity Android game crashes when you are trying to display a Fluct banner check the error log. If the error has anything to do with a missing database table „tbl_user_info“ then open the ZucksAdnetSDK.jar file with WinZip or WinRAR and copy all the *.sql files from the /assets folder to the /Plugins/Android/assets folder in your Unity Android project and try again.

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.

One Response to Unity 3D: Displaying a Fluct (Zucks AdNet) banner in a Unity Android game

  1. radar sagt:

    Hi ENATURE,

    I’ve try to create an Android Button written in Java and call to show and tap it from Unity.

    Having read a lot of post from your post I could successfully create button in Unity, But can not touch it. Which means any touch on that button make no sense.

    I’ve tried every way I could do but still failed.

    I go through your site but can’t find your email address or more properble connections.

    So I post my code project to the Unity community
    http://forum.unity3d.com/threads/145137-Can-not-touch-Android-Button

    Could you please make a help?

Schreibe einen Kommentar