通过Google Play服务将AdMob与Unity 3D集成

时间:2014-02-15 23:17:00

标签: android unity3d admob google-play-services

关注this tutorial我开始将AdMob集成到Unity中,但由于这使用旧版本,我必须进行一些更改才能将其更新为Google Play服务版本,但我想我可能搞砸了。

我将所有必需的库导入Unity 3D的Plugins / Android和Eclipse的Java Build Path以及Order and Export。

每当我在Unity中调用脚本时,游戏崩溃,LogCat说它在admob.admob $ 1.run(admob.java:52)上有一个带有“AndroidRuntime”标签的错误就是这行

adView.loadAd(adRequest);

这是Unity的AdMobController.js:

#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
    }
}

文件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
            }
        });
    }
}

Eclipse的AdMob清单:

<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>

最后,Unity中插件文件夹中的清单用于覆盖Unity内置清单(注意:android:value =“google_play_services_version”已更改,因为使用@ integer / Unity和Eclipse表示没有找到任何值,也不会出口)

<?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>

2 个答案:

答案 0 :(得分:1)

是的,这就是我的所作所为。 统一制作另一个文件夹;你的插件在哪里。

我的是资产/插件/ android / 你需要Assets / plugins / android / res / values

在values文件夹中将其保存在名为values.xml

的xml文件中
 <?xml version="1.0" encoding="UTF-8"?>

-<resources>

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

</resources>

答案 1 :(得分:0)

将文件夹google_play_services<Android sdk>/extras/google/复制到Assets/Plugins/Android中的Unity文件夹,然后重新构建项目。