質問編集履歴

1

C# プログラムを追加

2019/04/14 04:46

投稿

RTera
RTera

スコア11

test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,341 @@
43
43
  Unity Game Monetization | Admob Unity Tutorial
44
44
 
45
45
  https://www.youtube.com/watch?v=tGtRx1nOiGg
46
+
47
+
48
+
49
+ ```ここに言語を入力
50
+
51
+ /* AdManager.cs */
52
+
53
+ using System.Collections;
54
+
55
+ using System.Collections.Generic;
56
+
57
+ using System;
58
+
59
+ using UnityEngine;
60
+
61
+ using GoogleMobileAds.Api;
62
+
63
+
64
+
65
+ public class ADManager : MonoBehaviour {
66
+
67
+
68
+
69
+ private string APP_ID = "ca-app-pub-3709341320635003~7864168412";
70
+
71
+
72
+
73
+ private BannerView bannerAD;
74
+
75
+ private InterstitialAd interstitialAd;
76
+
77
+ private RewardBasedVideoAd rewardVideoAd;
78
+
79
+
80
+
81
+ void Start() {
82
+
83
+
84
+
85
+ // this is when you publish your app
86
+
87
+ MobileAds.Initialize(APP_ID);
88
+
89
+
90
+
91
+ //RequestBanner();
92
+
93
+ RequestInterstitial();
94
+
95
+ RequestVideoAD();
96
+
97
+
98
+
99
+ }
100
+
101
+
102
+
103
+ void RequestBanner() {
104
+
105
+
106
+
107
+ string banner_ID = "ca-app-pub-3940256099942544/6300978111"; // テスト用のUnitID
108
+
109
+ if (bannerAD != null) {
110
+
111
+ bannerAD.Destroy();
112
+
113
+ }
114
+
115
+ bannerAD = new BannerView(banner_ID, AdSize.SmartBanner, AdPosition.Bottom);
116
+
117
+
118
+
119
+ // FOR REAL APP
120
+
121
+ //AdRequest adRequest = new AdRequest.Builder().Build();
122
+
123
+
124
+
125
+ // FOR TESTING
126
+
127
+ AdRequest adRequest = new AdRequest.Builder()
128
+
129
+ .AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
130
+
131
+
132
+
133
+ bannerAD.LoadAd(adRequest);
134
+
135
+
136
+
137
+ }
138
+
139
+
140
+
141
+ void RequestInterstitial() {
142
+
143
+
144
+
145
+ string interstitial_ID = "ca-app-pub-3940256099942544/1033173712"; // テスト用のUnitID
146
+
147
+ interstitialAd = new InterstitialAd(interstitial_ID);
148
+
149
+
150
+
151
+ // FOR REAL APP
152
+
153
+ //AdRequest adRequest = new AdRequest.Builder().Build();
154
+
155
+
156
+
157
+ // FOR TESTING
158
+
159
+ AdRequest adRequest = new AdRequest.Builder()
160
+
161
+ .AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
162
+
163
+
164
+
165
+ interstitialAd.LoadAd(adRequest);
166
+
167
+
168
+
169
+ }
170
+
171
+
172
+
173
+ void RequestVideoAD() {
174
+
175
+
176
+
177
+ string video_ID = "ca-app-pub-3940256099942544/5224354917"; // テスト用のUnitID
178
+
179
+ rewardVideoAd = RewardBasedVideoAd.Instance;
180
+
181
+
182
+
183
+ // FOR REAL APP
184
+
185
+ //AdRequest adRequest = new AdRequest.Builder().Build();
186
+
187
+
188
+
189
+ // FOR TESTING
190
+
191
+ AdRequest adRequest = new AdRequest.Builder()
192
+
193
+ .AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
194
+
195
+
196
+
197
+ rewardVideoAd.LoadAd(adRequest, video_ID);
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+ public void Display_Banner() {
206
+
207
+ bannerAD.Show();
208
+
209
+ }
210
+
211
+
212
+
213
+ public void Display_InterstitialAD() {
214
+
215
+
216
+
217
+ if (interstitialAd.IsLoaded()) {
218
+
219
+ interstitialAd.Show();
220
+
221
+ }
222
+
223
+
224
+
225
+ }
226
+
227
+
228
+
229
+ public void Display_Reward_Video() {
230
+
231
+
232
+
233
+ if(rewardVideoAd.IsLoaded()) {
234
+
235
+ rewardVideoAd.Show();
236
+
237
+ }
238
+
239
+
240
+
241
+ }
242
+
243
+
244
+
245
+ // HANDLE EVENTS
246
+
247
+
248
+
249
+ public void HandleOnAdLoaded(object sender, EventArgs args) {
250
+
251
+ // ad is loaded show it
252
+
253
+ Display_Banner();
254
+
255
+ }
256
+
257
+
258
+
259
+ public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) {
260
+
261
+ // ad failed to load load it again
262
+
263
+ RequestBanner();
264
+
265
+ }
266
+
267
+
268
+
269
+ public void HandleOnAdOpened(object sender, EventArgs args) {
270
+
271
+ MonoBehaviour.print("HandleAdOpened event received");
272
+
273
+ }
274
+
275
+
276
+
277
+ public void HandleOnAdClosed(object sender, EventArgs args) {
278
+
279
+ MonoBehaviour.print("HandleAdClosed event received");
280
+
281
+ }
282
+
283
+
284
+
285
+ public void HandleOnAdLeavingApplication(object sender, EventArgs args) {
286
+
287
+ MonoBehaviour.print("HandleAdLeavingApplication event received");
288
+
289
+ }
290
+
291
+
292
+
293
+ void HandleBannerADEvents(bool subscribe) {
294
+
295
+
296
+
297
+ if(subscribe) {
298
+
299
+
300
+
301
+ // Called when an ad request has successfully loaded.
302
+
303
+ bannerAD.OnAdLoaded += HandleOnAdLoaded;
304
+
305
+ // Called when an ad request failed to load.
306
+
307
+ bannerAD.OnAdFailedToLoad += HandleOnAdFailedToLoad;
308
+
309
+ // Called when an ad is clicked.
310
+
311
+ bannerAD.OnAdOpening += HandleOnAdOpened;
312
+
313
+ // Called when the user returned from the app after an ad click.
314
+
315
+ bannerAD.OnAdClosed += HandleOnAdClosed;
316
+
317
+ // Called when the ad click caused the user to leave the application.
318
+
319
+ bannerAD.OnAdLeavingApplication += HandleOnAdLeavingApplication;
320
+
321
+
322
+
323
+ } else {
324
+
325
+
326
+
327
+ // Called when an ad request has successfully loaded.
328
+
329
+ bannerAD.OnAdLoaded -= HandleOnAdLoaded;
330
+
331
+ // Called when an ad request failed to load.
332
+
333
+ bannerAD.OnAdFailedToLoad -= HandleOnAdFailedToLoad;
334
+
335
+ // Called when an ad is clicked.
336
+
337
+ bannerAD.OnAdOpening -= HandleOnAdOpened;
338
+
339
+ // Called when the user returned from the app after an ad click.
340
+
341
+ bannerAD.OnAdClosed -= HandleOnAdClosed;
342
+
343
+ // Called when the ad click caused the user to leave the application.
344
+
345
+ bannerAD.OnAdLeavingApplication -= HandleOnAdLeavingApplication;
346
+
347
+
348
+
349
+ }
350
+
351
+
352
+
353
+ }
354
+
355
+
356
+
357
+ void OnEnable() {
358
+
359
+ HandleBannerADEvents(true);
360
+
361
+ }
362
+
363
+
364
+
365
+ void OnDisable() {
366
+
367
+ HandleBannerADEvents(false);
368
+
369
+ }
370
+
371
+
372
+
373
+ } // class
374
+
375
+ ```
376
+
377
+
378
+
379
+ ちなみに、「バナー広告」の処理をメントアウトすると、上記のエラーは消えますが、テストデバイスと広告の両方は表示されません。
380
+
381
+
382
+
383
+ お手数ですが、何卒よろしくお願いします。