Created
April 13, 2016 05:39
-
-
Save yasuyuki-kamata/b83c2de4cf3727725dd5de2bf2ce023f to your computer and use it in GitHub Desktop.
[Sample] UnityAds with AdMobMediation for Unity C#
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 GoogleMobileAds.Api; | |
using UnityEngine.Events; | |
public class AdMobMediationTest : MonoBehaviour | |
{ | |
[SerializeField] | |
string adUnitId_Android = "ca-app-pub-3504169427024836/4346441108"; | |
[SerializeField] | |
string adUnitId_iOS = "ca-app-pub-3504169427024836/9437618705"; | |
public UnityEvent OnRewardedVideoLoaded; | |
public UnityEvent OnRewardedBasedVideoFailedToLoad; | |
public UnityEvent OnRewardBasedVideoOpened; | |
public UnityEvent OnRewardBasedVideoStarted; | |
public UnityEvent OnRewardBasedVideoRewarded; | |
public UnityEvent OnRewardBasedVideoClosed; | |
public UnityEvent OnRewardBasedVideoLeftApplication; | |
private RewardBasedVideoAd mRewardedBasedVideo; | |
private bool rewardBasedEventHandlersSet = false; | |
AdRequest m_request; | |
void Start () | |
{ | |
RequestRewardBasedVideo (); | |
} | |
private void RequestRewardBasedVideo () | |
{ | |
#if UNITY_EDITOR | |
string adUnitId = "unused"; | |
#elif UNITY_ANDROID | |
string adUnitId = adUnitId_Android; | |
#elif UNITY_IPHONE | |
string adUnitId = adUnitId_iOS; | |
#else | |
string adUnitId = "unexpected_platform"; | |
#endif | |
mRewardedBasedVideo = RewardBasedVideoAd.Instance; | |
m_request = m_request ?? new AdRequest.Builder().Build(); | |
// Reward based video instance is a singleton. Register handlers once to | |
// avoid duplicate events. | |
if (!rewardBasedEventHandlersSet) { | |
// Ad event fired when the rewarded video ad | |
// has been received. | |
mRewardedBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded; | |
// has failed to load. | |
mRewardedBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad; | |
// is opened. | |
mRewardedBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened; | |
// has started playing. | |
mRewardedBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted; | |
// has rewarded the user. | |
mRewardedBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded; | |
// is closed. | |
mRewardedBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed; | |
// is leaving the application. | |
mRewardedBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication; | |
rewardBasedEventHandlersSet = true; | |
} | |
mRewardedBasedVideo.LoadAd(m_request, adUnitId); | |
} | |
void HandleRewardBasedVideoLoaded (object sender, System.EventArgs e) | |
{ | |
OnRewardedVideoLoaded.Invoke (); | |
Debug.Log ("OnRewardedVideoLoaded()", this); | |
} | |
void HandleRewardBasedVideoFailedToLoad (object sender, AdFailedToLoadEventArgs e) | |
{ | |
OnRewardedBasedVideoFailedToLoad.Invoke (); | |
Debug.Log ("OnRewardedBasedVideoFailedToLoad()", this); | |
} | |
void HandleRewardBasedVideoOpened (object sender, System.EventArgs e) | |
{ | |
OnRewardBasedVideoOpened.Invoke (); | |
Debug.Log ("OnRewardBasedVideoOpened()", this); | |
} | |
void HandleRewardBasedVideoStarted (object sender, System.EventArgs e) | |
{ | |
OnRewardBasedVideoStarted.Invoke (); | |
Debug.Log ("OnRewardBasedVideoStarted()", this); | |
} | |
void HandleRewardBasedVideoRewarded (object sender, Reward e) | |
{ | |
OnRewardBasedVideoRewarded.Invoke (); | |
Debug.Log ("OnRewardBasedVideoRewarded()", this); | |
} | |
void HandleRewardBasedVideoClosed (object sender, System.EventArgs e) | |
{ | |
OnRewardBasedVideoClosed.Invoke (); | |
Debug.Log ("OnRewardBasedVideoClosed()", this); | |
RequestRewardBasedVideo (); | |
} | |
void HandleRewardBasedVideoLeftApplication (object sender, System.EventArgs e) | |
{ | |
OnRewardBasedVideoLeftApplication.Invoke (); | |
Debug.Log ("OnRewardBasedVideoLeftApplication()", this); | |
} | |
public void ShowRewardedVideo () | |
{ | |
if (mRewardedBasedVideo.IsLoaded()) { | |
Debug.Log ("ShowRewardedVideo()", this); | |
mRewardedBasedVideo.Show (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment