Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions samples/HelloWorld/Assets/Scripts/AppOpenAdController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public void LoadAd()
RegisterEventHandlers(ad);

// Inform the UI that the ad is ready.
AdLoadedStatus?.SetActive(true);
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(true);
});
});
}

Expand Down Expand Up @@ -158,9 +162,20 @@ private void RegisterEventHandlers(AppOpenAd ad)
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log(String.Format("App open ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));

MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// This callback may be delayed on Android until the user
// returns to the app. Place all code that interacts with
// Unity UI and GameObjects inside this callback.
Debug.Log("App open ad paid callback " +
"invoked inside ExecuteInUpdate.");
});
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
Expand All @@ -177,8 +192,11 @@ private void RegisterEventHandlers(AppOpenAd ad)
{
Debug.Log("App open ad full screen content opened.");

// Inform the UI that the ad is consumed and not ready.
AdLoadedStatus?.SetActive(false);
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(false);
});
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
Expand All @@ -192,7 +210,13 @@ private void RegisterEventHandlers(AppOpenAd ad)
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("App open ad failed to open full screen content with error : "
+ error);
+ error);

// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(false);
});
};
}
}
Expand Down
19 changes: 17 additions & 2 deletions samples/HelloWorld/Assets/Scripts/BannerViewController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

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

// Inform the UI that the ad is ready.
AdLoadedStatus?.SetActive(true);
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// Inform the UI that the ad is ready.
AdLoadedStatus?.SetActive(true);
});
};
// Raised when an ad fails to load into the banner view.
_bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
Expand All @@ -146,9 +150,20 @@ private void ListenToAdEvents()
// Raised when the ad is estimated to have earned money.
_bannerView.OnAdPaid += (AdValue adValue) =>
{
// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log(String.Format("Banner view paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));

MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// This callback may be delayed on Android until the user
// returns to the app. Place all code that interacts with
// Unity UI and GameObjects inside this callback.
Debug.Log("Banner view paid callback " +
"invoked inside ExecuteInUpdate.");
});
};
// Raised when an impression is recorded for an ad.
_bannerView.OnAdImpressionRecorded += () =>
Expand Down
33 changes: 29 additions & 4 deletions samples/HelloWorld/Assets/Scripts/InterstitialAdController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

namespace GoogleMobileAds.Sample
{
Expand Down Expand Up @@ -67,7 +68,11 @@ public void LoadAd()
RegisterEventHandlers(ad);

// Inform the UI that the ad is ready.
AdLoadedStatus?.SetActive(true);
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(true);
});
});
}

Expand All @@ -85,9 +90,6 @@ public void ShowAd()
{
Debug.LogError("Interstitial ad is not ready yet.");
}

// Inform the UI that the ad is not ready.
AdLoadedStatus?.SetActive(false);
}

/// <summary>
Expand Down Expand Up @@ -123,9 +125,20 @@ private void RegisterEventHandlers(InterstitialAd ad)
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log(String.Format("Interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));

MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// This callback may be delayed on Android until the user
// returns to the app. Place all code that interacts with
// Unity UI and GameObjects inside this callback.
Debug.Log("Interstitial ad paid callback " +
"invoked inside ExecuteInUpdate.");
});
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
Expand All @@ -141,6 +154,12 @@ private void RegisterEventHandlers(InterstitialAd ad)
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Interstitial ad full screen content opened.");

// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(false);
});
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
Expand All @@ -152,6 +171,12 @@ private void RegisterEventHandlers(InterstitialAd ad)
{
Debug.LogError("Interstitial ad failed to open full screen content with error : "
+ error);

// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(false);
});
};
}
}
Expand Down
33 changes: 29 additions & 4 deletions samples/HelloWorld/Assets/Scripts/RewardedAdController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

namespace GoogleMobileAds.Sample
{
Expand Down Expand Up @@ -67,7 +68,11 @@ public void LoadAd()
RegisterEventHandlers(ad);

// Inform the UI that the ad is ready.
AdLoadedStatus?.SetActive(true);
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(true);
});
});
}

Expand All @@ -90,9 +95,6 @@ public void ShowAd()
{
Debug.LogError("Rewarded ad is not ready yet.");
}

// Inform the UI that the ad is not ready.
AdLoadedStatus?.SetActive(false);
}

/// <summary>
Expand Down Expand Up @@ -128,9 +130,20 @@ private void RegisterEventHandlers(RewardedAd ad)
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log(String.Format("Rewarded ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));

MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// This callback may be delayed on Android until the user
// returns to the app. Place all code that interacts with
// Unity UI and GameObjects inside this callback.
Debug.Log("Rewarded ad paid callback " +
"invoked inside ExecuteInUpdate.");
});
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
Expand All @@ -146,6 +159,12 @@ private void RegisterEventHandlers(RewardedAd ad)
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Rewarded ad full screen content opened.");

// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(false);
});
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
Expand All @@ -157,6 +176,12 @@ private void RegisterEventHandlers(RewardedAd ad)
{
Debug.LogError("Rewarded ad failed to open full screen content with error : "
+ error);

// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(false);
});
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

namespace GoogleMobileAds.Sample
{
Expand Down Expand Up @@ -70,7 +71,11 @@ public void LoadAd()
RegisterEventHandlers(ad);

// Inform the UI that the ad is ready.
AdLoadedStatus?.SetActive(true);
// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(true);
});
});
}

Expand All @@ -90,9 +95,6 @@ public void ShowAd()
{
Debug.LogError("Rewarded interstitial ad is not ready yet.");
}

// Inform the UI that the ad is not ready.
AdLoadedStatus?.SetActive(false);
}

/// <summary>
Expand Down Expand Up @@ -128,9 +130,20 @@ protected void RegisterEventHandlers(RewardedInterstitialAd ad)
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));

MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// This callback may be delayed on Android until the user
// returns to the app. Place all code that interacts with
// Unity UI and GameObjects inside this callback.
Debug.Log("Rewarded interstitial ad paid callback " +
"invoked inside ExecuteInUpdate.");
});
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
Expand All @@ -146,6 +159,12 @@ protected void RegisterEventHandlers(RewardedInterstitialAd ad)
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Rewarded interstitial ad full screen content opened.");

// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(false);
});
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
Expand All @@ -157,6 +176,12 @@ protected void RegisterEventHandlers(RewardedInterstitialAd ad)
{
Debug.LogError("Rewarded interstitial ad failed to open full screen content" +
" with error : " + error);

// Use MobileAdsEventExecutor to ensure the UI is updated on the main thread.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
AdLoadedStatus?.SetActive(false);
});
};
}
}
Expand Down
40 changes: 40 additions & 0 deletions samples/HelloWorld/Assets/Snippets/GlobalSettingsSnippets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

namespace GoogleMobileAds.Snippets
{
/// <summary>
/// Code snippets used for the developer guides covering global settings.
/// </summary>
internal class GlobalSettingsSnippets
{
internal GameObject _myGameObject;

private void HandleAdEventsOnMainThread()
{
// [START execute_in_update]
// Google Mobile Ads events are raised off the Unity main thread.

// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log("Executing off the Unity main thread.");

// Use ExecuteInUpdate to run code on the main thread, allowing you to
// interact with Unity UI and GameObjects.
// Changed to fully-qualified name to resolve CS0103
GoogleMobileAds.Common.MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// This callback may be delayed on Android until the user returns to the app.
Debug.Log("Executing on the Unity main thread.");

// Place all code that interacts with Unity UI and GameObjects inside this callback.
if (_myGameObject != null)
{
_myGameObject.SetActive(true);
}
});
// [END execute_in_update]
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.