unity接入unity Ads详细流程

unity官方提供的广告插件unity Ads总体来说还是很方便的,目前只支持安卓和iOS的广告,而且官方已经处理好了unity和安卓或者iOS的调用所以根本不需要再为平台编写中间件进行交互,这点还是很棒的。


看看unity官方宣传,拿《天天过马路》45天赚了1百万美元的广告费进行宣传,想想还真是有点小鸡冻!扯远了~~


下面看看官方的接入教程:

接入有两种办法

方法一:5.1以上的版本之间可以在Unity编辑器内Window > Services > Ads进行开启

1、在Window > Services > Ads进行开启



2、将开关打开,勾选下面的平台等信息即可(Enable test mode:勾选之后未上线之前,unity发布选项勾选development即可显示测试广告)



3、切换到Code Samples可以看到示例代码,在合适的地方如代码那样调用即可显示广告



方法二:5.1及以下的版本可以在Asset Store下载到插件:下载地址

1、下载完毕后将.unity文件导入到项目中

2、在http://dashboard.unityads.unity3d.com/ 创建项目,获得安卓和iOS的unity ads的id(储存起来,回头要用)

3、初始化广告

if (Advertisement.isSupported) { // If runtime platform is supported...
    Advertisement.Initialize(gameId, enableTestMode); // ...initialize.
}
4、在需要显示广告的地方调用显示广告

Advertisement.Show();

●共享一个unity ads帮助类,从unity ads demo提取出来的,特别好用

/// <summary>
/// UnityAdsHelper.cs - Written for Unity Ads Asset Store v1.1.4
///  by Nikkolai Davenport <nikkolai@unity3d.com> 
/// </summary>

using System;
using UnityEngine;
using System.Collections;
#if UNITY_IOS || UNITY_ANDROID
using UnityEngine.Advertisements;
#endif

public class UnityAdsHelper : MonoBehaviour 
{
	public string iosGameID = "24300";
	public string androidGameID = "24299";
	
	public bool enableTestMode = true;
	public bool showInfoLogs;
	public bool showDebugLogs;
	public bool showWarningLogs = true;
	public bool showErrorLogs = true;
	
	private static Action _handleFinished;
	private static Action _handleSkipped;
	private static Action _handleFailed;
	private static Action _onContinue;

#if UNITY_IOS || UNITY_ANDROID

	//--- Unity Ads Setup and Initialization

	void Start ()
	{
		Debug.Log("Running precheck for Unity Ads initialization...");

		string gameID = null;

	#if UNITY_IOS
		gameID = iosGameID;
	#elif UNITY_ANDROID
		gameID = androidGameID;
	#endif

		if (!Advertisement.isSupported)
		{
			Debug.LogWarning("Unity Ads is not supported on the current runtime platform.");
		}
		else if (Advertisement.isInitialized)
		{
			Debug.LogWarning("Unity Ads is already initialized.");
		}
		else if (string.IsNullOrEmpty(gameID))
		{
			Debug.LogError("The game ID value is not set. A valid game ID is required to initialize Unity Ads.");
		}
		else
		{
			Advertisement.debugLevel = Advertisement.DebugLevel.NONE;	
			if (showInfoLogs) Advertisement.debugLevel    |= Advertisement.DebugLevel.INFO;
			if (showDebugLogs) Advertisement.debugLevel   |= Advertisement.DebugLevel.DEBUG;
			if (showWarningLogs) Advertisement.debugLevel |= Advertisement.DebugLevel.WARNING;
			if (showErrorLogs) Advertisement.debugLevel   |= Advertisement.DebugLevel.ERROR;
			
			if (enableTestMode && !Debug.isDebugBuild)
			{
				Debug.LogWarning("Development Build must be enabled in Build Settings to enable test mode for Unity Ads.");
			}
			
			bool isTestModeEnabled = Debug.isDebugBuild && enableTestMode;
			Debug.Log(string.Format("Precheck done. Initializing Unity Ads for game ID {0} with test mode {1}...",
			                        gameID, isTestModeEnabled ? "enabled" : "disabled"));

			Advertisement.Initialize(gameID,isTestModeEnabled);

			StartCoroutine(LogWhenUnityAdsIsInitialized());
		}
	}

	private IEnumerator LogWhenUnityAdsIsInitialized ()
	{
		float initStartTime = Time.time;

		do yield return new WaitForSeconds(0.1f);
		while (!Advertisement.isInitialized);

		Debug.Log(string.Format("Unity Ads was initialized in {0:F1} seconds.",Time.time - initStartTime));
		yield break;
	}
	
	//--- Static Helper Methods

	public static bool isShowing { get { return Advertisement.isShowing; }}
	public static bool isSupported { get { return Advertisement.isSupported; }}
	public static bool isInitialized { get { return Advertisement.isInitialized; }}
	
