Skip to content

Instantly share code, notes, and snippets.

@yasuyuki-kamata
Last active March 10, 2018 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasuyuki-kamata/54804142c042d9096476664a9b12bb0f to your computer and use it in GitHub Desktop.
Save yasuyuki-kamata/54804142c042d9096476664a9b12bb0f to your computer and use it in GitHub Desktop.
UnityAds for Utage CustomCommand
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment