-
-
Save maio-endo/55fa4db85ee7503b36a9349f63670986 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Main : MonoBehaviour { | |
#if UNITY_IOS | |
public const string MEDIA_ID = "DemoPublisherMedia"; | |
public const string ZONE_ID = "DemoPublisherZone"; | |
#elif UNITY_ANDROID | |
public const string MEDIA_ID = "DemoPublisherMediaForAndroid"; | |
public const string ZONE_ID = "DemoPublisherZoneForAndroid"; | |
#else | |
public const string MEDIA_ID = "DemoPublisherMedia"; | |
public const string ZONE_ID = "DemoPublisherZone"; | |
#endif | |
[SerializeField] private Text _showAdButtonText; | |
[SerializeField] private Text _getCoinText; | |
[SerializeField] private GameObject _msgPanel; | |
//コインの数を格納する | |
private int _count = 0; | |
private readonly Queue<Action> _mainQueue = new Queue<Action> (); | |
private void Start () { | |
// イベントの登録を行います。 | |
// これによって、Plugin からのイベント通知を取得することが可能になります。 | |
Maio.OnChangedCanShow += HandleOnChangedCanShow; | |
Maio.OnFinishedAd += HandleOnFinishedAd; | |
// maio SDKの初期化を開始します。 | |
Maio.Start (MEDIA_ID); | |
_showAdButtonText.text = "ロード中・・・"; | |
} | |
private void Update () { | |
while (_mainQueue.Count > 0) { | |
lock (_mainQueue) { | |
var action = _mainQueue.Dequeue (); | |
action (); | |
} | |
} | |
} | |
private void OnDestroy () { | |
// シーンが遷移する際にイベントハンドラを解除します。 | |
Maio.OnChangedCanShow -= HandleOnChangedCanShow; | |
Maio.OnFinishedAd -= HandleOnFinishedAd; | |
} | |
/// <summary> | |
/// ボタンがタップされた際に呼び出されます。 | |
/// </summary> | |
public void OnShowAd () { | |
if (Maio.CanShow (ZONE_ID)) { | |
Maio.Show (ZONE_ID); | |
} | |
} | |
/// <summary> | |
/// 広告の配信可能状態が変更された際に呼ばれます。 | |
/// </summary> | |
/// <param name="zoneId">ゾーンID</param> | |
/// <param name="newValue">配信可能状態</param> | |
private void HandleOnChangedCanShow (string zoneId, bool newValue) { | |
lock (_mainQueue) { | |
_mainQueue.Enqueue (() => { | |
if (zoneId == ZONE_ID) { | |
if (newValue) { | |
_showAdButtonText.text = "動画広告を見て\nコインをもらおう!"; | |
} else { | |
_showAdButtonText.text = "ロード中・・・"; | |
} | |
} | |
}); | |
} | |
} | |
/// <summary> | |
/// 広告の視聴が終了した際に呼ばれます。 | |
/// </summary> | |
/// <param name="zoneId">ゾーンID</param> | |
/// <param name="playtime">動画の再生時間</param> | |
/// <param name="skipped">スキップされた場合 true</param> | |
/// <param name="rewardParam">(未使用)</param> | |
private void HandleOnFinishedAd (string zoneId, int playtime, bool skipped, string rewardParam) { | |
lock (_mainQueue) { | |
_mainQueue.Enqueue (() => { | |
if (zoneId == ZONE_ID) { | |
_msgPanel.SetActive(true); | |
_count += 1; | |
_getCoinText.text = "Get Coin! " + _count; | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment