Skip to content

Commit ed5c678

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

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

samples/HelloWorld/Assets/Scripts/GoogleMobileAdsConsentController.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GoogleMobileAds.Common;
12
using GoogleMobileAds.Ump.Api;
23
using System;
34
using UnityEngine;
@@ -154,32 +155,37 @@ void UpdatePrivacyButton()
154155
{
155156
if (_privacyButton != null)
156157
{
157-
_privacyButton.interactable =
158-
ConsentInformation.PrivacyOptionsRequirementStatus ==
159-
PrivacyOptionsRequirementStatus.Required;
158+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
159+
{
160+
_privacyButton.interactable =
161+
ConsentInformation.PrivacyOptionsRequirementStatus ==
162+
PrivacyOptionsRequirementStatus.Required;
163+
164+
});
160165
}
161166
}
162167

163168
void UpdateErrorPopup(string message)
164169
{
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)
170+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
180171
{
181-
_privacyButton.interactable = true;
182-
}
172+
if (string.IsNullOrEmpty(message))
173+
{
174+
return;
175+
}
176+
if (_errorText != null)
177+
{
178+
_errorText.text = message;
179+
}
180+
if (_errorPopup != null)
181+
{
182+
_errorPopup.SetActive(true);
183+
}
184+
if (_privacyButton != null)
185+
{
186+
_privacyButton.interactable = true;
187+
}
188+
});
183189
}
184190
}
185-
}
191+
}

samples/HelloWorld/Assets/Scripts/GoogleMobileAdsController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ private void Start()
4141
// This setting makes iOS behave consistently with Android.
4242
MobileAds.SetiOSAppPauseOnBackground(true);
4343

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-
4944
// Configure your RequestConfiguration with Child Directed Treatment
5045
// and the Test Device Ids.
5146
MobileAds.SetRequestConfiguration(new RequestConfiguration
@@ -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
}
@@ -176,4 +176,4 @@ public void OpenPrivacyOptions()
176176
});
177177
}
178178
}
179-
}
179+
}

0 commit comments

Comments
 (0)