Skip to content

Commit e2fb63b

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added Snippets to the Interstitial Ad developer guide.
PiperOrigin-RevId: 788522465
1 parent e3d2f73 commit e2fb63b

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using GoogleMobileAds.Api;
5+
6+
namespace GoogleMobileAds.Snippets
7+
{
8+
internal class InterstitialAdSnippets
9+
{
10+
#if UNITY_ANDROID
11+
private const string AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
12+
#elif UNITY_IPHONE
13+
private const string AD_UNIT_ID = "ca-app-pub-3940256099942544/4411468910";
14+
#else
15+
private const string AD_UNIT_ID = "unused";
16+
#endif
17+
18+
void LoadAd()
19+
{
20+
// [START load_ad]
21+
// Create our request used to load the ad.
22+
var adRequest = new AdRequest();
23+
24+
// Send the request to load the ad.
25+
InterstitialAd.Load(AD_UNIT_ID, adRequest, (InterstitialAd ad, LoadAdError error) =>
26+
{
27+
if (error != null)
28+
{
29+
// The ad failed to load.
30+
return;
31+
}
32+
// The ad loaded successfully.
33+
});
34+
// [END load_ad]
35+
}
36+
37+
void ShowAd(InterstitialAd interstitialAd)
38+
{
39+
// [START show_ad]
40+
if (interstitialAd != null && interstitialAd.CanShowAd())
41+
{
42+
interstitialAd.Show();
43+
}
44+
// [END show_ad]]
45+
}
46+
47+
void ListenToAdEvents(InterstitialAd interstitialAd)
48+
{
49+
// [START ad_events]
50+
interstitialAd.OnAdPaid += (AdValue adValue) =>
51+
{
52+
// Raised when the ad is estimated to have earned money.
53+
};
54+
interstitialAd.OnAdImpressionRecorded += () =>
55+
{
56+
// Raised when an impression is recorded for an ad.
57+
};
58+
interstitialAd.OnAdClicked += () =>
59+
{
60+
// Raised when a click is recorded for an ad.
61+
};
62+
interstitialAd.OnAdFullScreenContentOpened += () =>
63+
{
64+
// Raised when the ad opened full screen content.
65+
};
66+
interstitialAd.OnAdFullScreenContentClosed += () =>
67+
{
68+
// Raised when the ad closed full screen content.
69+
};
70+
interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
71+
{
72+
// Raised when the ad failed to open full screen content.
73+
};
74+
// [END ad_events]]
75+
}
76+
77+
void DestroyAd(InterstitialAd interstitialAd)
78+
{
79+
// [START destroy_ad]
80+
if (interstitialAd != null)
81+
{
82+
interstitialAd.Destroy();
83+
}
84+
// [END destroy_ad]]
85+
}
86+
87+
void ReloadAd(InterstitialAd interstitialAd)
88+
{
89+
// [START reload_ad]
90+
interstitialAd.OnAdFullScreenContentClosed += () =>
91+
{
92+
// Reload the ad so that we can show another as soon as possible.
93+
var adRequest = new AdRequest();
94+
InterstitialAd.Load(AD_UNIT_ID, adRequest, (InterstitialAd ad, LoadAdError error) =>
95+
{
96+
// Handle ad loading here.
97+
});
98+
};
99+
// [END reload_ad]]
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)