Skip to content

Commit 078c1db

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

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

samples/HelloWorld/Assets/Scripts/GoogleMobileAdsConsentController.cs

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

167163
void UpdateErrorPopup(string message)
168164
{
169-
MobileAdsEventExecutor.ExecuteInUpdate(() =>
165+
if (string.IsNullOrEmpty(message))
170166
{
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-
});
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)
180+
{
181+
_privacyButton.interactable = true;
182+
}
188183
}
189184
}
190185
}

samples/HelloWorld/Assets/Scripts/GoogleMobileAdsController.cs

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

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

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+
4549
// Configure your RequestConfiguration with Child Directed Treatment
4650
// and the Test Device Ids.
4751
MobileAds.SetRequestConfiguration(new RequestConfiguration
@@ -101,6 +105,7 @@ private void InitializeGoogleMobileAds()
101105

102106
// Initialize the Google Mobile Ads Unity plugin.
103107
Debug.Log("Google Mobile Ads Initializing.");
108+
104109
// [START initialize_sdk]
105110
MobileAds.Initialize((InitializationStatus initstatus) =>
106111
{
@@ -126,11 +131,6 @@ private void InitializeGoogleMobileAds()
126131

127132
Debug.Log("Google Mobile Ads initialization complete.");
128133
_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)