-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBannerViewController.cs
More file actions
190 lines (171 loc) · 6.09 KB
/
BannerViewController.cs
File metadata and controls
190 lines (171 loc) · 6.09 KB
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
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
namespace GoogleMobileAds.Sample
{
/// <summary>
/// Demonstrates how to use Google Mobile Ads banner views.
/// </summary>
[AddComponentMenu("GoogleMobileAds/Samples/BannerViewController")]
public class BannerViewController : 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/6300978111";
#elif UNITY_IPHONE
private const string _adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
private const string _adUnitId = "unused";
#endif
private BannerView _bannerView;
/// <summary>
/// Creates a 320x50 banner at top of the screen.
/// </summary>
public void CreateBannerView()
{
Debug.Log("Creating banner view.");
// If we already have a banner, destroy the old one.
if(_bannerView != null)
{
DestroyAd();
}
// Create a 320x50 banner at top of the screen.
_bannerView = new BannerView(_adUnitId, AdSize.Banner, AdPosition.Top);
// Listen to events the banner may raise.
ListenToAdEvents();
Debug.Log("Banner view created.");
}
/// <summary>
/// Creates the banner view and loads a banner ad.
/// </summary>
public void LoadAd()
{
// Create an instance of a banner view first.
if(_bannerView == null)
{
CreateBannerView();
}
// Create our request used to load the ad.
var adRequest = new AdRequest();
// Send the request to load the ad.
Debug.Log("Loading banner ad.");
_bannerView.LoadAd(adRequest);
}
/// <summary>
/// Shows the ad.
/// </summary>
public void ShowAd()
{
if (_bannerView != null)
{
Debug.Log("Showing banner view.");
_bannerView.Show();
}
}
/// <summary>
/// Hides the ad.
/// </summary>
public void HideAd()
{
if (_bannerView != null)
{
Debug.Log("Hiding banner view.");
_bannerView.Hide();
}
}
/// <summary>
/// Destroys the ad.
/// When you are finished with a BannerView, make sure to call
/// the Destroy() method before dropping your reference to it.
/// </summary>
public void DestroyAd()
{
if (_bannerView != null)
{
Debug.Log("Destroying banner view.");
_bannerView.Destroy();
_bannerView = null;
}
// Inform the UI that the ad is not ready.
AdLoadedStatus?.SetActive(false);
}
/// <summary>
/// Logs the ResponseInfo.
/// </summary>
public void LogResponseInfo()
{
if (_bannerView != null)
{
var responseInfo = _bannerView.GetResponseInfo();
if (responseInfo != null)
{
UnityEngine.Debug.Log(responseInfo);
}
}
}
/// <summary>
/// Listen to events the banner may raise.
/// </summary>
private void ListenToAdEvents()
{
// Raised when an ad is loaded into the banner view.
_bannerView.OnBannerAdLoaded += () =>
{
Debug.Log("Banner view loaded an ad with response : "
+ _bannerView.GetResponseInfo());
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// Inform the UI that the ad is ready.
AdLoadedStatus?.SetActive(true);
});
};
// Raised when an ad fails to load into the banner view.
_bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
{
Debug.LogError("Banner view failed to load an ad with error : " + error);
};
// Raised when the ad is estimated to have earned money.
_bannerView.OnAdPaid += (AdValue adValue) =>
{
// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log(String.Format("Banner view paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// This callback may be delayed on Android until the user
// returns to the app. Place all code that interacts with
// Unity UI and GameObjects inside this callback.
Debug.Log("Banner view paid callback " +
"invoked inside ExecuteInUpdate.");
});
};
// Raised when an impression is recorded for an ad.
_bannerView.OnAdImpressionRecorded += () =>
{
Debug.Log("Banner view recorded an impression.");
};
// Raised when a click is recorded for an ad.
_bannerView.OnAdClicked += () =>
{
Debug.Log("Banner view was clicked.");
};
// Raised when an ad opened full screen content.
_bannerView.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Banner view full screen content opened.");
};
// Raised when the ad closed full screen content.
_bannerView.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Banner view full screen content closed.");
};
}
}
}