-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGoogleMobileAdsConsentController.cs
More file actions
192 lines (173 loc) · 6.52 KB
/
GoogleMobileAdsConsentController.cs
File metadata and controls
192 lines (173 loc) · 6.52 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
191
192
using GoogleMobileAds.Common;
using GoogleMobileAds.Ump.Api;
using System;
using UnityEngine;
using UnityEngine.UI;
namespace GoogleMobileAds.Samples
{
/// <summary>
/// Helper class that implements consent using the Google User Messaging Platform (UMP) SDK.
/// </summary>
public class GoogleMobileAdsConsentController : MonoBehaviour
{
/// <summary>
/// If true, it is safe to call MobileAds.Initialize() and load Ads.
/// Test
/// </summary>
public bool CanRequestAds => ConsentInformation.CanRequestAds();
[SerializeField, Tooltip("Button to show user consent and privacy settings.")]
private Button _privacyButton;
[SerializeField, Tooltip("GameObject with the error popup.")]
private GameObject _errorPopup;
[SerializeField, Tooltip("Error message for the error popup,")]
private Text _errorText;
private void Start()
{
// Disable the error popup,
if (_errorPopup != null)
{
_errorPopup.SetActive(false);
}
}
/// <summary>
/// Startup method for the Google User Messaging Platform (UMP) SDK
/// which will run all startup logic including loading any required
/// updates and displaying any required forms.
/// </summary>
public void GatherConsent(Action<string> onComplete)
{
Debug.Log("Gathering consent.");
var requestParameters = new ConsentRequestParameters
{
// False means users are not under age.
TagForUnderAgeOfConsent = false,
ConsentDebugSettings = new ConsentDebugSettings
{
// For debugging consent settings by geography.
DebugGeography = DebugGeography.Disabled,
// https://developers.google.com/admob/unity/test-ads
TestDeviceHashedIds = GoogleMobileAdsController.TestDeviceIds,
}
};
// Combine the callback with an error popup handler.
onComplete = (onComplete == null)
? UpdateErrorPopup
: onComplete + UpdateErrorPopup;
// The Google Mobile Ads SDK provides the User Messaging Platform (Google's
// IAB Certified consent management platform) as one solution to capture
// consent for users in GDPR impacted countries. This is an example and
// you can choose another consent management platform to capture consent.
ConsentInformation.Update(requestParameters, (FormError updateError) =>
{
// Enable the change privacy settings button.
UpdatePrivacyButton();
if (updateError != null)
{
onComplete(updateError.Message);
return;
}
// Determine the consent-related action to take based on the ConsentStatus.
if (CanRequestAds)
{
// Consent has already been gathered or not required.
// Return control back to the user.
onComplete(null);
return;
}
// Consent not obtained and is required.
// Load the initial consent request form for the user.
ConsentForm.LoadAndShowConsentFormIfRequired((FormError showError) =>
{
UpdatePrivacyButton();
if (showError != null)
{
// Form showing failed.
if (onComplete != null)
{
onComplete(showError.Message);
}
}
// Form showing succeeded.
else if (onComplete != null)
{
onComplete(null);
}
});
});
}
/// <summary>
/// Shows the privacy options form to the user.
/// </summary>
/// <remarks>
/// Your app needs to allow the user to change their consent status at any time.
/// Load another form and store it to allow the user to change their consent status
/// </remarks>
public void ShowPrivacyOptionsForm(Action<string> onComplete)
{
Debug.Log("Showing privacy options form.");
// combine the callback with an error popup handler.
onComplete = (onComplete == null)
? UpdateErrorPopup
: onComplete + UpdateErrorPopup;
ConsentForm.ShowPrivacyOptionsForm((FormError showError) =>
{
UpdatePrivacyButton();
if (showError != null)
{
// Form showing failed.
if (onComplete != null)
{
onComplete(showError.Message);
}
}
// Form showing succeeded.
else if (onComplete != null)
{
onComplete(null);
}
});
}
/// <summary>
/// Reset ConsentInformation for the user.
/// </summary>
public void ResetConsentInformation()
{
ConsentInformation.Reset();
UpdatePrivacyButton();
}
void UpdatePrivacyButton()
{
if (_privacyButton != null)
{
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
_privacyButton.interactable =
ConsentInformation.PrivacyOptionsRequirementStatus ==
PrivacyOptionsRequirementStatus.Required;
});
}
}
void UpdateErrorPopup(string message)
{
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
if (string.IsNullOrEmpty(message))
{
return;
}
if (_errorText != null)
{
_errorText.text = message;
}
if (_errorPopup != null)
{
_errorPopup.SetActive(true);
}
if (_privacyButton != null)
{
_privacyButton.interactable = true;
}
});
}
}
}