	public static bool IsReady () 
	{ 
		return IsReady(null); 
	}
	public static bool IsReady (string zoneID) 
	{
		if (string.IsNullOrEmpty(zoneID)) zoneID = null;
		
		return Advertisement.isReady(zoneID);
	}

	public static void ShowAd () 
	{
		ShowAd(null,null,null,null,null);
	}
	public static void ShowAd (string zoneID) 
	{
		ShowAd(zoneID,null,null,null,null);
	}
	public static void ShowAd (string zoneID, Action handleFinished) 
	{
		ShowAd(zoneID,handleFinished,null,null,null);
	}
	public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped) 
	{
		ShowAd(zoneID,handleFinished,handleSkipped,null,null);
	}
	public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped, Action handleFailed) 
	{
		ShowAd(zoneID,handleFinished,handleSkipped,handleFailed,null);
	}
	public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped, Action handleFailed, Action onContinue)
	{
		if (string.IsNullOrEmpty(zoneID)) zoneID = null;

		_handleFinished = handleFinished;
		_handleSkipped = handleSkipped;
		_handleFailed = handleFailed;
		_onContinue = onContinue;

		if (Advertisement.isReady(zoneID))
		{
			Debug.Log("Showing ad now...");
			
			ShowOptions options = new ShowOptions();
			options.resultCallback = HandleShowResult;
			options.pause = true;

			Advertisement.Show(zoneID,options);
		}
		else 
		{
			Debug.LogWarning(string.Format("Unable to show ad. The ad placement zone {0} is not ready.",
			                               object.ReferenceEquals(zoneID,null) ? "default" : zoneID));
		}
	}

	private static void HandleShowResult (ShowResult result)
	{
		switch (result)
		{
		case ShowResult.Finished:
			Debug.Log("The ad was successfully shown.");
			if (!object.ReferenceEquals(_handleFinished,null)) _handleFinished();
			break;
		case ShowResult.Skipped:
			Debug.LogWarning("The ad was skipped before reaching the end.");
			if (!object.ReferenceEquals(_handleSkipped,null)) _handleSkipped();
			break;
		case ShowResult.Failed:
			Debug.LogError("The ad failed to be shown.");
			if (!object.ReferenceEquals(_handleFailed,null)) _handleFailed();
			break;
		}

		if (!object.ReferenceEquals(_onContinue,null)) _onContinue();
	}

#else

	void Start ()
	{
		Debug.LogWarning("Unity Ads is not supported under the current build platform.");
	}

	public static bool isShowing { get { return false; }}
	public static bool isSupported { get { return false; }}
	public static bool isInitialized { get { return false; }}

	public static bool IsReady () { return false; }
	public static bool IsReady (string zoneID) { return false; }

	public static void ShowAd () 
	{
		Debug.LogError("Failed to show ad. Unity Ads is not supported under the current build platform.");
	}
	public static void ShowAd (string zoneID) { ShowAd(); }
	public static void ShowAd (string zoneID, Action handleFinished) { ShowAd(); }
	public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped) { ShowAd(); }
	public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped, Action handleFailed) { ShowAd(); }
	public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped, Action handleFailed, Action onContinue) { ShowAd(); }

#endif
}

UnityHelper使用方法:

1、在项目中创建一个GameObject,将上面的代码UnityHelper.cs拖到该对象内,修改安卓和iOS的gameid即可自动初始化



2、在调用显示广告的地方调用接口即可显示,还有显示成功等回调,方便看广告完毕后加生命加金币之类的

public void ShowAd ()
{
	if (!UnityAdsHelper.isSupported)
		return;
	if (!UnityAdsHelper.isInitialized)
		return;
	if (UnityAdsHelper.isShowing)
		return;
	UnityAdsHelper.ShowAd (null, ShowAdFinish);
}

public void ShowAdFinish(){
    //增加道具
}


安卓打包问题:

如果有多个sdk,发现是不用合并AndroidManifest.xml文件的,我以为要合并所以合并之后打包正常但是显示广告就闪退了,不知道什么原理,不知道为什么一个项目可以有两个AndroidManifest.xml文件,是unity会自动合并么??有朋友知道的话可以告诉我下。


iOS打包问题:

什么都不用改动,直接打包就行了


碰到的问题:

●安卓打包成功,但是调用显示广告接口闪退?


解决办法:Assets\Plugins\Android\unityads直接放在Assets\Plugins\Android下,什么都不用动,也不用合并AndroidManifest.xml就好了


参考资料:

http://unityads.unity3d.com/help/help/resources

http://unityads.unity3d.com/help/monetization/integration-guide-unity

http://docs.unity3d.com/Manual/UnityAdsHowTo.html




  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值