序文
ここでは、UnityAds を使用して利用可能な広告をプロジェクトに追加することを選択します
UnityAds を選ぶ理由:
第一に、Unityは公式に中国でローカルに使用できる広告を追加しているため、広告を閲覧するためのはしごは必要ありません.第二に、Unity内で広告を追加するプロセスは非常に簡単で、基本的にはUnityで解決できます.このコンポーネントが付属しているため、面倒な外部インポートは必要ありません
プロセス
一般的なプロセスは3つのステップに分かれています
1. 広告コンポーネントを読み込む
2. 独自の広告アカウント ID を設定する
3. 広告読み込みプロセスを実装する
まず、広告コンポーネントを読み込む方法は 2 つあります。
1. Unity で Ctrl+0 をクリックして、ページをすばやく開きます
2. UnityPackageManager で Advertisement を検索し、コンポーネントをインポートする
まず、アカウントが必要です。以下のリンクでアカウントと広告 ID を登録してください。
登録後、次のページにアクセスしてください。
その中で、GameId は初期化する必要がある ID であり、これはアカウントに接続するためのゲームの ID です。
以下は、通常広告、リワード広告、バナー広告の 3 種類の広告です。ここでは、テストとしてリワード広告を選択します。
公式ドキュメントはそれを非常に明確に説明しています。これは公式ドキュメントからコピーされたコードです
使用例:
using UnityEngine;
using UnityEngine.Advertisements;
public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
{
[SerializeField] string _androidGameId;
[SerializeField] string _iOsGameId;
[SerializeField] bool _testMode = true;
[SerializeField] bool _enablePerPlacementMode = true;
private string _gameId;
void Awake()
{
InitializeAds();
}
public void InitializeAds()
{
_gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOsGameId
: _androidGameId;
Advertisement.Initialize(_gameId, _testMode, _enablePerPlacementMode, this);
}
public void OnInitializationComplete()
{
Debug.Log("Unity Ads initialization complete.");
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
}
}
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
using System.Collections;
public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
[SerializeField] Button _showAdButton;
[SerializeField] string _androidAdUnitId = "Rewarded_Android";
[SerializeField] string _iOsAdUnitId = "Rewarded_iOS";
public Text text;
string _adUnitId;
void Awake()
{
// Get the Ad Unit ID for the current platform:
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOsAdUnitId
: _androidAdUnitId;
//Disable button until ad is ready to show
//_showAdButton.interactable = false;
}
IEnumerator Load()
{
while (true)
{
if (!Advertisement.IsReady(_adUnitId))
{
LoadAd();
}
yield return new WaitForSeconds(20f);
}
}
private void Start()
{
_showAdButton.onClick.AddListener(ShowAd);
StartCoroutine(Load());
}
// Load content to the Ad Unit:
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
Debug.Log("Loading Ad: " + _adUnitId);
Advertisement.Load(_adUnitId, this);
//Invoke(nameof(ShowAd), 1f);
}
// If the ad successfully loads, add a listener to the button and enable it:
public void OnUnityAdsAdLoaded(string adUnitId)
{
Debug.Log("Ad Loaded: " + adUnitId);
if (adUnitId.Equals(_adUnitId))
{
// Configure the button to call the ShowAd() method when clicked:
_showAdButton.onClick.AddListener(ShowAd);
// Enable the button for users to click:
_showAdButton.interactable = true;
}
}
// Implement a method to execute when the user clicks the button.
public void ShowAd()
{
if (!Advertisement.IsReady(_adUnitId))
{
LoadAd();
}
//_showAdButton.interactable = false;
ShowOptions options = new ShowOptions { resultCallback = HandleShowResult };
//Advertisement.Show("rewardedVideo", options);
// Disable the button:
// Then show the ad:
Advertisement.Show(_adUnitId, options);
//print();
}
private void HandleShowResult(ShowResult result)
{
_showAdButton.interactable = true;
switch (result)
{
//广告看完
case ShowResult.Finished:
print("广告加载成功");
text.text = "Success";
//广告看完了,给玩家奖励
break;
//跳过广告
case ShowResult.Skipped:
print("广告完全加载成功了吗啊");
text.text = "Failed";
break;
case ShowResult.Failed:
text.text = "Failed";
print("广告完全加载成功了吗啊");
break;
}
}
// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
{
Debug.Log("Unity Ads Rewarded Ad Completed");
// Grant a reward.
// Load another ad:
Advertisement.Load(_adUnitId, this);
text.text = "SUccess";
print("广告完全加载成功了吗啊");
}
}
// Implement Load and Show Listener error callbacks:
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
text.text = "Failed";
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowStart(string adUnitId) { text.text = "Strt"; }
public void OnUnityAdsShowClick(string adUnitId) { text.text = "Click"; }
void OnDestroy()
{
// Clean up the button listeners:
_showAdButton.onClick.RemoveAllListeners();
}
}
2 つのスクリプトをマウントし、GameID を入力し、直接使用するボタン ボタンを追加します。