カスタムコマンドを使ってUnityAdsやAnalyticsを使うサンプル
投稿日:2016年7月2日 | 最終更新日:2023年3月29日
概要
Unityには公式機能として、動画広告を導入するUnityAdsや、ユーザーの統計データを取得するUnityAnalytics機能があります。
宴のカスタムコマンドの仕組みを使って、シナリオ上からこれらの機能を呼び出すときのやり方のサンプルが公開されていますのでご紹介します。
UnityAds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.Advertisements; | |
using UnityEngine.Assertions; | |
using UnityEngine.Events; | |
using UnityEngine.UI; | |
namespace Utage.CustomCommand | |
{ | |
public class UnityAdsCommand : AdvCustomCommandManager | |
{ | |
[SerializeField] private string _placementId = "rewardedVideo"; | |
[SerializeField] private string _gameIdIos = "1050908"; | |
[SerializeField] private string _gameIdAndroid = "1050907"; | |
[Header("OnReady Enable Button")] | |
public Button AdsButton; | |
[Header("OnReady Callback")] | |
public UnityEvent OnReadyAds; | |
[Header("OnFinished Callback")] | |
public UnityEvent OnFinishedAds; | |
[Header("OnSkipped Callback")] | |
public UnityEvent OnSkippedAds; | |
[Header("OnFailed Callback")] | |
public UnityEvent OnFailedAds; | |
void Awake() | |
{ | |
if (Advertisement.isSupported && !Advertisement.isInitialized) | |
{ | |
#if UNITY_ANDROID | |
Assert.IsNotNull (_gameIdAndroid); | |
Advertisement.Initialize(_gameIdAndroid); | |
#elif UNITY_IOS | |
Assert.IsNotNull (_gameIdIos); | |
Advertisement.Initialize(_gameIdIos); | |
#endif | |
} | |
} | |
void OnEnable() | |
{ | |
StartCoroutine(WaitForAdsReady()); | |
} | |
IEnumerator WaitForAdsReady() | |
{ | |
if (AdsButton != null) AdsButton.gameObject.SetActive(false); | |
yield return new WaitUntil(Advertisement.IsReady); | |
if (AdsButton != null) AdsButton.gameObject.SetActive(true); | |
OnReady(); | |
} | |
public void ShowUnityAds() | |
{ | |
#if UNITY_ANDROID || UNITY_IOS | |
if (Advertisement.IsReady(_placementId)) | |
{ | |
var options = new ShowOptions { resultCallback = HandleShowResult }; | |
Advertisement.Show(_placementId, options); | |
} | |
#endif | |
} | |
private void HandleShowResult(ShowResult result) | |
{ | |
switch (result) | |
{ | |
case ShowResult.Finished: | |
Debug.Log("The ad was successfully shown."); | |
OnFinished(); | |
break; | |
case ShowResult.Skipped: | |
Debug.Log("The ad was skipped before reaching the end."); | |
OnSkipped(); | |
break; | |
case ShowResult.Failed: | |
Debug.LogError("The ad failed to be shown."); | |
OnFailed(); | |
break; | |
} | |
} | |
void OnReady() | |
{ | |
OnReadyAds.Invoke(); | |
} | |
void OnFinished() | |
{ | |
OnFinishedAds.Invoke(); | |
} | |
void OnSkipped() | |
{ | |
OnSkippedAds.Invoke(); | |
} | |
void OnFailed() | |
{ | |
OnFailedAds.Invoke(); | |
} | |
public override void OnBootInit() | |
{ | |
AdvCommandParser.OnCreateCustomCommandFromID += CreateUnityAdsCommand; | |
} | |
private void CreateUnityAdsCommand(string id, StringGridRow row, AdvSettingDataManager datamanager, ref AdvCommand command) | |
{ | |
switch (id) | |
{ | |
case "ShowUnityAds": | |
command = new UnityAdsCommandShow(row); | |
break; | |
} | |
} | |
} | |
public class UnityAdsCommandShow : AdvCommand | |
{ | |
public UnityAdsCommandShow(StringGridRow row):base(row) | |
{ | |
} | |
public override void DoCommand(AdvEngine engine) | |
{ | |
engine.gameObject.GetComponent<UnityAdsCommand>().ShowUnityAds(); | |
} | |
} | |
} |
UnityAnalytics
詳細はGitHubのこちらもご確認ください。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine.Analytics; | |
namespace Utage | |
{ | |
[RequireComponent(typeof(AdvEngine))] | |
public class UnityAnalyticsCommand : AdvCustomCommandManager | |
{ | |
public static string analyticsCustomEventName = "UtageScenario"; | |
public override void OnBootInit() | |
{ | |
Utage.AdvCommandParser.OnCreateCustomCommnadFromID += CreateCustomCommand; | |
} | |
//AdvEngineのクリア処理のときに呼ばれる | |
public override void OnClear() | |
{ | |
} | |
//カスタムコマンドの作成用コールバック | |
public void CreateCustomCommand(string id, StringGridRow row, AdvSettingDataManager dataManager, ref AdvCommand command) | |
{ | |
switch (id) | |
{ | |
//新しい名前のコマンドを作る | |
case "UnityAnalytics": | |
command = new AdvCommandUnityAnalytics(row); | |
break; | |
} | |
} | |
} | |
public class AdvCommandUnityAnalytics : AdvCommand | |
{ | |
int rowNumber; | |
string name; | |
public AdvCommandUnityAnalytics(StringGridRow row) | |
: base(row) | |
{ | |
rowNumber = row.RowIndex; | |
name = row.Grid.Name; | |
} | |
//コマンド実行 | |
public override void DoCommand(AdvEngine engine) | |
{ | |
// 送信データを作成 | |
Dictionary<string, object> eventData = new Dictionary<string, object>(); | |
eventData.Add ("rowNumber", rowNumber); // 行番号 | |
eventData.Add ("name", name); // シナリオファイル・シートの名前 | |
// パラメーターを取得 | |
AdvParamStruct advParamStruct = engine.Param.GetDefault (); | |
List<AdvParamData> defaultList = advParamStruct.GetFileTypeList (AdvParamData.FileType.Default); | |
foreach (AdvParamData data in defaultList) { | |
eventData.Add (data.Key, data.Parameter); // デフォルトパラメータ | |
} | |
List<AdvParamData> systemList = advParamStruct.GetFileTypeList (AdvParamData.FileType.System); | |
foreach (AdvParamData data in systemList) { | |
eventData.Add (data.Key, data.Parameter); // システムパラメータ | |
} | |
// カスタムイベントを送信 | |
Analytics.CustomEvent (UnityAnalyticsCommand.analyticsCustomEventName, eventData); | |
} | |
} | |
} |
使い方
どちらも宴のカスタムコマンドの仕組みを使っていますので、AdvEngineにAddComponentして使ってください。
ただ、UnityAdsやUnityAnalyticsの使い方やセットアップの仕方は公式ドキュメントなどを確認してください。
将来的には、ブラッシュアップして宴の正式なコマンドとして使えるようにしようと思いますので、要望などありましたらQ&Aにご意見お願いします。UnityAdsやUnityAnalytics自体のサポートを要求されてもそれをするのは不可能なので、宴の機能にはしないことにしました。上記のプログラムもGitHubの引用です。あくまで参考にして基本的には自力でプログラムしてください。