Google Admob Unity
I'm developing an android game apk in Unity. I've integrated Google Admob SDK in my project and succeeded to show the Google admob reward video ads in my android apk by referring https://developers. Get personalized optimization tips, understand your account health and set up completion on the improved 'My AdMob page'. I am using Google Mobile Ads Unity Plugin v3.16 and tried in v3.0.0 and many more. Added app id in android manifest. Unity Ads are on in unity. My code of ads manager is below.
Banner ads are rectangular image or text ads that occupy a spot on screen.They stay on screen while users are interacting with the app, and can refreshautomatically after a certain period of time. If you're new to mobileadvertising, they're a great place to start.Case study.
This guide shows you how to integrate banner ads from AdMob into a Unity app.In addition to code snippets and instructions, it also includes informationabout sizing banners properly and links to additional resources. Human for mac.
Prerequisites
Complete Get Started. Your Unityapp should already have the Google Mobile Ads Unity plugin imported.
Create a BannerView
The first step toward displaying a banner is to create a BannerView
object ina C# script attached to a GameObject
.
For easier ads integration using the Unity Editor, try the new Ad Placements Beta.
The constructor for a BannerView
has the following parameters:
adUnitId
- The AdMob ad unit ID from which theBannerView
should load ads.AdSize
- The AdMob ad size you'd like to use (consult Bannersizes for details).AdPosition
- The position where the banner ad should be placed. TheAdPosition
enum lists the valid ad position values.
It is important to note how different ad units are used, depending on theplatform. You'll need to use an iOS ad unit for making ad requests on iOS and anAndroid ad unit for making requests on Android.
(Optional) Custom ad position
For greater control over where a BannerView
is placed on screen than what'soffered by AdPosition
values, use the BannerView
constructor that has x- andy-coordinates as parameters:
The top-left corner of the BannerView
will be positioned at the x and y valuespassed to the constructor, where the origin is the top-left of the screen.
(Optional) Custom ad sizes
In addition to using an AdSize constant, you can also specify a custom size foryour ad:
Always test with test ads
The sample code above contains an ad unit ID and you're free to request ads withit. It's been specially configured to return test ads rather than production adsfor every request, which makes it safe to use.
However, once you register an app in the AdMob UI and create your own ad unitIDs for use in your app, you'll need to explicitly configure your device as atest device when you're developing.This is extremely important. Testing with real ads (even if you never tap onthem) is against AdMob policy and can cause your account to be suspended. ReadTest Ads for information on how you can make sure youalways get test ads when developing.
Load an ad
Once the BannerView
is instantiated, the next step is to load an ad. That'sdone with the loadAd()
method in the BannerView
class. It takes anAdRequest
argument, which holds runtime information (such as targeting info)about a single ad request.
Here's an example that shows how to load an ad:
That's it! Your app is now ready to display banner ads from AdMob.
Ad events
To further customize the behavior of your ad, you can hook into a number ofevents in the ad's lifecycle: loading, opening, closing, and so on. Listen forthese events by registering a delegate for the appropriate EventHandler
, asshown below.
The OnAdFailedToLoad
event contains special event arguments. It passes aninstance of HandleAdFailedToLoadEventArgs
with a Message
describing theerror:
Ad event | Description |
---|---|
OnAdLoaded | The OnAdLoaded event is executed when an ad has finished loading. |
OnAdFailedToLoad | The OnAdFailedToLoad event is invoked when an ad fails to load. The Message parameter describes the type of failure that occurred. |
OnAdOpening | This method is invoked when the user taps on an ad. If you're using an analytics package to track clickthroughs, this is a good place to recordone. |
OnAdClosed | When a user returns to the app after viewing an ad's destination URL, this method is invoked. Your app can use it to resume suspended activities or perform any other work necessary to make itself ready for interaction. |
OnAdLeavingApplication | This method is invoked after onAdOpened , when a user click opens another app (such as the Google Play store), backgrounding the current app. |
Banner sizes
The table below lists the standard banner sizes.
Size in dp (WxH) | Description | Availability | AdSize constant |
---|---|---|---|
320x50 | Standard Banner | Phones and Tablets | BANNER |
320x100 | Large Banner | Phones and Tablets | LARGE_BANNER |
300x250 | IAB Medium Rectangle | Phones and Tablets | MEDIUM_RECTANGLE |
468x60 | IAB Full-Size Banner | Tablets | FULL_BANNER |
728x90 | IAB Leaderboard | Tablets | LEADERBOARD |
Provided width x Adaptive height | Adaptive banner | Phones and Tablets | N/A |
Screen width x 32|50|90 | Smart banner | Phones and Tablets | SMART_BANNER |
Learn more about Adaptive Banners, intended to replace Smart Banners. |
Clean up banner ads
Google Admob Unity Tutorial
When you are finished with a BannerView
, make sure to call the Destroy()
method before dropping your reference to it:
This notifies the plugin that the object is no longer used and the memory itoccupied can be reclaimed. Failure to call this method results in memory leaks.
Additional resources
Samples
Success stories
Note: This guide uses the legacy rewarded API. We recommend using thenew rewarded API.Rewarded video ads are fullscreen video ads that usershave the option of watching in fullin exchange for in-app rewards.
This guide shows you how to integrate rewarded video ads from AdMob into a Unityapp.
Prerequisites
Complete Get Started. Your Unityapplication should already have the Google Mobile Ads Unity plugin imported.
Get reference to singleton instance
The first step toward displaying a rewarded video ad is to get a reference tothe singleton RewardBasedVideoAd
instance. This reference can be retrieved bycalling RewardBasedVideoAd.Instance
.
Load an ad
Because RewardedBasedVideoAd
is a singleton, requests to load an ad should bemade using a shared instance.
It is highly recommended to call LoadAd()
as early as possible (for example,in the Start()
method of a script attached to a GameObject
) to allow videosto be preloaded, as shown below.
Google Admob Plugin For Unity
Always test with test ads
The sample code above contains an ad unit ID and you're free to request ads withit. It's been specially configured to return test ads rather than production adsfor every request, which makes it safe to use.
However, once you register an app in the AdMob UI and create your own ad unitIDs for use in your app, you'll need to explicitlyconfigure your device as a test devicewhen you're developing. This is extremely important. Testing with real ads(even if you never tap on them) is against AdMob policy and can cause youraccount to be suspended. SeeTest Ads for informationon how you can make sure you always get test ads when developing.
Note:Admob Unity Plugin
When you use the test ad unit, you will see the reward amount that wehave set there, not any value you set in your own ad unit.Ad events
File exe for mac. To further customize the behavior of your ad, you can hook into a number ofevents in the ad's lifecycle: loading, opening, closing, and so on. Listen forthese events by registering a delegate for the appropriate EventHandler
. Themost important event is OnAdRewarded
, which is called when the user should berewarded for watching a video. You can also implement other ad events, as shownbelow.
The OnAdRewarded
event contains special event arguments. It passes an instanceof Reward
with a Type
and Amount
describing the reward given to the user:
The OnAdFailedToLoad
event also contains special event arguments. It passes aninstance of HandleAdFailedToLoadEventArgs
with a Message
describing theerror:
Ad event | Description |
---|---|
OnAdLoaded | The OnAdLoaded event is invoked when an ad has finished loading. |
OnAdFailedToLoad | The OnAdFailedToLoad event is invoked when an ad fails toload. The Message parameter describes the type of failure thatoccurred. |
OnAdOpening | This method is invoked when the ad is displayed, covering the device'sscreen. If you're using an analytics package to track clickthroughs, this is agood place to record one. |
OnAdStarted | This method is invoked when the ad starts to play |
OnAdRewarded | This method is invoked when a user should be rewarded for watching avideo. The Reward parameter describes the reward to be presented tothe user. |
OnAdClosed | This method is invoked when the rewarded video ad is closed due tothe user tapping on the close icon or using the back button. If your app pausedits audio output or game loop, this is a great place to resume it. |
OnAdLeavingApplication | This method is invoked after OnAdOpened , when a user clickopens another app (such as the Google Play store), backgrounding the currentapp. |
Show the ad
Before displaying a rewarded ad to users, they must be presented with anexplicit choice to view rewarded ad content in exchange for a reward.Rewarded ads must always be an opt-in experience.
To show a rewarded video ad, use the IsLoaded()
method to verify that it'sdone loading, then call Show()
.
Google Mobile Ads Unity
The following code snippet demonstrates how to display a rewarded video ad.
Reload an ad
The OnAdClosed
event is a handy place to load a new rewardedvideo ad after displaying the previous one:
Additional resources
Samples
- HelloWorld example— a minimal implementation of all ad formats
Next steps
Admob Vs Unity Ads
- Create your own rewarded video ad unit in the AdMob UI.
- Try another ad format: