Mobvista广告接入Unity。主要讲插屏,视频广告以及Native的使用。
首先Mobvista广告得SDK目前应该只能通过由服务商提供,必须有Mobvista平台的账号。
下面开始接Mobvista广告,仍然是两个步骤。
第一步:接Mobvista广告插件。
将MVUnityPlugin_XXX.unitypackage导入你的项目。
然后注意以下几点,不管集成哪种广告形式:
1、请一定要加入AndroidSDK里 /jar/下的mobvista_common和mobvista_alpha这两文件下的所有jar包和res文件下所有东西。
2、其中所有添加的jar包全部放到Plugins/Android/下,所有res文件全部放到Plugins/Android/res/下。
分广告形式集成说明:
如果只集成native形式的广告,需要导入unityplugin里的MVUnity.cs、MVNativeAdsUnity.cs,以及SDK里面jar目录下的mobvista_mvnative文件夹下的jar、res。
如果只集成appwall形式的广告,需要导入unityplugin里的MVUnity.cs、MVAppWallUnity.cs,以及SDK里面jar目录下的mobvista_appwall文件夹下的jar、res。
如果只集成rewardvideo形式的广告,需要导入unityplugin里的MVUnity.cs、MVRewardAdsUnity.cs,以及SDK里面jar目录下的mobvista_reward、mobvista_playercommon、mobvista_videocommon、mobvista_videojs、mobvista_mvjscommon文件夹下的jar、res。
如果只集成offerwall形式的广告,需要导入unityplugin里的MVUnity.cs、MVOfferWallUnity.cs,以及SDK里面jar目录下的mobvista_offerwall、mobvista_playercommon、mobvista_mvjscommon文件夹下的jar、res。
如果只集成interstitial形式的广告,需要导入unityplugin里的MVUnity.cs、MVInterstitialUnity.cs,以及SDK里面jar目录下的mobvista_interstital、mobvista_mvjscommon文件夹下的jar、res。
(Android对应的SDK文件和IOS对应的framework文件也由服务商提供)
重点:
1,如果项目里没有android-support-v4文件的话需要将android-support-v4放到Plugins/Android/下;
2,AndroidManifest程序入口位置需要修改成下图代码。
以上,第一步完成。
第二步:代码部分。
1,初始化SDK
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using AssemblyCSharpMVpass;
public class MvInit : MonoBehaviour {
//AppID和ApiKey
private const string MVSDKAppIDForiOS = "22050";
private const string MVSDKApiKeyForiOS = "7c22942b749fe6a6e361b675e96b3ee9";
private const string MVSDKAppIDForAndroid = "24282";
private const string MVSDKApiKeyForAndroid = "7c22942b749fe6a6e361b675e96b3ee9";
private const string MVSDKApplicationIdForAndroid = "";
private static bool initMVSDKFlag = false;
//初始化方法
void InitMVSDK ()
{
string appId = null;
string apiKey = null;
string applicationId = null;
#if UNITY_ANDROID
appId = MVSDKAppIDForAndroid;
apiKey = MVSDKApiKeyForAndroid;
applicationId = MVSDKApplicationIdForAndroid;
#elif UNITY_IPHONE || UNITY_IOS
appId = MVSDKAppIDForiOS;
apiKey = MVSDKApiKeyForiOS;
applicationId = null;
#endif
MVUnity.InitMVSDKInUnity(appId,apiKey,applicationId);
}
// Use this for initialization
//初始化MVSDK一次即可
void Awake ()
{
Debug.Log(Mathf.Atan2(1,1));
if (!initMVSDKFlag) {
InitMVSDK ();
initMVSDKFlag = true;
Debug.Log("MvSdkInit");
}
}
}
2,插屏广告
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using AssemblyCSharpMVpass;
public class MvInterstitialAd : MonoBehaviour {
private MVInterstitialUnity interstitialAd;
private const string KInterstitialUnitIDForAndroid = "3667";
private const string KInterstitialUnitIDForiOS = "3703";
void Start ()
{
string interstitialUnitId = null;
#if UNITY_ANDROID
this.name = "interstitialBridge";
interstitialUnitId = KInterstitialUnitIDForAndroid;
#elif UNITY_IPHONE || UNITY_IOS
interstitialUnitId = KInterstitialUnitIDForiOS;
#endif
interstitialAd = new MVInterstitialUnity ();
interstitialAd.InitInterstitialInUnity (interstitialUnitId, MVAdCategory.MVAD_CATEGORY_ALL);
Debug.Log ("MVInterstitialUnity Init");
}
//加载广告方法
public void LoadMvInterlAds()
{
if (interstitialAd != null) {
#if UNITY_IPHONE || UNITY_IOS
MVInterstitialUnity.InterstitialAdDidLoad = (delegate() {
this.Log ("InterstitialAdDidLoad");
});
MVInterstitialUnity.InterstitialAdDidFailWithError = (delegate(string error) {
this.Log ("InterstitialAdDidFailWithError: " + error);
});
#endif
interstitialAd.LoadInterstitialInUnity ();
}
}
//显示广告方法
public void ShowMvInterlAds()
{
if (interstitialAd != null){
#if UNITY_IPHONE || UNITY_IOS
MVInterstitialUnity.InterstitialAdShowSuccess = (delegate() {
this.Log ("InterstitialAdShowSuccess");
});
MVInterstitialUnity.InterstitialAdShowFailed = (delegate(string error) {
this.Log ("InterstitialAdShowFailed: " + error);
});
MVInterstitialUnity.InterstitialAdDidClick = (delegate() {
this.Log ("InterstitialAdDidClick");
});
MVInterstitialUnity.InterstitialAdDidClosed = (delegate() {
this.Log ("InterstitialAdDidClosed");
});
#endif
interstitialAd.ShowInterstitialInUnity ();
}
}
/***************android callback area start**********************/
#if UNITY_ANDROID
void onInterstitialLoadSuccess(String message){
Debug.Log ("onInterstitialLoadSuccess :" + message);
}
void onInterstitialLoadFail(String errorMessage){
Debug.Log("onInterstitialLoadFail :" + errorMessage);
}
void onInterstitialClosed(String message){
Debug.Log("onInterstitialClosed :" + message);
}
#endif
/***************android callback area end**********************/
}
3,视频广告
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using AssemblyCSharpMVpass;
public class MvRewardAds : MonoBehaviour {
//Android
private const string KRewardUnitIDForAndroid = "2163";
private const string KRewardIDForAndroid = "7";
//ios
private const string KRewardUnitIDForiOS = "607";
private const string KRewardIDForiOS = "8794";
private MVRewardAdsUnity rewardVideoAd1;
void OnEnable ()
{
// Listen to all events for illustration purposes
#if UNITY_IPHONE || UNITY_IOS
MVRewardAdsUnity.loadedEvent += RewardWallAdDidLoad;
MVRewardAdsUnity.loadFailedEvent += RewardWallAdDidFailWithError;
MVRewardAdsUnity.shownEvent += RewardWallAdShowSuccess;
MVRewardAdsUnity.showFailedEvent += RewardWallAdShowFailWithError;
MVRewardAdsUnity.clickEvent += RewardWallAdDidClick;
MVRewardAdsUnity.rewardInfoEvent += RewardWallOnVideoAdDismissed;
#endif
}
void OnDisable ()
{
#if UNITY_IPHONE || UNITY_IOS
MVRewardAdsUnity.loadedEvent -= RewardWallAdDidLoad;
MVRewardAdsUnity.loadFailedEvent -= RewardWallAdDidFailWithError;
MVRewardAdsUnity.shownEvent -= RewardWallAdShowSuccess;
MVRewardAdsUnity.showFailedEvent -= RewardWallAdShowFailWithError;
MVRewardAdsUnity.clickEvent -= RewardWallAdDidClick;
MVRewardAdsUnity.rewardInfoEvent -= RewardWallOnVideoAdDismissed;
#endif
}
void Start ()
{
#if UNITY_ANDROID
this.name = "rewardwallBridge";
#endif
rewardVideoAd1 = new MVRewardAdsUnity ();
#if UNITY_ANDROID
string rewardUnitId = KRewardUnitIDForAndroid;
rewardVideoAd1.sharedInstanceForAndroid (rewardUnitId);
#endif
}
//加载广告方法
public void LoadMvRewardAds()
{
string rewardUnitId = null;
#if UNITY_ANDROID
rewardUnitId = KRewardUnitIDForAndroid;
#elif UNITY_IPHONE || UNITY_IOS
rewardUnitId = KRewardUnitIDForiOS;
#endif
rewardVideoAd1.LoadVideoInUnity (rewardUnitId);
}
//显示广告方法
public void ShowMvRewardAds()
{
string rewardUnitId = null;
string rewardId = null;
string userId = "Your UserId";
#if UNITY_ANDROID
rewardUnitId = KRewardUnitIDForAndroid;
rewardId = KRewardIDForAndroid;
#elif UNITY_IPHONE || UNITY_IOS
rewardUnitId = KRewardUnitIDForiOS;
rewardId = KRewardIDForiOS;
#endif
if (rewardVideoAd1.IsVideoReadyToPlayInUnity (rewardUnitId)) {
Debug.Log("rewardVideoAd is Ready, will play");
rewardVideoAd1.ShowVideoInUnity (rewardUnitId, rewardId, userId);
} else {
Debug.Log("rewardVideoAd not Ready");
}
}
/*************** rewardvideo callback area start**********************/
#if UNITY_ANDROID
void onVideoLoadSuccess(string message){
Debug.Log("RewardWallLoadSuccess:" + message);
}
void onShowFail(string errorMessage){
Debug.Log("RewardWallShowFail:" + errorMessage);
}
void onVideoLoadFail(string message){
Debug.Log("RewardWallVideoLoadFail:" + message);
}
void onAdShow(string message){
Debug.Log("RewardWallAdShow:" + message);
}
void onAdClose(string message){
MVRewardAdInfo rewardInfo = new MVRewardAdInfo ();
rewardInfo = JsonUtility.FromJson<MVRewardAdInfo> (message);
if (rewardInfo.converted == "1") {
Debug.Log("RewardWallOnVideoAdClosed:" + rewardInfo.rewardName);
}
else{
Debug.Log("RewardWallOnVideoAdClosed:" + "No Reward");
}
}
#endif
#if UNITY_IOS || UNITY_IPHONE
void RewardWallAdDidLoad(string unitId)
{
Debug.Log ("RewardWallAdDidLoad unitId:" + unitId);
}
void RewardWallAdDidFailWithError(string unitId,string errorMsg)
{
Debug.Log ("RewardWallAdDidFailWithError: " + errorMsg + "unitId:" + unitId);
}
void RewardWallAdShowSuccess(string unitId)
{
Debug.Log ("RewardWallAdShowSuccess unitId:" + unitId);
}
void RewardWallAdShowFailWithError(string unitId,string errorMsg)
{
Debug.Log ("RewardWallAdShowFailed: " + errorMsg + "unitId:" + unitId);
}
void RewardWallAdDidClick(string unitId)
{
Debug.Log ("RewardWallAdDidClick unitId:" + unitId);
}
void RewardWallOnVideoAdDismissed(string unitId,bool converted,MVRewardAdInfo rewardInfo)
{
if(converted && rewardInfo != null){
Debug.Log ("RewardWallOnVideoAdDismissed:" + rewardInfo.rewardName);
}
}
#endif
/*************** rewardvideo callback area end**********************/
}
4,Native广告。用法参照FaceBookNative广告。
using AssemblyCSharpMVpass;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MvNativeAds : MonoBehaviour {
public bool IsNativeMobvistaAdLoaded;
//public GameObject CallBtn;
//对应广告位的ID
private MVNativeAdsUnity nativeAd;
[Header("Text:")]
public Text title;
public Text socialContext;
public Text InstallButtonText;
[Header("Images:")]
public Image panel;
public Image coverImage;
public Image iconImage;
MVButton coverViewButtonForiOS;
public Sprite IconImage { get; private set; }
public Sprite CoverImage { get; private set; }
public string unitId = "23085";
MVCampaign campaignForCurrentAdView;
// Use this for initialization
void InitMVNativeAds()
{
MVAdCategory adCategory = MVAdCategory.MVAD_CATEGORY_GAME;
//广告模板
MVTemplate[] supportedTemplates = new MVTemplate[1];
//模板类型
supportedTemplates[0] = new MVTemplate() { templateType = MVAdTemplateType.MVAD_TEMPLATE_BIG_IMAGE, adsNum = 3 };
this.name = "nativeBridge";
//初始化unitId
nativeAd = new MVNativeAdsUnity();
nativeAd.InitNativeAdsInUnity(unitId, "", 1, false, adCategory, supportedTemplates);
#if UNITY_IPHONE || UNITY_IOS
//register the AdsTappedCallback function
nativeAd.AddAdsTappedCallBackForiOS();
#endif
this.Log("MVNativeAdsUnity Init");
}
void DrwaMVButtonFullScreen()
{
Debug.Log("画个区域" + "DrwaMVButtonFullScreen");
// react Area just the InstallButton
float x = 0;
float y = 0;
float widht = 716 * 1.2f;
float height = 113 * 1.2f;
Vector3 v3 = new Vector3(x, y, 0.0f);
Vector2 v2 = new Vector2(widht, height);
coverViewButtonForiOS = new MVButton();
coverViewButtonForiOS.DrawMVButton(panel.transform, v3, v2, Color.red);
coverViewButtonForiOS.Touched = (delegate (string campaign)
{
Debug.Log("coverViewButtonForiOS Tapped");
});
}
void RemoveAndroidCoverView()
{
#if UNITY_ANDROID
if (nativeAd != null)
{
if (this.campaignForCurrentAdView != null)
{
nativeAd.UnRegisterViewForAndroid(this.campaignForCurrentAdView.index);
}
}
#endif
}
void Start()
{
#if UNITY_ANDROID
this.name = "nativeBridge";
#endif
InitMVNativeAds();
#if UNITY_IPHONE || UNITY_IOS
//UI , draw coverView for Regist AdInstallButton
DrwaMVButtonJustForiOS ();
#endif
LoadAd();
}
void OnGUI()
{
// Update GUI from native ad info
if (nativeAd != null && CoverImage != null)
{
coverImage.sprite = CoverImage;
}
if (nativeAd != null && IconImage != null)
{
iconImage.sprite = IconImage;
}
}
//load Image With ImageUrl
private static TextureFormat imageFormatRGBA32()
{
return TextureFormat.RGBA32;
}
private static TextureFormat imageFormatRGB24()
{
return TextureFormat.RGB24;
}
public IEnumerator LoadIconImage(string url)
{
if (url != null)
{
Texture2D texture = new Texture2D(4, 4, imageFormatRGBA32(), false);
WWW www = new WWW(url);
yield return www;
www.LoadImageIntoTexture(texture);
if (texture)
{
IconImage = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
}
}
public IEnumerator LoadCoverImage(string url)
{
if (url != null)
{
Texture2D texture = new Texture2D(4, 4, imageFormatRGB24(), false);
WWW www = new WWW(url);
yield return www;
www.LoadImageIntoTexture(texture);
if (texture)
{
CoverImage = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
}
}
public void LoadAd()
{
Debug.Log("加载广告");
if (nativeAd != null)
{
#if UNITY_IPHONE || UNITY_IOS
MVNativeAdsUnity.NativeAdDidLoad = (delegate (MVCampaign[] list) {
this.Log ("NativeAdDidLoad");
if(list.Length > 0){
MVCampaign campaign = list[0];
this.campaignForCurrentAdView = campaign;
StartCoroutine (LoadIconImage (campaign.iconUrl));
StartCoroutine (LoadCoverImage (campaign.imageUrl));
title.text = campaign.appName;
socialContext.text = campaign.appDesc;
InstallButtonText.text = campaign.adCall;
// Binding the campaign to the CoverViewButton when you get the campaign or your adView will show.
string json = JsonUtility.ToJson(campaign);
if(nativeAd.nativeAdManager != IntPtr.Zero){
coverViewButtonForiOS.RegistCampaign (nativeAd.nativeAdManager,json);
}
}
});
MVNativeAdsUnity.NativeAdDidFailWithError = (delegate(string error) {
this.Log ("Native ad failed to load with error: " + error);
});
MVNativeAdsUnity.NativeAdDidClick = (delegate(MVCampaign campaign) {
this.Log ("Ad Clicked: campaignId:" + campaign.adId);
});
MVNativeAdsUnity.NativeAdClickUrlWillStartToJump = (delegate(string clickUrl) {
this.Log ("OnAdClickUrlWillStartToJumpCallBack.");
});
MVNativeAdsUnity.NativeAdClickUrlWillDidToJump = (delegate(string clickUrl) {
this.Log ("OnAdClickUrlWillDidToJumpCallBack.");
});
MVNativeAdsUnity.NativeAdClickUrlWillDidToJumpError = (delegate(string finalUrl,string error) {
this.Log ("OnAdClickUrlWillDidToJumpErrorCallBack.");
});
MVNativeAdsUnity.NativeAdImpressionWithType = (delegate(MVAdSourceType adSource) {
this.Log ("NativeAdImpressionWithType: " + adSource );
});
#endif
// Initiate a request to load an ad.
nativeAd.LoadNativeAdsInUnity();
}
}
public void RegisterUIView()
{
#if UNITY_ANDROID
if (nativeAd != null)
{
nativeAd.RegisterViewForAndroid(40, 60, 716, 113, this.campaignForCurrentAdView.index);
}
#endif
}
public void UnRegisterUIView()
{
#if UNITY_ANDROID
if (nativeAd != null && this.campaignForCurrentAdView != null)
{
nativeAd.UnRegisterViewForAndroid(this.campaignForCurrentAdView.index);
}
#endif
}
/***************android callback area start**********************/
#if UNITY_ANDROID
void callback_android_data(String json)
{
this.Log("Ad loaded");
MVCampaignArrayWrapper wrapper = JsonUtility.FromJson<MVCampaignArrayWrapper>(json);
MVCampaign[] campaigns = wrapper.objects;
if (campaigns.Length > 0)
{
MVCampaign campaign = campaigns[0];
this.campaignForCurrentAdView = campaign;
StartCoroutine(LoadIconImage(campaign.iconUrl));
StartCoroutine(LoadCoverImage(campaign.imageUrl));
title.text = campaign.appName;
socialContext.text = campaign.appDesc;
InstallButtonText.text = campaign.adCall;
RegisterUIView();
}
}
void onAdLoadError(String errorMessage)
{
this.Log("AdLoadError :" + errorMessage);
}
void onLoggingImpression(String adSource)
{
MVAdSourceType source = (MVAdSourceType)Convert.ToInt32(adSource);
this.Log("onLoggingImpression adSource:" + source);
}
void onAdLoadClick(String message)
{
MVCampaign mvCampaign = new MVCampaign();
mvCampaign = JsonUtility.FromJson<MVCampaign>(message);
if (mvCampaign != null)
{
this.Log("AdClick:" + mvCampaign.imageUrl);
}
}
void onStartRedirection(String message)
{
MVCampaignTrack campaignTrack = new MVCampaignTrack();
campaignTrack = JsonUtility.FromJson<MVCampaignTrack>(message);
if (campaignTrack != null)
{
this.Log("StartRedirection:" + campaignTrack.campaign.imageUrl);
this.Log("StartRedirection:" + campaignTrack.url);
}
}
void onRedirectionFailed(String message)
{
MVCampaignTrack campaignTrack = new MVCampaignTrack();
campaignTrack = JsonUtility.FromJson<MVCampaignTrack>(message);
if (campaignTrack != null)
{
this.Log("RedirectionFailed:" + campaignTrack.campaign.imageUrl);
this.Log("RedirectionFailed:" + campaignTrack.url);
}
}
void onRedirectionFinished(String message)
{
MVCampaignTrack campaignTrack = new MVCampaignTrack();
campaignTrack = JsonUtility.FromJson<MVCampaignTrack>(message);
if (campaignTrack != null)
{
this.Log("FinishRedirection:" + campaignTrack.campaign.imageUrl);
this.Log("FinishRedirection:" + campaignTrack.url);
}
}
void onDownloadStart(String message)
{
MVCampaign downloadCampaign = new MVCampaign();
downloadCampaign = JsonUtility.FromJson<MVCampaign>(message);
if (downloadCampaign != null)
{
this.Log("DownloadStart:" + downloadCampaign.appName);
}
}
void onDownloadFinish(String message)
{
MVCampaign downloadCampaign = new MVCampaign();
downloadCampaign = JsonUtility.FromJson<MVCampaign>(message);
if (downloadCampaign != null)
{
this.Log("DownloadFinish:" + downloadCampaign.appName);
}
}
void onDownloadProgress(String progress)
{
this.Log("DownloadProgress :" + progress);
}
void onInterceptDefaultLoadingDialog(String message)
{
this.Log("InterceptDefaultLoadingDialog :" + message);
}
void onShowLoading(String message)
{
MVCampaign showLoadingCampaign = new MVCampaign();
showLoadingCampaign = JsonUtility.FromJson<MVCampaign>(message);
if (showLoadingCampaign != null)
{
this.Log("ShowLoading:" + showLoadingCampaign.appName);
}
}
void onDismissLoading(String message)
{
MVCampaign dismissLoadingCampaign = new MVCampaign();
dismissLoadingCampaign = JsonUtility.FromJson<MVCampaign>(message);
if (dismissLoadingCampaign != null)
{
this.Log("DismissLoading:" + dismissLoadingCampaign.appName);
}
}
#endif
/***************android callback area end**********************/
private void Log(string s)
{
s = "Log: " + s;
Debug.Log(s);
}
private void OnDisable()
{
UnRegisterUIView();
}
}
以上插屏和视频广告替换成自己的广告ID即可。
Mobvista的Native广告在接入Unity中还有本身存在一些缺点,使用时需要根据自己需求做相应改动。
以上。