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

标签 android unity3d admob google-play-services

正在关注 this tutorial我开始致力于将 AdMob 集成到 Unity 中,但由于它使用的是旧版本,我不得不进行一些更改以将其更新为 Google Play 服务版本,但我认为我可能搞砸了。

我将所有必需的库导入到 Unity 3D 的插件/Android 和 Eclipse 的 Java 构建路径以及订购和导出中。

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

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

<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 的插件文件夹中的 list 用于覆盖 Unity 构建的 list (注意: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>

最佳答案

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

我的是 Assets/plugins/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>

关于android - 通过 Google Play 服务将 AdMob 与 Unity 3D 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21805003/

相关文章:

java - 从 Parse.com localDatastore 计算列(数字类型)的总和时遇到问题?

android - 如何解决使用 multidex 和 Google Analytics 时应用程序崩溃的问题?

c# - 在 c# 中成为 unity 3d 对象的子对象

ios - 仅将 AdMob 用于自家广告

java - JSON/Java/Twilio 响应 : "No ' To' number is specified"when request sent from JSON

android - Facebook 延迟深度链接与 FB Unity3D SDK 设置

android - GvrViewerMain 自己旋转相机。 Unity3D + 谷歌VR

android - Admobs 需要我已经拥有的权限

android - 测试奖励广告未显示 - 广告无法加载 : LoadAdError(code: 3, 域 : com. google.android.gms.ads,消息 : No ad config.

android - 在没有选择的情况下滚动到 AlertDialog 中的列表项