最近想接入一款广告,百度搜了下最后选定了有米广告,最主要因为它提款及时,还有就是它有unity3d版本,不过遗憾的是现在没有IOS版本。至于接入的步骤在官方网站里面已经说的很清楚了http://www.youmi.net/page/download/sdk,大家可以自己前往查阅,安卓版本有些功能未实现,所以我在它的基础上增加了一些功能,并使sdk更加方便的在uniity3d使用。
1、增加了设置unity3d的对象名称
<pre name="code" class="java">String unityGoName="Main Camera";
//设置unity回调类名
public void setGameObject(String name){
unityGoName=name;
}
ps:本来将有米的key和secret也用unity3d回调设置,发现导致广告弹不出来了所以后来还是在java中设置,所以sdk没法做到通用,希望官方能支持这方面,让u3d开发者可以不用管java发布,就像sharesdk一样。
2、发送消息给unity3d(回调注册对象名称和方法名称,看参数只能支持一个sring类型的参数)
//发送消息到unity
public void sendMessageToUnity(String handle,String arg){
UnityPlayer.UnitySendMessage(unityGoName, handle,arg);
}
3、增加了closeBanner关闭广告条的方法
//关闭横条广告
public void closeBanner(){
mHandler.post(new Runnable() {
@Override
public void run() {
if(mAdView!=null){
mWindowManager.removeView(layout);
mAdView.setEnabled(false);
layout=null;
mAdView=null;
mHandler.removeCallbacks(mBannerRun);
sendMessageToUnity("BannerResult","close");
}
}
});
}
ps:mWindowManager、layout、mBannerRun是全局声明
WindowManager mWindowManager;
LinearLayout layout;
Runnable mBannerRun;
/**
* 实例化无积分Banner并且将其加入到游戏界面中 --可以在Unity3d中直接调用
*/
public void showBanner() {
//showTipsInUiThread("执行打开广告:"+mAdView, Toast.LENGTH_SHORT);
if (mAdView == null) {
mBannerRun= new Runnable() {
@Override
public void run() {
// 实例化广告条
mAdView = new AdView(YouMiActivity.this, AdSize.SIZE_320x50);
mAdView.setAdListener(new AdViewListener() {
@Override
public void onSwitchedAd(AdView arg0) {
//showTipsInUiThread("广告条切换广告了", Toast.LENGTH_SHORT);
sendMessageToUnity("BannerResult","switch");
}
@Override
public void onReceivedAd(AdView arg0) {
//showTipsInUiThread("广告条接收到广告了", Toast.LENGTH_SHORT);
sendMessageToUnity("BannerResult","recieve");
}
@Override
public void onFailedToReceivedAd(AdView arg0) {
//showTipsInUiThread("广告条展示失败", Toast.LENGTH_SHORT);
sendMessageToUnity("BannerResult","fail");
}
});
// 创建布局来承载广告条
layout = new LinearLayout(YouMiActivity.this);
layout.addView(mAdView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
// 采用WindowManager来进行
mWindowManager = (WindowManager) YouMiActivity.this
.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams mWmParams = new WindowManager.LayoutParams();
mWmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mWmParams.height = LayoutParams.WRAP_CONTENT;
mWmParams.width = LayoutParams.WRAP_CONTENT;
mWmParams.alpha = 1.0F;
mWmParams.format = 1;
mWmParams.gravity = Gravity.TOP;
mWindowManager.addView(layout, mWmParams);
}
};
mHandler.post(mBannerRun);
}
}
4、unity3d部分接入代码
#if UNITY_ANDROID
using UnityEngine;
using System.Collections;
/**显示banner会回调注册的gameobject类的方法以及参数
BannerResult(string result)
1-"switch"
2-"recieve"
3-"fail"
4-"close"
**/
/**
显示Spot回调 SpotResult(string result)
1-"success"
2-"fail"
3-"close"
**/
public class YouMiAdForAndroid{
private static YouMiAdForAndroid instance;
AndroidJavaClass mJc;
AndroidJavaObject mJo;
//初始化
private void InitSDK(){
mJc=new AndroidJavaClass("com.unity3d.player.UnityPlayer");
mJo=mJc.GetStatic<AndroidJavaObject>("currentActivity");
}
//打开
public void Open(string goName){
mJo.Call ("setGameObject", goName);
}
//打开广告条
public void ShowBanner(){
mJo.Call ("showBanner");
}
//关闭广告条
public void CloseBanner(){
mJo.Call ("closeBanner");
}
//打开弹出广告
public void ShowSpot(){
mJo.Call ("showSpot");
}
//关闭弹出广告
public bool CloseSpot(int type){
return mJo.Call<bool> ("closeSpot", type);
}
//显示tips
public void ShowTipsInUiThread(string str, int duartion){
mJo.Call ("showTipsInUiThread", str, duartion);
}
public static YouMiAdForAndroid GetInstance(){
if(instance==null) {
instance=new YouMiAdForAndroid();
instance.InitSDK();
}
return instance;
}
}
#endif
5、以及一个和sharesdk一样的中介层,分发调用安卓或者ios代码,并判断平台以防没有真机调试时报错(ios未实现)
using UnityEngine;
using System.Collections;
public static class YouMiADSDK{
//打开
public static void Open(string goName){
if (Application.platform == RuntimePlatform.Android) {
#if UNITY_ANDROID
YouMiAdForAndroid.GetInstance().Open(goName);
#endif
}else if(Application.platform==RuntimePlatform.IPhonePlayer){
#if UNITY_IPHONE
#endif
}
}
//打开广告条
public static void ShowBanner(){
if (Application.platform == RuntimePlatform.Android) {
#if UNITY_ANDROID
YouMiAdForAndroid.GetInstance().ShowBanner();
#endif
}else if(Application.platform==RuntimePlatform.IPhonePlayer){
#if UNITY_IPHONE
#endif
}
}
//关闭广告条
public static void CloseBanner(){
if (Application.platform == RuntimePlatform.Android) {
#if UNITY_ANDROID
YouMiAdForAndroid.GetInstance().CloseBanner();
#endif
}else if(Application.platform==RuntimePlatform.IPhonePlayer){
#if UNITY_IPHONE
#endif
}
}
//打开弹出广告
public static void ShowSpot(){
if (Application.platform == RuntimePlatform.Android) {
#if UNITY_ANDROID
YouMiAdForAndroid.GetInstance().ShowSpot();
#endif
}else if(Application.platform==RuntimePlatform.IPhonePlayer){
#if UNITY_IPHONE
#endif
}
}
//关闭弹出广告 type->0-esc 1-home
public static bool CloseSpot(int type){
if (Application.platform == RuntimePlatform.Android) {
#if UNITY_ANDROID
return YouMiAdForAndroid.GetInstance().CloseSpot(type);
#endif
}else if(Application.platform==RuntimePlatform.IPhonePlayer){
#if UNITY_IPHONE
#endif
}
return true;
}
//显示tips
public static void ShowTipsInUiThread(string str, int duartion){
if (Application.platform == RuntimePlatform.Android) {
#if UNITY_ANDROID
YouMiAdForAndroid.GetInstance().ShowTipsInUiThread(str,duartion);
#endif
}else if(Application.platform==RuntimePlatform.IPhonePlayer){
#if UNITY_IPHONE
#endif
}
}
}
using UnityEngine;
using System.Collections;
public class AdScript : MonoBehaviour {
private string currentInfo="";
void Awake () {
YouMiADSDK.Open (gameObject.name);
}
void BannerResult(string result){
currentInfo = "banner:" + result;
}
void SpotResult(string result){
currentInfo = "spot:" + result;
Ego.isShowAdSpot = result == "success";
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
// GUI.depth = 999;
// GUI.color = Color.black;
// GUILayout.BeginArea (new Rect (Screen.width/2, 30, Screen.width, Screen.height));
// GUILayout.Label(currentInfo);
// GUILayout.EndArea ();
}
public static void ShowBanner(){
//YouMiADSDK.ShowTipsInUiThread ("尝试打开banner", 3);
YouMiADSDK.ShowBanner ();
print ("显示广告条");
}
public static void CloseBanner(){
//YouMiADSDK.ShowTipsInUiThread ("尝试关闭banner", 3);
YouMiADSDK.CloseBanner ();
print ("关闭广告条");
}
public static void ShowSpot(){
YouMiADSDK.ShowSpot ();
print ("显示弹出广告");
}
public static bool CloseSpot(int type){
return YouMiADSDK.CloseSpot (type);
}
}
7、最后附上合并后的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.egogame.jungleadventure"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
<uses-permission android:name="android.permission.GET_TASKS" />
<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" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<!-- 有米必备权限配置 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- 以下为可选权限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false"
android:allowBackup="true" android:name="cn.sharesdk.unity3d.ShareSDKApplication">
<activity
android:name="com.egogame.jungleadventure.YouMiActivity"
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>
</activity>
<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">
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
<activity
android:name="cn.sharesdk.framework.ShareSDKUIShell"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
<!-- 微信分享回调 -->
<activity
android:name="cn.sharesdk.onekeyshare.wxapi.WXEntryActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"/>
<!-- 配置unity3d的activity -->
<activity android:name="com.unity3d.player.UnityPlayerActivity" >
</activity>
<!-- 允许unity3d将事件传到DalvikVM的转发机制 -->
<meta-data
android:name="unityplayer.ForwardNativeEventsToDalvik"
android:value="true" />
<!-- 有米必备组件配置 -->
<activity
android:name="net.youmi.android.AdBrowser"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Light.NoTitleBar" >
</activity>
<service
android:name="net.youmi.android.AdService"
android:exported="false" >
</service>
<receiver android:name="net.youmi.android.AdReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<!-- (可选)设置有米广告推广渠道号,参数列表:http://wiki.youmi.net/Wiki/PromotionChannelIDs -->
<meta-data
android:name="YOUMI_CHANNEL"
android:value="这里替换为非负整数的渠道号" >
</meta-data>
</application>
</manifest>
大概就是这样,有什么问题欢迎在下面留言。
欢迎大家关注我的新浪微博:http://weibo.com/lessloo
2015.2.11号更新:
有米广告在360平台被拒绝,原因是广告点击就立即下载,容易造成玩家误操作。这点还挺恶心的。