Skip to content

Commit 72a5f19

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added defensive EventExecutor to prevent potential threading issues.
PiperOrigin-RevId: 783805357
1 parent 631855c commit 72a5f19

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

samples/HelloWorld/Assets/Scripts/GoogleMobileAdsConsentController.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,32 +154,37 @@ void UpdatePrivacyButton()
154154
{
155155
if (_privacyButton != null)
156156
{
157-
_privacyButton.interactable =
158-
ConsentInformation.PrivacyOptionsRequirementStatus ==
159-
PrivacyOptionsRequirementStatus.Required;
157+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
158+
{
159+
_privacyButton.interactable =
160+
ConsentInformation.PrivacyOptionsRequirementStatus ==
161+
PrivacyOptionsRequirementStatus.Required;
162+
163+
});
160164
}
161165
}
162166

163167
void UpdateErrorPopup(string message)
164168
{
165-
if (string.IsNullOrEmpty(message))
166-
{
167-
return;
168-
}
169-
170-
if (_errorText != null)
171-
{
172-
_errorText.text = message;
173-
}
174-
175-
if (_errorPopup != null)
176-
{
177-
_errorPopup.SetActive(true);
178-
}
179-
if (_privacyButton != null)
169+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
180170
{
181-
_privacyButton.interactable = true;
182-
}
171+
if (string.IsNullOrEmpty(message))
172+
{
173+
return;
174+
}
175+
if (_errorText != null)
176+
{
177+
_errorText.text = message;
178+
}
179+
if (_errorPopup != null)
180+
{
181+
_errorPopup.SetActive(true);
182+
}
183+
if (_privacyButton != null)
184+
{
185+
_privacyButton.interactable = true;
186+
}
187+
});
183188
}
184189
}
185190
}

samples/HelloWorld/Assets/Scripts/GoogleMobileAdsController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using UnityEngine;
4+
using GoogleMobileAds.Common;
45
using GoogleMobileAds.Api;
56
using GoogleMobileAds.Ump.Api;
67

@@ -41,11 +42,6 @@ private void Start()
4142
// This setting makes iOS behave consistently with Android.
4243
MobileAds.SetiOSAppPauseOnBackground(true);
4344

44-
// When true all events raised by GoogleMobileAds will be raised
45-
// on the Unity main thread. The default value is false.
46-
// https://developers.google.com/admob/unity/quick-start#raise_ad_events_on_the_unity_main_thread
47-
MobileAds.RaiseAdEventsOnUnityMainThread = true;
48-
4945
// Configure your RequestConfiguration with Child Directed Treatment
5046
// and the Test Device Ids.
5147
MobileAds.SetRequestConfiguration(new RequestConfiguration
@@ -105,7 +101,6 @@ private void InitializeGoogleMobileAds()
105101

106102
// Initialize the Google Mobile Ads Unity plugin.
107103
Debug.Log("Google Mobile Ads Initializing.");
108-
109104
// [START initialize_sdk]
110105
MobileAds.Initialize((InitializationStatus initstatus) =>
111106
{
@@ -131,6 +126,11 @@ private void InitializeGoogleMobileAds()
131126

132127
Debug.Log("Google Mobile Ads initialization complete.");
133128
_isInitialized = true;
129+
130+
// Google Mobile Ads events are raised off the Unity Main thread. If you need to
131+
// access UnityEngine objects after initialization,
132+
// use MobileAdsEventExecutor.ExecuteInUpdate(). For more information, see:
133+
// https://developers.google.com/admob/unity/global-settings#raise_ad_events_on_the_unity_main_thread
134134
});
135135
// [END initialize_sdk]
136136
}

0 commit comments

Comments
 (0)