【unity3d開發】unity接入unity Ads詳細流程
阿新 • • 發佈:2019-01-06
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 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 <[email protected]> /// </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就好了