原地址:http://dong2008hong.blog.163.com/blog/static/4696882720140441353482/

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

  • Download the AdMob JAR file and register for an account if you haven’t done so already.
  • 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 AdMob 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.
  • To test if the ads are being displayed we first create a regular Android class calledAdMobActivity.java. The content of this class looks like this:
    先决条件
    我假设你已经装好了Eclipse的Android插件,在您的计算机上安装。如果没有,请按照本教程,让您的工作区准备:下载Android SDK。
  • 在Eclipse/ Android的部分
    下载的AdMob的JAR文件,并注册一个帐号,如果你还没有这样做的话。
    创建一个新的Android项目。请确保您的Unity Android项目的命名空间的命名空间(包名)是相同的。您可以设置统一的命名空间中,通过Build Settings > Player Settings > Android tab > Other Settings > “Bundle Identifier”
  • 在我的例子中,我使用的my.android.game。
    AdMob的JAR文件复制到/libs文件夹中的项目(您可能需要手动创建该文件夹)。
    搜索内统一安装目录中的文件classes.jar的。把这个文件复制到/libs文件夹的Eclipse项目。
    如果要测试所显示的广告,我们首先创建一个普通的Android称为AdMobActivity.java的类。这一类的内容看起来像这样:
  • 以下是代码部分:
  • package my.android.game;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.LinearLayout;
    import com.admob.android.ads.AdManager;
    import com.admob.android.ads.AdView;
    import com.admob.android.ads.SimpleAdListener;
    public class AdMobActivity extends Activity
    {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    Log.i("AdMobTest", "onCreate");
    // super (UnityPlayerActivity) will use setContentView() ...
    super.onCreate(savedInstanceState);
    // ... so instead of using setContentView(), we call addContentView()
    // from setupAds()
    setupAds();
    // Add test devices (check the logcat for the ID string of your
    // device..)
    // If you want to display test-ads uncomment this line of code and replace YOUR_DEVICE_ID with your device ID which AdMob displays in your logfile.
    AdManager.setTestDevices( new String[] { "YOUR_DEVICE_ID" } );
    }
    private void setupAds() {
    // And this is the same, but done programmatically
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setGravity(Gravity.BOTTOM);
    addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    AdView adView = new AdView(this);
    layout.addView(adView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    adView.setBackgroundColor(0xff000000);
    adView.setPrimaryTextColor(0xffffffff);
    adView.setSecondaryTextColor(0xffcccccc);
    adView.setKeywords("Android game");
    adView.setRequestInterval(15);
    // add listener for easier debugging
    adView.setAdListener(new SimpleAdListener() {
    @Override
    public void onFailedToReceiveAd(com.admob.android.ads.AdView adView) {
    Log.d("AdListener", "onFailedToReceiveAd: " + adView.toString());
    super.onFailedToReceiveAd(adView);
    }
    @Override
    public void onFailedToReceiveRefreshedAd(com.admob.android.ads.AdView adView) {
    Log.d("AdListener", "onFailedToReceiveRefreshedAd: " + adView.toString());
    super.onFailedToReceiveRefreshedAd(adView);
    }
    @Override
    public void onReceiveAd(com.admob.android.ads.AdView adView) {
    Log.d("AdListener", "onReceiveAd: " + adView.toString());
    super.onReceiveAd(adView);
    }
    @Override
    public void onReceiveRefreshedAd(com.admob.android.ads.AdView adView) {
    Log.d("AdListener", "onReceiveRefreshedAd: " + adView.toString());
    super.onReceiveRefreshedAd(adView);
    }
    });
    adView.requestFreshAd();
    }
    }
  • Update the AndroidManifest.xml file in your Eclipse project. It should look like this:
    更新AndroidManifest.xml 文件,像以下这样
  • <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.android.game"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
    android:name="my.android.game.AdMobActivity"
    android:label="@string/app_name" >
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <!-- The application's publisher ID assigned by AdMob -->
    <meta-data android:value="YOUR_PUBLISHER_ID" android:name="ADMOB_PUBLISHER_ID" />
    <!-- AdMobActivity definition -->
    <activity android:name="com.admob.android.ads.AdMobActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="orientation|keyboard|keyboardHidden" />
    </application>
    </manifest>

    Make sure to replace YOUR_PUBLISHER_ID with your actual AdMob publisher ID.

  • 将其中的YOUR_PUBLISHER_ID 替换成你的Admob发布商ID

  • Build and run the app on your Android phone and you should see the AdMob banner being displayed. If you don’t please refer to the AdMob tutorials how to implement the banner into the Android app. Once this works continue with this tutorial.
  • 如果你没看到Admob的广告条,请按照Admob的官网教程去查看你的Admob配置步骤是否正确。
  • Create a class called AdMobUnityActivity.java. You can use your previously created AdMobActivity.java class as a base. The class will look like this:
    package my.android.game;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.LinearLayout;
    import com.admob.android.ads.AdManager;
    import com.admob.android.ads.AdView;
    import com.admob.android.ads.SimpleAdListener;
    import com.unity3d.player.UnityPlayer;
    import com.unity3d.player.UnityPlayerActivity;
    public class AdMobUnityActivity extends UnityPlayerActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }
    public static void setupAdsStatic() {
    UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
    public void run() {
    // If you want to display test-ads uncomment this line of code and replace YOUR_DEVICE_ID with your device ID which AdMob displays in your logfile.
    // AdManager.setTestDevices(new String[] { "YOUR_DEVICE_ID" });
    // And this is the same, but done programmatically
    LinearLayout layout = new LinearLayout(UnityPlayer.currentActivity.getApplicationContext());
    layout.setOrientation(LinearLayout.VERTICAL); // SET HERE IF YOU WANT THE BANNER TO BE DISPLAYED AT THE TOP OR BOTTOM OF THE SCREEN
    layout.setGravity(Gravity.BOTTOM);
    UnityPlayer.currentActivity.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    AdView adView = new AdView(UnityPlayer.currentActivity);
    layout.addView(adView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    adView.setBackgroundColor(0xff000000);
    adView.setPrimaryTextColor(0xffffffff);
    adView.setSecondaryTextColor(0xffcccccc); // SET SOME KEYWORDS FOR THE ADS TO DISPLAY
    adView.setKeywords("Android game");
    adView.setRequestInterval(15);
    // add listener for easier debugging
    adView.setAdListener(new SimpleAdListener() {
    @Override
    public void onFailedToReceiveAd(com.admob.android.ads.AdView adView) {
    Log.d("AdListener", "onFailedToReceiveAd: " + adView.toString());
    super.onFailedToReceiveAd(adView);
    }
    @Override
    public void onFailedToReceiveRefreshedAd(com.admob.android.ads.AdView adView) {
    Log.d("AdListener", "onFailedToReceiveRefreshedAd: " + adView.toString());
    super.onFailedToReceiveRefreshedAd(adView);
    }
    @Override
    public void onReceiveAd(com.admob.android.ads.AdView adView) {
    Log.d("AdListener", "onReceiveAd: " + adView.toString());
    super.onReceiveAd(adView);
    }
    @Override
    public void onReceiveRefreshedAd(com.admob.android.ads.AdView adView) {
    Log.d("AdListener", "onReceiveRefreshedAd: " + adView.toString());
    super.onReceiveRefreshedAd(adView);
    }
    });
    adView.requestFreshAd();
    }
    });
    }
    }

    The class now extends UnityPlayerActivity instead of Activity. Also we created the static function setupAdsStatic() and left the onCreate() function nearly empty. Also we have to wrap the whole content of this function into

    这个类继承了UnityPlayerActivity 。我们将创建一个静态方法setupAdsStatic().
  • 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.

  • 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:
    重新配置AndroidManifest.xml 文件,增加一个activity标签如下:
  • <activity android:name="my.android.game.AdMobUnityActivity"></activity>
    
  • 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 3D.

