Simple Admob android for unity3d [在Android中添加广告]

Simple Admob android for unity3d

AdMob 是移动电话广告市场,可提供用户在移动电话网络上播放广告。 
先来一张图,嘿嘿


Eclipse 部分:
1、下载googleadmobadssdkandroidGoogle admob for android SDK,现在官网的最新sdk版本是GoogleAdMobAdsSdk-6.2.1,我们需要的是GoogleAdMobAdsSdk-6.2.1.jar这个jar包。
2、eclipse里新建一个android工程,把GoogleAdMobAdsSdk-6.2.1.jar复制到libs里面,在C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androiddevelopmentplayer\bin下找到classes.jarwin下)并复制到新建的android工程下的libs里,
3、classes.jarGoogleAdMobAdsSdk-6.2.1.jar添加的buildpath,直接右键 /BuildPath/Add to Build Path
4、哈哈,现在环境配置好了,开始写咧,新建一个java文件包名com.android.plugin,文件名AriAndroidPluginActivity并继承自UnityPlayerActivity


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.android.plugin;
 
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.google.ads.AdRequest.ErrorCode;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import android.os.Bundle;
 
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
 
public class AriAndroidPluginActivity extends UnityPlayerActivity {
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     }
 
     private static void setupAds() {
         UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
 
             @Override
             public void run() {
                 // TODO Auto-generated method stub
                 LinearLayout layout = new LinearLayout(
                         UnityPlayer.currentActivity.getApplicationContext());
                 layout.setOrientation(LinearLayout.VERTICAL);
                 layout.setGravity(Gravity.BOTTOM);
                 UnityPlayer.currentActivity.addContentView(layout,
                         new LayoutParams(LayoutParams.FILL_PARENT,
                                 LayoutParams.FILL_PARENT));
 
                 AdView adView = new AdView(UnityPlayer.currentActivity,
                         AdSize.BANNER, "a1510c9070b657b");
                 layout.addView(adView, new LayoutParams(
                         LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
 
                 adView.setBackgroundColor(0xff000000);
                 adView.setAdListener(new AdListener() {
                     @Override
                     public void onReceiveAd(Ad arg0) {
                         // TODO Auto-generated method stub
                         Log.i("admob ari", "onReceiveAd");
                     }
 
                     @Override
                     public void onPresentScreen(Ad arg0) {
                         // TODO Auto-generated method stub
                         Log.i("admob ari", "onPresentScreen");
                     }
 
                     @Override
                     public void onLeaveApplication(Ad arg0) {
                         // TODO Auto-generated method stub
                         Log.i("admob ari", "onLeaveApplication");
                     }
 
                     @Override
                     public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
                         // TODO Auto-generated method stub
                         Log.i("admob ari", "onFailedToReceiveAd+" + arg1);
                     }
 
                     @Override
                     public void onDismissScreen(Ad arg0) {
                         // TODO Auto-generated method stub
                         Log.i("admob ari", "onDismissScreen");
                     }
                 });
                 Log.i("admob ari", "adView.loadAd(new AdRequest())");
                 AdRequest adRequest = new AdRequest();
                 adRequest.addTestDevice("DA879575641EA02169070A93B5FE4071");
                 adView.loadAd(adRequest);
 
             }
         });
 
     }
 
}

 
 
哈哈,eclipse部分就大部分完成了

6、选中AriAndroidPluginActivity 右键 export 选择Java/jar file 然后next ,设置导出的位置和文件名  ,C:\Users\dell\Desktop\AriAdmobPlugin.jar;然后finish,这样就OK了,在桌面就会生成一个名字为 AriAdmoPlugin.jarjar

Unity部分:
1、新建一个空unity工程 然后新建目录Plugins\Android,在Android 目录下新建名为binlibs的子文件夹,然后把前面导出的AriAdmoPlugin.jar放在bin目录下,GoogleAdMobAdsSdk-6.2.1.jar放在libs目录下。
2、开始写配置文件了,添加两个activity AriAndroidPluginActivitycom.google.ads.AdActivity还有需要的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
配置文件如下,把这个AndroidManifest.xml放在Android目录下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.android.plugin"
       android:versionCode="1"
       android:versionName="1.0">
       
     <application>
         <activity android:name=".AriAndroidPluginActivity"
                   android:label="@string/app_name">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
   
<!-- AdMobActivity definition -->
         <activity android:name="com.google.ads.AdActivity"
                   android:theme="[url=u.php?uid=3750]@android[/url]  :style/Theme.NoTitleBar.Fullscreen"
                    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<!-- Track Market installs -->
         <receiver android:name="com.admob.android.ads.analytics.InstallReceiver" android:exported="true">
             <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" />
             </intent-filter>
         </receiver>
     </application>
     
        <!-- AdMob SDK requires Internet permission -->
     <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>



2、新建一个C#文件,AdmobManager.cs

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using UnityEngine;
using System.Collections;
 
public class AdmobManager : MonoBehaviour
{
     public static AndroidJavaClass adMobJavaClass;
     // Use this for initialization
     void Start ()
     {
         if(Application.platform == RuntimePlatform.Android){
             adMobJavaClass = new AndroidJavaClass("com.android.plugin.AriAndroidPluginActivity");
             Debug.Log("call admob in unity");
             adMobJavaClass.CallStatic("setupAds");
         }
     }
     
 
     void OnGUI ()
     {
         if (GUI.Button (new Rect (10, 10, 120, 60), "admobTest")) {
//            AriPlugin.StartActivity ("com.android.plugin.AriAndroidPluginActivity");
         }
         if (GUI.Button (new Rect (10, 80, 120, 60), "quit")) {
//            AriPlugin.StartActivity ("com.android.plugin.AriAndroidPluginActivity");
             Application.Quit();
         }
     }
}




3、把AdmobManager.cs挂在任意一个物体上,当游戏启动的时候就会在屏幕的下方看到广告条了,哇哈哈~
googleadmobadssdkandroid.zip



//貌似论坛已经有类似的教程了,管他呢,哇哈哈


http://game.ceeger.com/forum/read.php?tid=9363&fid=2

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值