Skip to content

Instantly share code, notes, and snippets.

@yasuyuki-kamata
Created February 9, 2021 12:59
Show Gist options
  • Save yasuyuki-kamata/c1df0d0c62c9b9b99b6142a39bd0e8f6 to your computer and use it in GitHub Desktop.
Save yasuyuki-kamata/c1df0d0c62c9b9b99b6142a39bd0e8f6 to your computer and use it in GitHub Desktop.
UnityAdsを再生時にすぐ表示するサンプルスクリプト
/// UnityAdsSampleScript.cs
/// Created by Yasuyuki Kamata on 9 Feb 2021
/// Copyright © 2021 Yasuyuki Kamata. Licensed under MIT License
using System.Collections;
using UnityEditor;
using UnityEngine;
using UnityEngine.Advertisements;
public class UnityAdsSampleScript : MonoBehaviour
{
[SerializeField] private string gameIdGooglePlay = "4007355";
[SerializeField] private string gameIdAppleAppStore = "4007354";
private string _gameId;
[SerializeField] private string placementId = "rewardedVideo";
void Start()
{
#if UNITY_ANDROID || UNITY_IOS
InitAd();
StartCoroutine(ShowAdCoroutine());
#else
var buildTarget = EditorUserBuildSettings.activeBuildTarget;
Debug.Log($"Unity Adsがサポートしていないビルドターゲットです: {buildTarget}");
#endif
}
private void InitAd()
{
if (Advertisement.isInitialized) return;
#if UNITY_ANDROID
_gameId = gameIdGooglePlay;
#elif UNITY_IOS
_gameId = gameIdAppleAppStore;
#endif
Advertisement.Initialize(_gameId);
}
private IEnumerator ShowAdCoroutine()
{
yield return new WaitUntil(() => Advertisement.isInitialized);
yield return new WaitUntil(() => Advertisement.IsReady(placementId));
Advertisement.Show(placementId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment