Skip to content

Commit 7d7b731

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 937613673
1 parent 3ae3490 commit 7d7b731

7 files changed

Lines changed: 174 additions & 18 deletions

samples/HelloWorld/Assets/Scripts/AppOpenAdController.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ public void LoadAd()
9090
RegisterEventHandlers(ad);
9191

9292
// Inform the UI that the ad is ready.
93-
AdLoadedStatus?.SetActive(true);
93+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
94+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
95+
{
96+
AdLoadedStatus?.SetActive(true);
97+
});
9498
});
9599
}
96100

@@ -158,9 +162,20 @@ private void RegisterEventHandlers(AppOpenAd ad)
158162
// Raised when the ad is estimated to have earned money.
159163
ad.OnAdPaid += (AdValue adValue) =>
160164
{
165+
// This log is executed off the Unity main thread.
166+
// Write all time-sensitive code before ExecuteInUpdate().
161167
Debug.Log(String.Format("App open ad paid {0} {1}.",
162168
adValue.Value,
163169
adValue.CurrencyCode));
170+
171+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
172+
{
173+
// This callback may be delayed on Android until the user
174+
// returns to the app. Place all code that interacts with
175+
// Unity UI and GameObjects inside this callback.
176+
Debug.Log("App open ad paid callback " +
177+
"invoked inside ExecuteInUpdate.");
178+
});
164179
};
165180
// Raised when an impression is recorded for an ad.
166181
ad.OnAdImpressionRecorded += () =>
@@ -177,8 +192,11 @@ private void RegisterEventHandlers(AppOpenAd ad)
177192
{
178193
Debug.Log("App open ad full screen content opened.");
179194

180-
// Inform the UI that the ad is consumed and not ready.
181-
AdLoadedStatus?.SetActive(false);
195+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
196+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
197+
{
198+
AdLoadedStatus?.SetActive(false);
199+
});
182200
};
183201
// Raised when the ad closed full screen content.
184202
ad.OnAdFullScreenContentClosed += () =>
@@ -192,7 +210,13 @@ private void RegisterEventHandlers(AppOpenAd ad)
192210
ad.OnAdFullScreenContentFailed += (AdError error) =>
193211
{
194212
Debug.LogError("App open ad failed to open full screen content with error : "
195-
+ error);
213+
+ error);
214+
215+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
216+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
217+
{
218+
AdLoadedStatus?.SetActive(false);
219+
});
196220
};
197221
}
198222
}

samples/HelloWorld/Assets/Scripts/BannerViewController.cs

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

56
namespace GoogleMobileAds.Sample
67
{
@@ -135,8 +136,11 @@ private void ListenToAdEvents()
135136
Debug.Log("Banner view loaded an ad with response : "
136137
+ _bannerView.GetResponseInfo());
137138

138-
// Inform the UI that the ad is ready.
139-
AdLoadedStatus?.SetActive(true);
139+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
140+
{
141+
// Inform the UI that the ad is ready.
142+
AdLoadedStatus?.SetActive(true);
143+
});
140144
};
141145
// Raised when an ad fails to load into the banner view.
142146
_bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
@@ -146,9 +150,20 @@ private void ListenToAdEvents()
146150
// Raised when the ad is estimated to have earned money.
147151
_bannerView.OnAdPaid += (AdValue adValue) =>
148152
{
153+
// This log is executed off the Unity main thread.
154+
// Write all time-sensitive code before ExecuteInUpdate().
149155
Debug.Log(String.Format("Banner view paid {0} {1}.",
150156
adValue.Value,
151157
adValue.CurrencyCode));
158+
159+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
160+
{
161+
// This callback may be delayed on Android until the user
162+
// returns to the app. Place all code that interacts with
163+
// Unity UI and GameObjects inside this callback.
164+
Debug.Log("Banner view paid callback " +
165+
"invoked inside ExecuteInUpdate.");
166+
});
152167
};
153168
// Raised when an impression is recorded for an ad.
154169
_bannerView.OnAdImpressionRecorded += () =>

samples/HelloWorld/Assets/Scripts/InterstitialAdController.cs

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

56
namespace GoogleMobileAds.Sample
67
{
@@ -67,7 +68,11 @@ public void LoadAd()
6768
RegisterEventHandlers(ad);
6869

6970
// Inform the UI that the ad is ready.
70-
AdLoadedStatus?.SetActive(true);
71+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
72+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
73+
{
74+
AdLoadedStatus?.SetActive(true);
75+
});
7176
});
7277
}
7378

@@ -85,9 +90,6 @@ public void ShowAd()
8590
{
8691
Debug.LogError("Interstitial ad is not ready yet.");
8792
}
88-
89-
// Inform the UI that the ad is not ready.
90-
AdLoadedStatus?.SetActive(false);
9193
}
9294

9395
/// <summary>
@@ -123,9 +125,20 @@ private void RegisterEventHandlers(InterstitialAd ad)
123125
// Raised when the ad is estimated to have earned money.
124126
ad.OnAdPaid += (AdValue adValue) =>
125127
{
128+
// This log is executed off the Unity main thread.
129+
// Write all time-sensitive code before ExecuteInUpdate().
126130
Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
127131
adValue.Value,
128132
adValue.CurrencyCode));
133+
134+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
135+
{
136+
// This callback may be delayed on Android until the user
137+
// returns to the app. Place all code that interacts with
138+
// Unity UI and GameObjects inside this callback.
139+
Debug.Log("Interstitial ad paid callback " +
140+
"invoked inside ExecuteInUpdate.");
141+
});
129142
};
130143
// Raised when an impression is recorded for an ad.
131144
ad.OnAdImpressionRecorded += () =>
@@ -141,6 +154,12 @@ private void RegisterEventHandlers(InterstitialAd ad)
141154
ad.OnAdFullScreenContentOpened += () =>
142155
{
143156
Debug.Log("Interstitial ad full screen content opened.");
157+
158+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
159+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
160+
{
161+
AdLoadedStatus?.SetActive(false);
162+
});
144163
};
145164
// Raised when the ad closed full screen content.
146165
ad.OnAdFullScreenContentClosed += () =>
@@ -152,6 +171,12 @@ private void RegisterEventHandlers(InterstitialAd ad)
152171
{
153172
Debug.LogError("Interstitial ad failed to open full screen content with error : "
154173
+ error);
174+
175+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
176+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
177+
{
178+
AdLoadedStatus?.SetActive(false);
179+
});
155180
};
156181
}
157182
}

samples/HelloWorld/Assets/Scripts/RewardedAdController.cs

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

56
namespace GoogleMobileAds.Sample
67
{
@@ -67,7 +68,11 @@ public void LoadAd()
6768
RegisterEventHandlers(ad);
6869

6970
// Inform the UI that the ad is ready.
70-
AdLoadedStatus?.SetActive(true);
71+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
72+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
73+
{
74+
AdLoadedStatus?.SetActive(true);
75+
});
7176
});
7277
}
7378

@@ -90,9 +95,6 @@ public void ShowAd()
9095
{
9196
Debug.LogError("Rewarded ad is not ready yet.");
9297
}
93-
94-
// Inform the UI that the ad is not ready.
95-
AdLoadedStatus?.SetActive(false);
9698
}
9799

98100
/// <summary>
@@ -128,9 +130,20 @@ private void RegisterEventHandlers(RewardedAd ad)
128130
// Raised when the ad is estimated to have earned money.
129131
ad.OnAdPaid += (AdValue adValue) =>
130132
{
133+
// This log is executed off the Unity main thread.
134+
// Write all time-sensitive code before ExecuteInUpdate().
131135
Debug.Log(String.Format("Rewarded ad paid {0} {1}.",
132136
adValue.Value,
133137
adValue.CurrencyCode));
138+
139+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
140+
{
141+
// This callback may be delayed on Android until the user
142+
// returns to the app. Place all code that interacts with
143+
// Unity UI and GameObjects inside this callback.
144+
Debug.Log("Rewarded ad paid callback " +
145+
"invoked inside ExecuteInUpdate.");
146+
});
134147
};
135148
// Raised when an impression is recorded for an ad.
136149
ad.OnAdImpressionRecorded += () =>
@@ -146,6 +159,12 @@ private void RegisterEventHandlers(RewardedAd ad)
146159
ad.OnAdFullScreenContentOpened += () =>
147160
{
148161
Debug.Log("Rewarded ad full screen content opened.");
162+
163+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
164+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
165+
{
166+
AdLoadedStatus?.SetActive(false);
167+
});
149168
};
150169
// Raised when the ad closed full screen content.
151170
ad.OnAdFullScreenContentClosed += () =>
@@ -157,6 +176,12 @@ private void RegisterEventHandlers(RewardedAd ad)
157176
{
158177
Debug.LogError("Rewarded ad failed to open full screen content with error : "
159178
+ error);
179+
180+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
181+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
182+
{
183+
AdLoadedStatus?.SetActive(false);
184+
});
160185
};
161186
}
162187
}

samples/HelloWorld/Assets/Scripts/RewardedInterstitialAdController.cs

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

56
namespace GoogleMobileAds.Sample
67
{
@@ -70,7 +71,11 @@ public void LoadAd()
7071
RegisterEventHandlers(ad);
7172

7273
// Inform the UI that the ad is ready.
73-
AdLoadedStatus?.SetActive(true);
74+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
75+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
76+
{
77+
AdLoadedStatus?.SetActive(true);
78+
});
7479
});
7580
}
7681

@@ -90,9 +95,6 @@ public void ShowAd()
9095
{
9196
Debug.LogError("Rewarded interstitial ad is not ready yet.");
9297
}
93-
94-
// Inform the UI that the ad is not ready.
95-
AdLoadedStatus?.SetActive(false);
9698
}
9799

98100
/// <summary>
@@ -128,9 +130,20 @@ protected void RegisterEventHandlers(RewardedInterstitialAd ad)
128130
// Raised when the ad is estimated to have earned money.
129131
ad.OnAdPaid += (AdValue adValue) =>
130132
{
133+
// This log is executed off the Unity main thread.
134+
// Write all time-sensitive code before ExecuteInUpdate().
131135
Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
132136
adValue.Value,
133137
adValue.CurrencyCode));
138+
139+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
140+
{
141+
// This callback may be delayed on Android until the user
142+
// returns to the app. Place all code that interacts with
143+
// Unity UI and GameObjects inside this callback.
144+
Debug.Log("Rewarded interstitial ad paid callback " +
145+
"invoked inside ExecuteInUpdate.");
146+
});
134147
};
135148
// Raised when an impression is recorded for an ad.
136149
ad.OnAdImpressionRecorded += () =>
@@ -146,6 +159,12 @@ protected void RegisterEventHandlers(RewardedInterstitialAd ad)
146159
ad.OnAdFullScreenContentOpened += () =>
147160
{
148161
Debug.Log("Rewarded interstitial ad full screen content opened.");
162+
163+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
164+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
165+
{
166+
AdLoadedStatus?.SetActive(false);
167+
});
149168
};
150169
// Raised when the ad closed full screen content.
151170
ad.OnAdFullScreenContentClosed += () =>
@@ -157,6 +176,12 @@ protected void RegisterEventHandlers(RewardedInterstitialAd ad)
157176
{
158177
Debug.LogError("Rewarded interstitial ad failed to open full screen content" +
159178
" with error : " + error);
179+
180+
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
181+
MobileAdsEventExecutor.ExecuteInUpdate(() =>
182+
{
183+
AdLoadedStatus?.SetActive(false);
184+
});
160185
};
161186
}
162187
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using UnityEngine;
2+
using GoogleMobileAds.Api;
3+
using GoogleMobileAds.Common;
4+
5+
namespace GoogleMobileAds.Snippets
6+
{
7+
/// <summary>
8+
/// Code snippets used for the developer guides covering global settings.
9+
/// </summary>
10+
internal class GlobalSettingsSnippets
11+
{
12+
internal GameObject _myGameObject;
13+
14+
private void HandleAdEventsOnMainThread()
15+
{
16+
// [START execute_in_update]
17+
// Google Mobile Ads events are raised off the Unity main thread.
18+
19+
// This log is executed off the Unity main thread.
20+
// Write all time-sensitive code before ExecuteInUpdate().
21+
Debug.Log("Executing off the Unity main thread.");
22+
23+
// Use ExecuteInUpdate to run code on the main thread, allowing you to
24+
// interact with Unity UI and GameObjects.
25+
// Changed to fully-qualified name to resolve CS0103
26+
GoogleMobileAds.Common.MobileAdsEventExecutor.ExecuteInUpdate(() =>
27+
{
28+
// This callback may be delayed on Android until the user returns to the app.
29+
Debug.Log("Executing on the Unity main thread.");
30+
31+
// Place all code that interacts with Unity UI and GameObjects inside this callback.
32+
if (_myGameObject != null)
33+
{
34+
_myGameObject.SetActive(true);
35+
}
36+
});
37+
// [END execute_in_update]
38+
}
39+
}
40+
}

samples/HelloWorld/Assets/Snippets/GlobalSettingsSnippets.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)