[原]Unity Android平台调用Admob
关于android的知识我就不在此说明,不熟悉的朋友可以先去了解一下基础知识。
开发环境:android 4.0、admob sdk 6.1.0、unity3.5、操作系统 win7
开发前需要将 classes.jar admob sdk 6.1.0.jar 拖到android工程 libs下面 (注意:不要通过java bulid path 导入jar包,那样老说找不到 admob的相关库)
如下所示:
本实例没有配置界面的XML,都是代码创建控件
特别说明:unity中bundle Identifier 必须为包名(本例 com.example.testadmob)
先贴代码
package com.example.testadmob; import com.google.ads.AdRequest; import com.google.ads.AdSize; import com.google.ads.AdView; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import com.unity3d.player.UnityPlayer; import com.unity3d.player.UnityPlayerActivity; public class MainActivity extends UnityPlayerActivity { private AdView adView; LinearLayout layout; AdRequest adRequest; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setupAds(); //admob广告的添加 } public void setupAds() { layout = new LinearLayout(this); //创建linearLayout layout.setOrientation(LinearLayout.VERTICAL); //设置成垂直 addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); //layout添加到住View adView = new AdView(this,AdSize.BANNER,"a150547a7a8cfc9"); layout.addView(adView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); adView.setBackgroundColor(0xff000000); adRequest=new AdRequest(); } public static void ShowAds() {//unity 中调用此函数 显示广告 final MainActivity currentActivity = (MainActivity)UnityPlayer.currentActivity; currentActivity.runOnUiThread(new Runnable() { public void run() { currentActivity.showAds(); } }); } public static void HideAds() { final MainActivity currentActivity = (MainActivity)UnityPlayer.currentActivity;//在unity中获取当前主activity currentActivity.runOnUiThread(new Runnable() { public void run() { currentActivity.hideAds(); } }); } public void showAds() { adView.setVisibility(View.VISIBLE); adView.loadAd(adRequest);//请求广告 Log.d("AdListener", "showAds"); } public void hideAds() { adView.setVisibility(View.GONE); Log.d("AdListener", "hideAds"); } }
AndroidManifest.xml 如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testadmob" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> </application> </manifest>
选择工程 导出——》JAR file——》Next——》选择路径——》finish 导出jar包
在unity中新建plugins/Android 文件夹 将android工程下的libs、bin、res 、AndroidManifest.xml拷贝到 Android目录下。 清空bin libs下的内容,将导出的jar包和andmobsdk分别拷贝到两个文件夹下
如图所示:
unity 中调用
private AndroidJavaClass cls_TheActivity; cls_TheActivity = new AndroidJavaClass("com.example.testadmob.MainActivity");//对应android的包名 if(GUI.Button(new Rect(100,300,100,40),"show")) { cls_TheActivity.CallStatic("ShowAds"); } if(GUI.Button(new Rect(200,300,100,40),"hide")) { cls_TheActivity.CallStatic("HideAds"); }