UnityAdvertisement,顧名思義,以Unity為主架構的廣告系統,趁著工作上的需要,來記錄實作過程。
*使用版本為2019LTS,當前日期為2021/02/03
第一步
在Windows→General→Services
第二步
選擇好Unity的帳號(這步驟看似很簡單,但可能人多之後選錯帳號會有很多意料之外的錯誤)
第三步
簡單介紹下Advertisement和一些功能簡介
第四步
將開關打開,並選擇continue之後Unity會自動幫你把相關SDK裝好
*這裡要注意已經藉由Service安裝了SDK不可以去AssetStore安裝Monetization,否則有可能出現以下類似版本衝突的錯誤
-Library\PackageCache\com.unity.ads@3.5.0\Runtime\Monetization\AndroidPlacementContentOperations.cs(54,17): error CS0433: The type 'IUnityLifecycleManager' exists in both 'UnityEngine.Advertisements.Editor, Version=3.6.0.0, Culture=neutral, PublicKeyToken=null' and 'UnityEngine.Advertisements, Version=3.4.6.0, Culture=neutral, PublicKeyToken=null'
第五步
打開專案後台之後照著畫面中圈起來的部分來進到廣告管理的頁面
第六步
打開來之後就可以看到如上圖中的內容,其中Banner為我另外加上去的,原先是只有上面兩種廣告
*要是該專案未安裝Advertisement的SDK則會是以下畫面
第九步
分別介紹3種廣告的code,基本上大同小異,就是在同樣基礎上做些微的修改。
1.Banner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;//使用Advertisements的SDK
public class StartBanner : MonoBehaviour
{
private string GameID_ANDROID = "399xx25";//這裡輸入廣告控制頁面中得到的
private string placementID = "Banner";//輸入要執行的廣告的ID
private bool IsAdReady = true;//是否執行的旗標
private void Start()
{
Advertisement.Initialize(GameID_ANDROID, IsAdReady);
StartCoroutine(ShowBannerWhenReady());//播放廣告時機
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);//廣告位置設定,例如"TOP_CENTER"就會變成在上方
}
IEnumerator ShowBannerWhenReady()
{
while (!Advertisement.IsReady(placementID))
{
yield return null;
}
Advertisement.Banner.Show(placementID);
}
}
效果:
2.不可跳過的那種廣告
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class ReviewVideo : MonoBehaviour, IUnityAdsListener
{
private string GameID_ANDROID = "399xx25";
private string placementID = "rewardedVideo";
private bool IsAdReady = true;
// Use this for initialization
void Start()
{
Advertisement.Initialize(GameID_ANDROID, IsAdReady);
Advertisement.AddListener(this);
}
/// <summary>
/// 開始撥放廣告
/// </summary>
public void ShowRewardAD()
{
if (Advertisement.isInitialized && Advertisement.IsReady(placementID))
{
Advertisement.Show(placementID);
}
}
/***********************************以下為監聽***********************************/
public void OnUnityAdsDidError(string message)
{
Debug.Log("廣告播放錯誤");
}
public void OnUnityAdsReady(string placementId)
{
Debug.Log("廣告載入完成");
}
public void OnUnityAdsDidStart(string placementId)
{
Debug.Log("廣告播放開始");
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
if (showResult == ShowResult.Finished)
{
Debug.Log("廣告播放完成,玩家接受獎勵");
}
if (showResult == ShowResult.Failed)
{
Debug.Log("廣告播放失敗");
}
if (showResult == ShowResult.Skipped)
{
Debug.Log("廣告播放跳過,玩家無法接受獎勵");
}
}
}
效果:
3.可跳過的那種
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class Video : MonoBehaviour, IUnityAdsListener
{
private string GameID_ANDROID = "399xx25";
private string placementID = "video";
private bool IsAdReady = true;
// Use this for initialization
void Start()
{
Advertisement.Initialize(GameID_ANDROID, IsAdReady);
Advertisement.AddListener(this);
}
/// <summary>
/// 開始撥放廣告
/// </summary>
public void ShowRewardAD()
{
if (Advertisement.isInitialized && Advertisement.IsReady(placementID))
{
Advertisement.Show(placementID);
}
}
/***********************************以下為監聽***********************************/
public void OnUnityAdsDidError(string message)
{
Debug.Log("廣告播放錯誤");
}
public void OnUnityAdsReady(string placementId)
{
Debug.Log("廣告載入完成");
}
public void OnUnityAdsDidStart(string placementId)
{
Debug.Log("廣告播放開始");
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
if (showResult == ShowResult.Finished)
{
Debug.Log("廣告播放完成,玩家接受獎勵");
}
if (showResult == ShowResult.Failed)
{
Debug.Log("廣告播放失敗");
}
if (showResult == ShowResult.Skipped)
{
Debug.Log("廣告播放跳過,玩家無法接受獎勵");
}
}
}
效果圖
=======================================================================================
到目前為止單純架設的部分就先到這邊了,關於廣告其實還有後臺分析跟應用理論...等等很多的東西,而且Unity也都有支援,不過那些就等有機會時坐在記錄囉。
先這樣,GGJ的紀錄過幾天應該會趁著有空的時候補上來。
應 應該拉!