【unity3d开发】unity接入unity Ads详细流程

来源:互联网 发布:怎么开淘宝店铺 流程 编辑:程序博客网 时间:2024/06/07 16:50

目录(?)[+]

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
 在CODE上查看代码片派生到我的代码片
  1. if (Advertisement.isSupported) { // If runtime platform is supported...  
  2.     Advertisement.Initialize(gameId, enableTestMode); // ...initialize.  
  3. }  
4、在需要显示广告的地方调用显示广告

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Advertisement.Show();  

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

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /// <summary>  
  2. /// UnityAdsHelper.cs - Written for Unity Ads Asset Store v1.1.4  
  3. ///  by Nikkolai Davenport <nikkolai@unity3d.com>   
  4. /// </summary>  
  5.   
  6. using System;  
  7. using UnityEngine;  
  8. using System.Collections;  
  9. #if UNITY_IOS || UNITY_ANDROID  
  10. using UnityEngine.Advertisements;  
  11. #endif  
  12.   
  13. public class UnityAdsHelper : MonoBehaviour   
  14. {  
  15.     public string iosGameID = "24300";  
  16.     public string androidGameID = "24299";  
  17.       
  18.     public bool enableTestMode = true;  
  19.     public bool showInfoLogs;  
  20.     public bool showDebugLogs;  
  21.     public bool showWarningLogs = true;  
  22.     public bool showErrorLogs = true;  
  23.       
  24.     private static Action _handleFinished;  
  25.     private static Action _handleSkipped;  
  26.     private static Action _handleFailed;  
  27.     private static Action _onContinue;  
  28.  
  29. #if UNITY_IOS || UNITY_ANDROID  
  30.   
  31.     //--- Unity Ads Setup and Initialization  
  32.   
  33.     void Start ()  
  34.     {  
  35.         Debug.Log("Running precheck for Unity Ads initialization...");  
  36.   
  37.         string gameID = null;  
  38.  
  39.     #if UNITY_IOS  
  40.         gameID = iosGameID;  
  41.     #elif UNITY_ANDROID  
  42.         gameID = androidGameID;  
  43.     #endif  
  44.   
  45.         if (!Advertisement.isSupported)  
  46.         {  
  47.             Debug.LogWarning("Unity Ads is not supported on the current runtime platform.");  
  48.         }  
  49.         else if (Advertisement.isInitialized)  
  50.         {  
  51.             Debug.LogWarning("Unity Ads is already initialized.");  
  52.         }  
  53.         else if (string.IsNullOrEmpty(gameID))  
  54.         {  
  55.             Debug.LogError("The game ID value is not set. A valid game ID is required to initialize Unity Ads.");  
  56.         }  
  57.         else  
  58.         {  
  59.             Advertisement.debugLevel = Advertisement.DebugLevel.NONE;     
  60.             if (showInfoLogs) Advertisement.debugLevel    |= Advertisement.DebugLevel.INFO;  
  61.             if (showDebugLogs) Advertisement.debugLevel   |= Advertisement.DebugLevel.DEBUG;  
  62.             if (showWarningLogs) Advertisement.debugLevel |= Advertisement.DebugLevel.WARNING;  
  63.             if (showErrorLogs) Advertisement.debugLevel   |= Advertisement.DebugLevel.ERROR;  
  64.               
  65.             if (enableTestMode && !Debug.isDebugBuild)  
  66.             {  
  67.                 Debug.LogWarning("Development Build must be enabled in Build Settings to enable test mode for Unity Ads.");  
  68.             }  
  69.               
  70.             bool isTestModeEnabled = Debug.isDebugBuild && enableTestMode;  
  71.             Debug.Log(string.Format("Precheck done. Initializing Unity Ads for game ID {0} with test mode {1}...",  
  72.                                     gameID, isTestModeEnabled ? "enabled" : "disabled"));  
  73.   
  74.             Advertisement.Initialize(gameID,isTestModeEnabled);  
  75.   
  76.             StartCoroutine(LogWhenUnityAdsIsInitialized());  
  77.         }  
  78.     }  
  79.   
  80.     private IEnumerator LogWhenUnityAdsIsInitialized ()  
  81.     {  
  82.         float initStartTime = Time.time;  
  83.   
  84.         do yield return new WaitForSeconds(0.1f);  
  85.         while (!Advertisement.isInitialized);  
  86.   
  87.         Debug.Log(string.Format("Unity Ads was initialized in {0:F1} seconds.",Time.time - initStartTime));  
  88.         yield break;  
  89.     }  
  90.       
  91.     //--- Static Helper Methods  
  92.   
  93.     public static bool isShowing { get { return Advertisement.isShowing; }}  
  94.     public static bool isSupported { get { return Advertisement.isSupported; }}  
  95.     public static bool isInitialized { get { return Advertisement.isInitialized; }}  
  96.       
  97.     public static bool IsReady ()   
  98.     {   
  99.         return IsReady(null);   
  100.     }  
  101.     public static bool IsReady (string zoneID)   
  102.     {  
  103.         if (string.IsNullOrEmpty(zoneID)) zoneID = null;  
  104.           
  105.         return Advertisement.isReady(zoneID);  
  106.     }  
  107.   
  108.     public static void ShowAd ()   
  109.     {  
  110.         ShowAd(null,null,null,null,null);  
  111.     }  
  112.     public static void ShowAd (string zoneID)   
  113.     {  
  114.         ShowAd(zoneID,null,null,null,null);  
  115.     }  
  116.     public static void ShowAd (string zoneID, Action handleFinished)   
  117.     {  
  118.         ShowAd(zoneID,handleFinished,null,null,null);  
  119.     }  
  120.     public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped)   
  121.     {  
  122.         ShowAd(zoneID,handleFinished,handleSkipped,null,null);  
  123.     }  
  124.     public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped, Action handleFailed)   
  125.     {  
  126.         ShowAd(zoneID,handleFinished,handleSkipped,handleFailed,null);  
  127.     }  
  128.     public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped, Action handleFailed, Action onContinue)  
  129.     {  
  130.         if (string.IsNullOrEmpty(zoneID)) zoneID = null;  
  131.   
  132.         _handleFinished = handleFinished;  
  133.         _handleSkipped = handleSkipped;  
  134.         _handleFailed = handleFailed;  
  135.         _onContinue = onContinue;  
  136.   
  137.         if (Advertisement.isReady(zoneID))  
  138.         {  
  139.             Debug.Log("Showing ad now...");  
  140.               
  141.             ShowOptions options = new ShowOptions();  
  142.             options.resultCallback = HandleShowResult;  
  143.             options.pause = true;  
  144.   
  145.             Advertisement.Show(zoneID,options);  
  146.         }  
  147.         else   
  148.         {  
  149.             Debug.LogWarning(string.Format("Unable to show ad. The ad placement zone {0} is not ready.",  
  150.                                            object.ReferenceEquals(zoneID,null) ? "default" : zoneID));  
  151.         }  
  152.     }  
  153.   
  154.     private static void HandleShowResult (ShowResult result)  
  155.     {  
  156.         switch (result)  
  157.         {  
  158.         case ShowResult.Finished:  
  159.             Debug.Log("The ad was successfully shown.");  
  160.             if (!object.ReferenceEquals(_handleFinished,null)) _handleFinished();  
  161.             break;  
  162.         case ShowResult.Skipped:  
  163.             Debug.LogWarning("The ad was skipped before reaching the end.");  
  164.             if (!object.ReferenceEquals(_handleSkipped,null)) _handleSkipped();  
  165.             break;  
  166.         case ShowResult.Failed:  
  167.             Debug.LogError("The ad failed to be shown.");  
  168.             if (!object.ReferenceEquals(_handleFailed,null)) _handleFailed();  
  169.             break;  
  170.         }  
  171.   
  172.         if (!object.ReferenceEquals(_onContinue,null)) _onContinue();  
  173.     }  
  174.  
  175. #else  
  176.   
  177.     void Start ()  
  178.     {  
  179.         Debug.LogWarning("Unity Ads is not supported under the current build platform.");  
  180.     }  
  181.   
  182.     public static bool isShowing { get { return false; }}  
  183.     public static bool isSupported { get { return false; }}  
  184.     public static bool isInitialized { get { return false; }}  
  185.   
  186.     public static bool IsReady () { return false; }  
  187.     public static bool IsReady (string zoneID) { return false; }  
  188.   
  189.     public static void ShowAd ()   
  190.     {  
  191.         Debug.LogError("Failed to show ad. Unity Ads is not supported under the current build platform.");  
  192.     }  
  193.     public static void ShowAd (string zoneID) { ShowAd(); }  
  194.     public static void ShowAd (string zoneID, Action handleFinished) { ShowAd(); }  
  195.     public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped) { ShowAd(); }  
  196.     public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped, Action handleFailed) { ShowAd(); }  
  197.     public static void ShowAd (string zoneID, Action handleFinished, Action handleSkipped, Action handleFailed, Action onContinue) { ShowAd(); }  
  198.  
  199. #endif  
  200. }  

UnityHelper使用方法:

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



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

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public void ShowAd ()  
  2. {  
  3.     if (!UnityAdsHelper.isSupported)  
  4.         return;  
  5.     if (!UnityAdsHelper.isInitialized)  
  6.         return;  
  7.     if (UnityAdsHelper.isShowing)  
  8.         return;  
  9.     UnityAdsHelper.ShowAd (null, ShowAdFinish);  
  10. }  
  11.   
  12. public void ShowAdFinish(){  
  13.     //增加道具  
  14. }  


安卓打包问题:

如果有多个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

0 0
原创粉丝点击