The Unity part

  • Copy the created JAR file inside your Unity Android project into the folder/Plugins/Android/
  • Also copy the AdMob 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"> <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.AdMobUnityActivity"></activity> <!-- The application's publisher ID assigned by AdMob -->
    <meta-data android:value="YOUR_PUBLISHER_ID" android:name="ADMOB_PUBLISHER_ID" />
    <!-- AdMobActivity definition -->
    <activity android:name="com.admob.android.ads.AdMobActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="orientation|keyboard|keyboardHidden" />
    </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>

    Make sure the package name (namespace) is correct and identical to the namespace in your Eclipse project. Also change the YOUR_PUBLISHER_ID value with the value of your actual AdMob publisher ID. 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 adMobJavaClass; void Start() { if( Application.platform == RuntimePlatform.Android ) {
    // Initialize AdMob
    adMobJavaClass = new AndroidJavaClass("my.android.game.AdMobUnityActivity"); // AdMob display banner
    adMobJavaClass.CallStatic("setupAdsStatic");
    } }
    }

Unity3d Android程序嵌入Admob广告条的更多相关文章

  1. 为免费app嵌入Admob广告

    为免费app嵌入Admob广告,进而获得广告收入. 1.http://www.admob.com/注册一个帐号, 添加Add Mobile Site/app,输入相关信息后,提交完成, 下载Andro ...

  2. 使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB)

    使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB) http://www.cnblogs.com/mrkelly/p/4015245.html 以往调试Androi ...

  3. Unity3d开发集成Google Admob广告增加收入

    在Unity游戏中植入广告是Unity 游戏产品增加收入的一种重要方式,常用的广告有谷歌Admob,百度ssp,腾讯广点通,unity公司的unityads等等,而使用的最多的应该属于谷歌Admob, ...

  4. android自定义控件之滚动广告条

    在一些电子商务网站上经常能够看到一些滚动的广告条,许多软件在首次使用时也有类似的广告条,如图: 其实在github上有实现这种效果的控件,不过这东西做起来也是很简单,我们今天就来看看该怎么做. 先来看 ...

  5. Android -- 仿淘宝广告条滚动

    1,在赶项目的时候我们经常会实现下面这个功能,及添加滚动条广告广播,先看一下淘宝的效果 2,这次实现效果主要使用Android自带的ViewFlipper控件,先来看一下我们的它的基本属性和基本方法吧 ...

  6. 自定义控件(视图)2期笔记03:自定义控件之使用系统控件(优酷案例之广告条Viewpager)

    1.首先我们看看运行效果,如下: 2. 下面就是详细实现这个效果的过程: (1)新建一个Android工程,命名为"广告条的效果",如下: (2)这里用到一个控件ViewPager ...

  7. 【Unity与Android】02-在Unity导出的Android工程中接入Google Admob广告

    我在上一篇文章 [Unity与Android]01-Unity与Android交互通信的简易实现) 中介绍了Unity与Android通讯的基本方法. 这一篇开始进入应用阶段,这次要介绍的是如何在An ...

  8. Android中使用ViewPager实现广告条

    我们在使用电商或视频的手机客户端时,通常会看到广告条的效果.在网上搜索时才知道使用的是ViewPager,于是自己也做了一个Demo. 以下是效果图: 图中包括背景图片.文字描述以及白点. 其中Vie ...

  9. Android Google AdMob 广告接入示例

    Android Google AdMob 广告接入示例 [TOC] 首先请大家放心,虽然 Google搜索等服务被qiang了,但是 广告服务国内还是可以用的,真是普天同庆啊~~~噗! 其实这篇文章也 ...

