最近想接入一款广告,百度搜了下最后选定了有米广告,最主要因为它提款及时,还有就是它有unity3d版本,不过遗憾的是现在没有IOS版本。至于接入的步骤在官方网站里面已经说的很清楚了http://www.youmi.net/page/download/sdk,大家可以自己前往查阅,安卓版本有些功能未实现,所以我在它的基础上增加了一些功能,并使sdk更加方便的在uniity3d使用。
1、增加了设置unity3d的对象名称
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("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 ("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
大概就是这样,有什么问题欢迎在下面留言。
欢迎大家关注我的新浪微博:http://weibo.com/lessloo
2015.2.11号更新:
有米广告在360平台被拒绝,原因是广告点击就立即下载,容易造成玩家误操作。这点还挺恶心的。