Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating AdMob through Google Play Services with Unity 3D

Following this tutorial I started working on integrating AdMob into Unity, but as this uses an older version I had to make some changes to update it to the Google Play Services version, but I think I may have messed up.

I imported all the required libraries into both Unity 3D's Plugins/Android and Eclipse's Java Build Path and Order and Export.

Every time I call the script in Unity the game crashes and LogCat says it had an error with a tag "AndroidRuntime" "at admob.admob$1.run(admob.java:52) which is this line

adView.loadAd(adRequest);

Here's my AdMobController.js from Unity:

#pragma strict

public class AdMobController extends MonoBehaviour {

    private static var s_Controller : AdMobController;
    private static var jo:AndroidJavaObject;

    function Awake()
    {
        s_Controller = this;
        #if UNITY_ANDROID
        jo = new AndroidJavaObject("admob.admob");
        #endif
    }
}

File admob.java

package admob;

import com.google.android.gms.ads.*;
import com.unity3d.player.UnityPlayer;

import android.app.Activity;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class admob{

    //  private String pubID = "ADMOB_ID_HERE"; //Your public AdMob ID. Make sure this is correct, or you won't get any credit for the Ad.
    private Activity activity; //Store the android main activity
    private AdView adView; //The AdView we will display to the user
    private LinearLayout layout; //The layout the AdView will sit on

    //Constructor
    public admob() {
        activity = UnityPlayer.currentActivity;

        activity.runOnUiThread(new Runnable() {
            public void run() {

                /*
                    layout = new LinearLayout(activity);

                    activity.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    adView = new AdView(activity);
                    adView.setAdSize(AdSize.SMART_BANNER);
                    adView.setAdUnitId(pubID);
                    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
                    adView.loadAd(adRequestBuilder.build());
                */

                layout = new LinearLayout(activity);

                // Create the adView.
                adView = new AdView(activity);
                adView.setAdUnitId("MY_AD_ID"); //Yes, I put the correct ID here. I only deleted it before posting here.
                adView.setAdSize(AdSize.BANNER);

                // Add the adView to it.
                layout.addView(adView);

                activity.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)       // Emulator
                    .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // Test phone. Not my actual phone because I could
                                                                       // not call AdRequest to get the ID for it.
                    .build();

                // Load the adView with the ad request.
                Log.d("unity321", "unity321");
                adView.loadAd(adRequest);

                layout.addView(adView.getRootView(), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                // Code in here will run on the UI thread of this application
            }
        });
    }
}

AdMob manifest from Eclipse:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.admob.admob"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
                   android:value="google_play_services_version"/>
        <activity android:name="com.google.android.gms.ads.AdActivity"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

And finally the manifest in the plugins folder in Unity used to override the Unity built manifest (NOTE: android:value="google_play_services_version" is changed because with the @integer/ both Unity and Eclipse said no value was found and would not export)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.TESTCOMPANY.TESTAPP"
          android:theme="@android:style/Theme.NoTitleBar"
          android:versionName="1.0"
          android:versionCode="1"
          android:installLocation="auto">
    <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">
         <activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
                   android:label="@string/app_name"
                   android:screenOrientation="sensorLandscape"
                   android:launchMode="singleTask"
                   android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
             <meta-data android:name="unityplayer.UnityActivity"
                        android:value="true" />
             <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik"
                        android:value="true" />
             <meta-data android:name="com.google.android.gms.version"
                        android:value="google_play_services_version"/>
         </activity>
         <activity android:name="com.google.android.gms.ads.AdActivity"
                   android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
    <uses-sdk android:minSdkVersion="9"
              android:targetSdkVersion="19" />
    <uses-feature android:glEsVersion="0x00020000" />
    <uses-feature android:name="android.hardware.sensor.accelerometer" />
    <uses-feature android:name="android.hardware.touchscreen" />
    <uses-feature android:name="android.hardware.touchscreen.multitouch"
                  android:required="false" />
    <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct"
                  android:required="false" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
like image 566
Armavir Avatar asked Nov 10 '22 12:11

Armavir


1 Answers

Yep, this is what I did. Make another folder in unity; where your plugins are.

Mine is Assets/plugins/android/ You need Assets/plugins/android/res/values

In the values folder save this in an xml file named values.xml

 <?xml version="1.0" encoding="UTF-8"?>

-<resources>

<integer name="google_play_services_version">4132500</integer>

</resources>
like image 71
Dblfstr Avatar answered Nov 14 '22 23:11

Dblfstr