找回密码
 立即注册
查看: 1562|回复: 16

[简易教程] unity接入unity Ads详细流程

[复制链接]
发表于 2019-2-18 17:39 | 显示全部楼层 |阅读模式

http://www.sohu.com/a/160890758_667928



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、初始化广告


[csharp] view plain copy




  • if (Advertisement.isSupported) { // If runtime platform is supported...  
  •     Advertisement.Initialize(gameId, enableTestMode); // ...initialize.  
  • }  

4、在需要显示广告的地方调用显示广告

[csharp] view plain copy




  • Advertisement.Show();  



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

[csharp] view plain copy




  • /// <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、在调用显示广告的地方调用接口即可显示,还有显示成功等回调,方便看广告完毕后加生命加金币之类的


[csharp] view plain copy




  • 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


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
 楼主| 发表于 2019-2-18 17:39 | 显示全部楼层
发表于 2019-5-1 08:33 | 显示全部楼层
楼主是超人
发表于 2019-5-1 08:37 | 显示全部楼层
好帖就是要顶
发表于 2019-5-1 08:11 | 显示全部楼层
难得一见的好帖
发表于 2019-5-1 08:01 | 显示全部楼层
不错不错
发表于 2019-5-1 08:53 | 显示全部楼层
LZ真是人才
发表于 2019-10-11 09:23 | 显示全部楼层
顶顶多好
发表于 2019-10-11 09:33 | 显示全部楼层
真心顶
发表于 2019-10-11 09:06 | 显示全部楼层
说的非常好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-3-28 16:58 , Processed in 0.105068 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表