forked from nhathuy7996/ReUseScript_Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdManager.cs
284 lines (234 loc) · 8.46 KB
/
AdManager.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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using GoogleMobileAds.Api;
public class AdManager : MonoBehaviour
{
string AppID = "ca-app-pub-9056640292640134~3154610018",//"ca-app-pub-3940256099942544~3347511713",
BannerID = "ca-app-pub-3940256099942544/6300978111",//"ca-app-pub-3940256099942544/6300978111",
InterID = "ca-app-pub-3940256099942544/1033173712",//"ca-app-pub-3940256099942544/1033173712",
VideoID = "ca-app-pub-3940256099942544/5224354917";//"ca-app-pub-3940256099942544/5224354917";
[Header("AdUnit")]
[SerializeField] private InterstitialAd interstitial = null;
private List<RewardedAd> RewardAds = new List<RewardedAd>();
[SerializeField] int MaxReward = 3;
public UnityEvent RewardCallBack = null, InterCallBack = null;
private BannerView bannerAd = null;
private bool _Banner_active = false, _InterShowed = false;
float TimeInter = 0;
[SerializeField] float MaxTimerInter = 60;
public bool Banner_active => _Banner_active;
public bool InterShowed => _InterShowed;
float DelayReloadInter = 1, DelayReloadReward = 1, DelayReloadBanner = 1;
void Start() {
MobileAds.Initialize(this.AppID);
this.RequestInterstitial();
//this.RequestBanner();
for (int i = 0; i < MaxReward; i++) {
RewardAds.Add(this.RequestReward());
}
}
void Update() {
if (_InterShowed) {
TimeInter += Time.deltaTime;
if (TimeInter >= MaxTimerInter)
{
TimeInter = 0;
_InterShowed = false;
}
}
}
#region Banner
public void RequestBanner()
{
Debug.LogError("Banner request!!");
// Create a 320x50 banner at the top of the screen.
bannerAd = new BannerView(this.BannerID, AdSize.SmartBanner, AdPosition.Top);
// Called when an ad request has successfully loaded.
bannerAd.OnAdLoaded += Banner_HandleOnAdLoaded;
// Called when an ad request failed to load.
bannerAd.OnAdFailedToLoad += Banner_HandleOnAdFailedToLoad;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
bannerAd.LoadAd(request);
}
#endregion
#region Handle Banner
public void Banner_HandleOnAdLoaded(object sender, System.EventArgs args)
{
Debug.LogError("Load banner success!");
_Banner_active = true;
}
public void Banner_HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
Debug.LogError("Load banner faild");
Invoke("RequestBanner", DelayReloadBanner);
DelayReloadBanner *= 5;
}
#endregion
#region Inter
public void RequestInterstitial()
{
if (this.interstitial != null) {
this.interstitial.Destroy();
}
// Initialize an InterstitialAd.
this.interstitial = new InterstitialAd(InterID);
// Called when an ad request has successfully loaded.
this.interstitial.OnAdLoaded += Inter_HandleOnAdLoaded;
// Called when an ad request failed to load.
this.interstitial.OnAdFailedToLoad += Inter_HandleOnAdFailedToLoad;
// Called when an ad is shown.
this.interstitial.OnAdOpening += Inter_HandleOnAdOpened;
// Called when the ad is closed.
this.interstitial.OnAdClosed += Inter_HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
this.interstitial.OnAdLeavingApplication += Inter_HandleOnAdLeavingApplication;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.interstitial.LoadAd(request);
Debug.LogError("Inter requested");
StartCoroutine(WaitLoadInter());
}
IEnumerator WaitLoadInter() {
yield return new WaitUntil(() => this.interstitial.IsLoaded());
Debug.LogError("Inter load done!");
}
public void ShowInter()
{
if (this.interstitial.IsLoaded()) {
if (_InterShowed)
return;
_InterShowed = true;
TimeInter = 0;
this.interstitial.Show();
}
}
#endregion
#region HandleInter
public void Inter_HandleOnAdLoaded(object sender, System.EventArgs args)
{
MonoBehaviour.print("Inter HandleAdLoaded event received");
}
public void Inter_HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("inter HandleFailedToReceiveAd event received with message: ");
Invoke("RequestInterstitial", DelayReloadInter);
DelayReloadInter *= 2;
}
public void Inter_HandleOnAdOpened(object sender, System.EventArgs args)
{
MonoBehaviour.print("Inter HandleAdOpened event received");
}
public void Inter_HandleOnAdClosed(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
RequestInterstitial();
if (InterCallBack != null) {
InterCallBack.Invoke();
InterCallBack.RemoveAllListeners();
}
}
public void Inter_HandleOnAdLeavingApplication(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleAdLeavingApplication event received");
RequestInterstitial();
}
#endregion
#region Reward
public RewardedAd RequestReward()
{
RewardedAd rewardedAd = new RewardedAd(VideoID);
rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
rewardedAd.OnAdClosed += HandleRewardedAdClosed;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
rewardedAd.LoadAd(request);
Debug.Log("Reward requested");
return rewardedAd;
}
public void ShowReward()
{
#if UNITY_EDITOR
if (RewardCallBack != null)
{
RewardCallBack.Invoke();
RewardCallBack.RemoveAllListeners();
}
return;
#endif
RewardedAd R = CheckAvailableReward();
if (R != null) {
R.Show();
return;
}
if (RewardCallBack != null)
{
RewardCallBack.RemoveAllListeners();
}
Debug.LogError("Not avaiable reward!");
}
public RewardedAd CheckAvailableReward() {
foreach (RewardedAd R in this.RewardAds) {
if (R.IsLoaded()) {
return R;
}
}
if (this.RewardAds.Count < MaxReward)
RewardAds.Add(this.RequestReward());
return null;
}
#endregion
#region HandleReward
public void HandleRewardedAdLoaded(object sender, System.EventArgs args)
{
Debug.Log("HandleRewardedAdLoaded event received");
}
public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
{
Debug.Log("HandleRewardedAdFailedToLoad event received with message: "
+ args.Message);
this.RewardAds.Remove((RewardedAd)sender);
Invoke("CheckAvailableReward", DelayReloadReward);
DelayReloadReward *= 2;
}
public void HandleRewardedAdOpening(object sender, System.EventArgs args)
{
Debug.Log("HandleRewardedAdOpening event received");
}
public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
{
Debug.Log("HandleRewardedAdFailedToShow event received with message: ");
if (RewardCallBack != null)
{
RewardCallBack.RemoveAllListeners();
}
}
public void HandleRewardedAdClosed(object sender, System.EventArgs args)
{
Debug.Log("HandleRewardedAdClosed event received");
this.RewardAds.Remove((RewardedAd)sender);
CheckAvailableReward();
}
public void HandleUserEarnedReward(object sender, Reward args)
{
Debug.Log("HandleRewardedAdRewarded event received for ");
if (RewardCallBack != null) {
StartCoroutine(RunRewardCallBack());
}
this.RewardAds.Remove((RewardedAd)sender);
CheckAvailableReward();
}
private IEnumerator RunRewardCallBack()
{
yield return null;
RewardCallBack.Invoke();
RewardCallBack.RemoveAllListeners();
}
#endregion
}