Easy Ad Integration for Unity Developers | Package Manager UI Website (2023)

overview

This guide covers integration to implement Unity Ads in your Made with Unity game.

  • If you are an iOS developer using Objective-C,Click here.
  • If you are an Android developer using Java,Click here.
  • Click herefor C#AnnouncementAPI reference.

observation: If you only plan to implement video, interstitial, and banner ads for your monetization strategy, Unity recommends usingAnnouncementAPI for an easier onboarding experience. However, if you plan to implementcustom placements, you need to integrate with Unity Adsmonetizationapi. For more information, seeIntegration for custom placementsSection.

Guide content

  • Configure your project
    • Set your building goals
    • Installing the SDK
  • Creating a placement
  • script implementation
    • Initialize the SDK
    • Interstitial Ads
    • Rewarded Video Ads
  • Integration for custom placements
  • Testing your implementation

Configure your project

Set construction goals

Configure your project using for a supported platformBuild settings window. Set the platform toiOSorAndroid, then clickchange platform.

Easy Ad Integration for Unity Developers | Package Manager UI Website (1)

Installing Unity Ads

To ensure you have the latest version of Unity Ads, download it from the Asset Store or from the Unity Package Manager in the Editor.

Important: You must select the asset or the package. Installing both can result in compilation errors.

(Video) Unity Ads 2022 (Rewarded and Interstitial) - Easy Unity Tutorial

Using the resource pack

Downloadthe latest version of Unity Ads from the Asset Store. For information on downloading and installing resource packs, seeAsset pack documentation.

Using the package manager

Install the latest version of Unity Ads fromUnity-Paket-Managerfollowing these steps:

  1. Select in Unity editorWindow>package managerto open the package manager.
  2. To chooseAnnouncementPackage from the list and select the latest verified version.
  3. tightenTo installorTo updateI like.

Creating a placement

placementsare events triggered in your game that indicate monetization content. Manage ratings throughTo workguide ofDeveloper Dashboardselecting your project and then selectingmonetization>placementsin the left navigation bar.

tightenADD POSITIONButton to open the placement creation prompt. Name your placement and select its type:

  • To chooseunrewardedto display simple interstitial ads or promotional content. Unrewarded placements allow players to skip the ad after a period of time.
  • To chooserewardedto allow players to opt out of viewing advertisements in exchange for incentives. Rewarded placements do not allow the player to skip the ad.
  • To chooseFlagto create a dedicated banner ad channel.

Each Unity Ads-enabled project has an (unrewarded) "Video' and (rewards) 'award-winning video’ Placement by default. Feel free to use one of these for your first implementation if it meets your needs, or create your own.

script implementation

Initialize the SDK

To initialize the SDK, you need to reference your project's Game ID for the appropriate platform. You can find the ID onTo workguide ofDeveloper Dashboardselecting the project and then selecting itIdeas>project settingsin the left navigation bar (see thepanel guidesectionproject settingsfor details).

(Video) New Advertisement Tutorial - How to Use Unity Ads 4.1 in your Unity Game

In the header of the game script, add theUnityEngine.Shownamespace. Initialize the SDK at the beginning of the game's run cycle, preferably at startup, with theDisplay.InitializeFunction. For example:

Usando UnityEngine; Usando UnityEngine.Advertisements; public class InitializeAdsScript : MonoBehavior { string gameId = "1234567"; bool testMode = true; void Start() { Advertisement.Initialize(gameId, testMode); }}

Interstitial Display Ads

To display an interstitial ad in full screen, use theAnnouncementAPI, just initialize the SDK and use theWerbung.ShowFunction. For example:

