-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RewardedAdController.cs
163 lines (147 loc) · 5.32 KB
/
RewardedAdController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using UnityEngine;
using GoogleMobileAds.Api;
namespace GoogleMobileAds.Sample
{
/// <summary>
/// Demonstrates how to use Google Mobile Ads rewarded ads.
/// </summary>
[AddComponentMenu("GoogleMobileAds/Samples/RewardedAdController")]
public class RewardedAdController : MonoBehaviour
{
/// <summary>
/// UI element activated when an ad is ready to show.
/// </summary>
public GameObject AdLoadedStatus;
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private const string _adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
private const string _adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
private const string _adUnitId = "unused";
#endif
private RewardedAd _rewardedAd;
/// <summary>
/// Loads the ad.
/// </summary>
public void LoadAd()
{
// Clean up the old ad before loading a new one.
if (_rewardedAd != null)
{
DestroyAd();
}
Debug.Log("Loading rewarded ad.");
// Create our request used to load the ad.
var adRequest = new AdRequest();
// Send the request to load the ad.
RewardedAd.Load(_adUnitId, adRequest, (RewardedAd ad, LoadAdError error) =>
{
// If the operation failed with a reason.
if (error != null)
{
Debug.LogError("Rewarded ad failed to load an ad with error : " + error);
return;
}
// If the operation failed for unknown reasons.
// This is an unexpected error, please report this bug if it happens.
if (ad == null)
{
Debug.LogError("Unexpected error: Rewarded load event fired with null ad and null error.");
return;
}
// The operation completed successfully.
Debug.Log("Rewarded ad loaded with response : " + ad.GetResponseInfo());
_rewardedAd = ad;
// Register to ad events to extend functionality.
RegisterEventHandlers(ad);
// Inform the UI that the ad is ready.
AdLoadedStatus?.SetActive(true);
});
}
/// <summary>
/// Shows the ad.
/// </summary>
public void ShowAd()
{
if (_rewardedAd != null && _rewardedAd.CanShowAd())
{
Debug.Log("Showing rewarded ad.");
_rewardedAd.Show((Reward reward) =>
{
Debug.Log(String.Format("Rewarded ad granted a reward: {0} {1}",
reward.Amount,
reward.Type));
});
}
else
{
Debug.LogError("Rewarded ad is not ready yet.");
}
// Inform the UI that the ad is not ready.
AdLoadedStatus?.SetActive(false);
}
/// <summary>
/// Destroys the ad.
/// </summary>
public void DestroyAd()
{
if (_rewardedAd != null)
{
Debug.Log("Destroying rewarded ad.");
_rewardedAd.Destroy();
_rewardedAd = null;
}
// Inform the UI that the ad is not ready.
AdLoadedStatus?.SetActive(false);
}
/// <summary>
/// Logs the ResponseInfo.
/// </summary>
public void LogResponseInfo()
{
if (_rewardedAd != null)
{
var responseInfo = _rewardedAd.GetResponseInfo();
UnityEngine.Debug.Log(responseInfo);
}
}
private void RegisterEventHandlers(RewardedAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Rewarded ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Rewarded ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("Rewarded ad was clicked.");
};
// Raised when the ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Rewarded ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded ad failed to open full screen content with error : "
+ error);
};
}
}
}