随机推荐

  1. php面向对象的多态

    多态是指使用类的上下文来重新定义或改变类的性质或行为,或者说接口的多种不同的实现方式即为多态.把不同的子类对象都当成父类来看,可以屏蔽不同子类对象之间的差异,写出通用的代码,做出通用的编程,以适应需要 ...

  2. Coarse-Grained lock 粗粒度锁

    用一个锁Lock一组相关的对象 有时,需要按组来修改多个对象. 这样,在需要锁住其中一个的时候,必须连带地将其他的对象都上锁. 为每一个对象都加上一个锁是很繁琐的. 粗粒度锁是覆盖多个对象的单个锁. ...

  3. [javascript|基本概念|Underfined]学习笔记

    Underfined类型的值:underfined(只有一个) 1/声明未初始化 e.g.:var msg;-->msg == underfined:true 2/申明并值初始化为underfi ...

  4. 杭电ACM2057--A + B Again

    这是题目 A + B Again 这是源代码: #include <stdio.h> int main() { __int64 a,b; while (scanf("%I64X ...

  5. The Name/Origin of Country names

    1.Puerto Rico (Spanish for "Rich Port"/富裕的港口/富港 ) 2.HongKong(Chinese for "香港")

  6. FTP 数字代码的意义

    110 重新启动标记应答. 120 服务在多久时间内ready. 125 数据链路埠开启,准备传送. 150 文件状态正常,开启数据连接端口. 200 命令执行成功. 202 命令执行失败. 211 ...

  7. 下载服务器dll文件并动态加载

    1.新加一个类库 namespace ClassLibrary1 { public class Class1 { public int Add(int a, int b) { return a + b ...

  8. mercurial(hg)使用

    # 版本管理软件的比较 svn 每个目录下建一个.svn目录实在是不爽. git 分支管理非常方便,但没感觉有什么用,主要还是在修改前提交一次代码, 等后悔时再回来,没什么其他的目的.关键是中文乱码问 ...

  9. Perl 关于 use strict 的用法

    什么场合要用 use strict 当你的程序有一定的行数时,尤其是在一页放不下时,或者是你找不到发生错误的原因时. 为什么要用 use strict? 众多的原因之一是帮你寻找因为错误拼写造成的错误 ...

  10. Shell 总结

    find: –name 'filenme' * ? [] ; –iname; –regex PATTERN; –user username; –group; –uid; –gid; –nouser; ...