Using UnityEngine; Using UnityEngine.Advertisement; public class InterstitialAdsScript : MonoBehavior { string gameId = "1234567"; bool testMode = true; // Initialize the ad service: void Start() { Advertisement.Initialize(gameId, testMode); } // Show advertisement: Advertisement.Show();}

Rewarded Video Ads

Rewarding players for watching ads increases user engagement, which leads to increased revenue. For example, games may reward players with in-game currency, consumables, extra lives, or experience multipliers. For more information on how to effectively create your rewarded ads, see the documentation atAds best practices.

To reward players for completing a video ad, implement a callback method using theshow resultResult to check if the user finished the ad and should be rewarded. For example:

Using UnityEngine; Using UnityEngine.Advertisements; public class RewardedAdsScript: MonoBehaviour, IUnityAdsListener { string gameId = "1234567"; myPlacementId = "rewardedVideo"; bool testMode = true; // Initialize the ad listener and service: void Start() { Advertisement.AddListener(this); Advertisement.Initialize(gameId, testMode); } // Implement the IUnityAdsListener interface methods: public void OnUnityAdsDidFinish(string PlacementId, ShowResult showResult) { // Define conditional logic for each ad completion status: if (showResult == ShowResult.Finished) { // Reward the user for viewing the completion of the ad . } else if (showResult == ShowResult.Skipped) { // Don't reward the user for skipping the display. } else if (showResult == ShowResult.Failed) { Debug.LogWarning("Delivery did not stop due to an error.); } } public void OnUnityAdsReady (string placementId) { // If placement is rewarded, show the ad: if (placementId == myPlacementId) { Advertisement.Show (myPlacementId); } } public void OnUnityAdsDidError (string message) { // Log errors. } public void OnUnityAdsDidStart (string placementId) { // Optional actions to take when user end trigger an ad. } }

Bonus Video Ad Buttons

Using a button that allows the player to turn on viewing an ad is a common implementation for rewarded video ads. Use the sample code below to create a rewarded ads button. The ad button displays an ad when pressed, as long as there are ads available. To configure the button in the Unity editor:

  1. To choosegame object>user interface>I liketo add a button to your scene.
  2. Select the button you added to your scene and use the inspector (add component>new script). Name the scriptRewardedAdsButtonmatches the class name.
  3. Open the script and add the following code:
Using UnityEngine;Using UnityEngine.UI;Using UnityEngine.Advertisements;[RequireComponent (typeof (Button))]public class RewardedAdsButton : MonoBehaviour, IUnityAdsListener { #if UNITY_IOS private string gameId = "1486551"; #elif UNITY_ANDROID private string gameId = "1486550"; #endif Button myButton; public string myPlacementId = "rewardedVideo"; Void Start() { myButton = GetComponent<Button>(); // Set interactivity to depend on placement status: myButton.interactable = Advertisement.IsReady(myPlacementId); // Bind the ShowRewardedVideo function to the button's click listener: if (myButton) myButton.onClick.AddListener (ShowRewardedVideo); // Initialize the ad listener and service: Advertisement.AddListener(this); Advertisement.Initialize(gameId, true); } // Implement a function to display a video ad with a reward: void ShowRewardedVideo() { Advertisement.Show(myPlacementId); } // Implement the IUnityAdsListener interface methods: public void OnUnityAdsReady (string placementId) { // If placement is rewarded, activate the button: if (placementId == myPlacementId) { myButton.interactable = true; } } public void OnUnityAdsDidFinish (string PlacementId, ShowResult showResult) { // Define conditional logic for each ad completion status: if (showResult == ShowResult.Finished) { // Reward the user for watching the ad to completion. } else if (showResult == ShowResult.Skipped) { // Don't reward the user for skipping the display. } else if (showResult == ShowResult.Failed) { Debug.LogWarning("Display did not stop due to an error."); } } public void OnUnityAdsDidError (string message) { // Log errors. } public void OnUnityAdsDidStart(string placementId) { // Optional actions to take when the end user triggers an ad. } }

Integration for custom placements

Unity's monetization platform offers powerful revenue tools. If your game uses in-app purchases and ads, Unity's machine learning data model can seamlessly mix content types for a streamlined monetization strategy. To learn more about how Unity can help you optimize revenue, read the Custom Placements documentation.

(Video) How To Add Support For Multiple Languages In Unity

The Unity Ads integration for custom placements is a little different in that it requires themonetizationAPI instead ofAnnouncementAPI.

startup

In the header of the game script, add theUnityEngine.Monetizationnamespace. Initialize the SDK at the beginning of the game's run cycle, preferably at startup, with theMonetization. InitializeFunction. For example:

Usando UnityEngine.Monetization;public class UnityAdsScript : MonoBehaviour { string gameId = "1234567"; bool testMode = true; void Start() { Monetization.Initialize(gameId, testMode); }}

Implement simple (non-rewarded) ads

serving contentis an object that represents the monetization content your placement can display (see the documentation atcontent typesEcustom placements). Use oMonetization.GetPlacementContentAbility to fetch content when it's ready to be viewed and itsShowfunction to display it. For example:

Verwendung von UnityEngine.Monetization;öffentliche Klasse UnityAdsPlacement : MonoBehaviour { public string placementId = "video"; public void ShowAd () { StartCoroutine (ShowAdWhenReady ()); } private IEnumerator ShowAdWhenReady () { while (!Monetization.IsReady (placementId)) { yield return new WaitForSeconds(0.25f); } ShowAdPlacementContent-Anzeige = null; anĂșncio = Monetization.GetPlacementContent (placementId) como ShowAdPlacementContent; if(ad != null) { ad.Show (); } }}

In this example, the coroutine checks the availability of the passed serving codeserving contentUse ofMonetization. Is readyProperty. When content is available it is stored as a variable and run with itShowFunction.

Implement Rewarded Ads

Rewarding players for watching ads increases user engagement, which leads to increased revenue. For example, games may reward players with in-game currency, consumables, extra lives, or experience multipliers. For more information on how to effectively create your rewarded ads, see the documentation atAds best practices.

To reward players for viewing ads, follow the same steps outlined in the basic implementation section, but serve the ad using a reward callback method with custom logic for players who complete the ad.

(Video) The Unity Package Manager Complete Guide Unity 2020 and above - Episode#1

choosing a channel

You must show rewarded adsawarded placements. Every Unity Ads-enabled project also has a'Award winning video'Positioning by default. Feel free to use this for your implementation, orcreate your own(Make sure your placement is set to "Rewarded").

Adding a callback method to your script

OShowThe function accepts a callback, which the SDK uses to return ashow resultTell. This result indicates whether the player finished or skipped the ad. Use this information to write a custom function to handle each scenario. For example:

Verwendung von UnityEngine.Monetization;öffentliche Klasse RewardedAdsPlacement : MonoBehaviour { public string placementId = "rewardedVideo"; public void ShowAd () { StartCoroutine (WaitForAd ()); } IEnumerator WaitForAd () { while (!Monetization.IsReady (placementId)) { yield return null; } ShowAdPlacementContent-Anzeige = null; anĂșncio = Monetization.GetPlacementContent (placementId) como ShowAdPlacementContent; if (ad != null) { ad.Show (AdFinished); } } void AdFinished (ShowResult result) { if (result == ShowResult.Finished) { // Den Spieler belohnen } }}

Sample code for Rewarded Ads button.

Rewarded ads typically use a button that prompts players to opt-in to viewing the ad. Here's how to create a rewarded ads button that displays an ad when pressed, as long as the content is available.

  1. Select in Unity editorgame object>user interface>I liketo add a button to your scene.
  2. With the button selected in the inspector, clickadd component>new scriptto add a script component to it. Name the scriptUnityAdsButtonmatches the class name.
  3. Open the script and add the following sample code:
Verwendung von UnityEngine;Verwendung von UnityEngine.UI;Verwendung von UnityEngine.Monetization;[RequireComponent (typeof (Button))]public class UnityAdsButton : MonoBehaviour { public string placementId = "rewardedVideo"; private string adButton;#if UNITY_IOS private string gameId = "1234567";#elif UNITY_ANDROID private string gameId = "7654321";#endif void Start () { adButton = GetComponent<Button> (); if (adButton) {adButton.onClick.AddListener (ShowAd); } if (Monetization.isSupported) {Monetization.Initialize (gameId, true); } } void Update () { if (adButton) { adButton.interactable = Monetization.IsReady (placementId); } } void ShowAd () { opçÔes ShowAdCallbacks = new ShowAdCallbacks (); opçÔes.finishCallback = HandleShowResult; ShowAdPlacementContent-Anzeige = Monetization.GetPlacementContent (placementId) tambĂ©m ShowAdPlacementContent; ad.Show (opçÔes); } void HandleShowResult (ShowResult result) { if (result == ShowResult.Finished) { // Den Spieler belohnen } else if (result == ShowResult.Skipped) { Debug.LogWarning ("Der Spieler hat das Video ĂŒbersprungen - NICHT BELOHNUNG! "); } else if (result == ShowResult.Failed) { Debug.LogError ("Video konnte nicht angezeigt werden"); } }}

observation: This example checks at a certain point in the game if the serving content is ready to be displayed. As an alternative method, you can implement a listener that notifies you when content is available.

Test

Before publishing your game, enable test mode by following the steps below:

  1. DoTo workguide ofDeveloper Dashboard, select your project.
  2. To choosemonetization>platformsin the left navigation bar.
  3. Select the desired platform and then select theIDEASAba.
  4. Scroll down toBODY MODEsection and toggle the Override Client test mode, then select theForce test mode ONknopf radio.

In the Unity editor, click the play button to run your project and test your ad implementation.

(Video) Mobile Ads - Unity 2021.3 Full ads integration and Google Play Console setup tutorial

observation: You must enable test mode before testing the ad integration to avoid being flagged as a scam.

What is the next?

Take your implementation to the next level by taking advantage of Unity's additional monetization features to optimize your revenue. Here are some of the next steps to explore:

  • Integrate other types of ad content.
    • See documentation forIntegration of advertising banners.
    • See documentation forAR Ad Integration.
  • Integrate in-app purchases (IAP) and promote them.
    • If you haven't implemented IAP, create an in-app store withUnity-IAP.
    • If you've already done this, implementIAP-Promoto promote your in-app offers.
  • Let machine learning drive your monetization strategy.
    • If you have ads and in-app ads set up, usecustom placementsto increase sales of your entire game.
  • check out ourrecommended course of actionGuide to insights on how to design effective ad engines.

Videos

1. Unity/C# Script Communication & UI Integration Tips
(Denis)
2. Appodeal Ads Integration (Unity) - Easy Way To Monetize Your Game
(Reso Coder)
3. Unity Tutorial - Easy Popup integration - Free Package
(Hamza Herbou)
4. Cracking Software with Reverse Engineering 😳
(nang)
5. Publishing a Unity Game to Google Play Store With AdMob Ads Integration in 2023 Complete Tutorial
(Waqas Khalid)
6. Unity Firebase Database Integration - Easy Tutorial
(Solo Game Dev)
Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated: 11/06/2023

Views: 5942

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.