-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdmob Manager ((ADAPTED TO UMP)
373 lines (306 loc) · 11.9 KB
/
Admob Manager ((ADAPTED TO UMP)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using System;
using UnityEngine.UI;
using TMPro;
public class AdmobManager : MonoBehaviour
{
// These ad units are configured to always serve test ads.
// Test Ad ID: ca-app-pub-3940256099942544/1033173712
#if UNITY_ANDROID
private string intersstitialAdsID = "xxx"; //FIX BEFORE PUBLISHING
#elif UNITY_IPHONE
private string _adUnitId = "xxx";
#else
private string _adUnitId = "unused";
#endif
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string rewardedInterstitialAds = "xxx"; //FIX BEFORE PUBLISHING
#elif UNITY_IPHONE
private string _adUnitId = "xxx";
#else
private string _adUnitId = "unused";
#endif
private InterstitialAd _interstitialAd;
private RewardedInterstitialAd _rewardedInterstitialAd;
[SerializeField] Button rewardedAdsButton;
[SerializeField] TextMeshProUGUI rewardedAdsButtonText;
private void Start()
{
//Due to consent, best to initialize start functions from UMP Manager
}
#region Rewards
void GrantDarts(int darts)
{
ScoreManager scoreManager = FindObjectOfType<ScoreManager>();
scoreManager.remainingDarts += darts;
Debug.Log("Grant darts initiated");
scoreManager.UpdateDartText();
scoreManager.ContinueAfterRewardedAds();
}
#endregion
//Initialize Mobile Ads
public void GoogleMobileAdsInit()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { });
}
#region InterstitialAds
/// <summary>
/// Loads the interstitial ad.
/// </summary>
public void LoadInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (_interstitialAd != null)
{
_interstitialAd.Destroy();
_interstitialAd = null;
}
Debug.Log("Loading the interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
InterstitialAd.Load(intersstitialAdsID, adRequest,
(InterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Interstitial ad loaded with response : "
+ ad.GetResponseInfo());
_interstitialAd = ad;
});
}
/// <summary>
/// Shows the interstitial ad.
/// </summary>
public void ShowInterstitialAd()
{
if (_interstitialAd != null && _interstitialAd.CanShowAd())
{
Debug.Log("Showing interstitial ad.");
_interstitialAd.Show();
RegisterEventHandlersForInterstitialAds(_interstitialAd);
}
else
{
Debug.LogError("Interstitial ad is not ready yet.");
}
}
public void RegisterEventHandlersForInterstitialAds(InterstitialAd interstitialAd)
{
// Raised when the ad is estimated to have earned money.
interstitialAd.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
interstitialAd.OnAdImpressionRecorded += () =>
{
Debug.Log("Interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
interstitialAd.OnAdClicked += () =>
{
Debug.Log("Interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
interstitialAd.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
interstitialAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial ad full screen content closed.");
_interstitialAd.Destroy();
RegisterReloadHandlerForInterstitialAd(_interstitialAd);
};
// Raised when the ad failed to open full screen content.
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
RegisterReloadHandlerForInterstitialAd(_interstitialAd);
};
}
private void RegisterReloadHandlerForInterstitialAd(InterstitialAd interstitialAd)
{
// Raised when the ad closed full screen content.
interstitialAd.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Interstitial Ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadInterstitialAd();
};
// Raised when the ad failed to open full screen content.
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Interstitial ad failed to open full screen content " +
"with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadInterstitialAd();
};
}
#endregion
#region RewardedInterstitialAds
/// <summary>
/// Loads the rewarded interstitial ad.
/// </summary>
public void LoadRewardedInterstitialAd()
{
Debug.Log("Loading the rewarded interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
adRequest.Keywords.Add("unity-admob-sample");
// send the request to load the ad.
RewardedInterstitialAd.Load(rewardedInterstitialAds, adRequest,
(RewardedInterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("rewarded interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded interstitial ad loaded with response : "
+ ad.GetResponseInfo());
_rewardedInterstitialAd = ad;
RegisterEventHandlersForRewardedInterstitialAds(_rewardedInterstitialAd);
});
}
public void ShowRewardedInterstitialAd()
{
const string rewardMsg =
"Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";
// Check if the rewardedInterstitialAd is null or cannot be shown
if (_rewardedInterstitialAd == null || !_rewardedInterstitialAd.CanShowAd())
{
Debug.Log("Ad is null or cannot be shown, loading ad...");
// Load the rewarded interstitial ad if it's not loaded.
LoadRewardedInterstitialAd();
//Display a "Try Again" message on the screen.
ShowTryAgainMessage();
// Exit the method as the ad is not ready to be shown yet.
return;
}
if (_rewardedInterstitialAd != null && _rewardedInterstitialAd.CanShowAd())
{
_rewardedInterstitialAd.Show((Reward reward) =>
{
// TODO: Reward the user.
Debug.Log("Rewarding the user x5 Darts");
int dartsAwarded = 5;
GrantDarts(dartsAwarded);
Debug.Log(string.Format(rewardMsg, dartsAwarded));
});
}
}
public void RegisterEventHandlersForRewardedInterstitialAds(RewardedInterstitialAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Rewarded interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("Rewarded interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Rewarded interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded interstitial ad full screen content closed.");
// Register reload handler to prepare for the next ad
CleanupRewardedInterstitialAd();
RegisterReloadHandlerForRewardedInterstitialAd(_rewardedInterstitialAd);
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded interstitial ad failed to open " +
"full screen content with error : " + error);
// Register reload handler to prepare for the next ad
RegisterReloadHandlerForRewardedInterstitialAd(_rewardedInterstitialAd);
};
}
public void RegisterReloadHandlerForRewardedInterstitialAd(RewardedInterstitialAd ad)
{
Debug.Log("Reload Handler for Rewarded Ads Initiated ");
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded interstitial ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadRewardedInterstitialAd();
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded interstitial ad failed to open " +
"full screen content with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadRewardedInterstitialAd();
};
}
private void CleanupRewardedInterstitialAd()
{
if (_rewardedInterstitialAd != null)
{
_rewardedInterstitialAd.Destroy();
_rewardedInterstitialAd = null; // Set to null to indicate it's no longer in use
}
}
private IEnumerator DisplayTryAgainMessage()
{
//Change the text to "Try Again!";
rewardedAdsButtonText.text = "Loading...";
//Disable button interaction for 3 seconds
rewardedAdsButton.interactable = false;
float countdownDuration = 3f; //seconds
float countdownTimer = countdownDuration;
while (countdownTimer > 0f)
{
//Update the countdown text
rewardedAdsButtonText.text = Mathf.Ceil(countdownTimer).ToString();
//Wait for the next frame
yield return null;
//Update the countdown timer
countdownTimer -= Time.deltaTime;
}
//Reset the button Text and enable button interaction.
rewardedAdsButtonText.text = "Darts x5";
rewardedAdsButton.interactable = true;
}
//Call this method when you want to display the "Try Again" message
void ShowTryAgainMessage()
{
StartCoroutine(DisplayTryAgainMessage());
}
#endregion
}