【unity3d开发】unity接⼊unityAds详细流程
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⽂件导⼊到项⽬中
3、初始化⼴告
if (Advertisement.isSupported) { // If runtime platform
Advertisement.Initialize(gameId, enableTestMode); // ...initialize.
}
4、在需要显⽰⼴告的地⽅调⽤显⽰⼴告
Advertisement.Show();
●共享⼀个unity ads帮助类,从提取出来的,特别好⽤
/// <summary>
/// UnityAdsHelper.cs - Written for Unity Ads Asset Store v1.1.4
/// by Nikkolai Davenport <nikkolai@unity3d>
/// </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 ");
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))
unity 教程{
Debug.Log("Showing ");
ShowOptions options = new ShowOptions();
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论