Integrating Unity Ads
In this post we are going to look at Unity’s new ad service as well as how to integrate it into your current projects. Our goal is to make it easy to make money using your current (or hopefully future) player base with very little overhead and maintenance. We will see exactly what you need to do to integrate advertisements in your project (trust me, it’s not a lot), as well as some of the “gotchas” that might slow you down along the way.
Creating Your Account
Setting up ads in a project is really only a couple of steps. The first thing you will need to do is go to the Unity Ads website and create an account (it’s free and only takes a few minutes). Once you are logged in, you will be presented with the Ads Dashboard. To begin monetizing your project, click “Start Monetizing” (you could also click the “Games” menu item on the left and then “Add New Game”).
You will then be prompted for some basic information regarding your project. Go ahead and specify your target platform and your project name. If your project is already deployed to market you can look it up. If your project is still in development, don’t worry. There is a link at the bottom you can click to add the development version to your account.
An interesting takeaway here is that if your game is for both iOS and Android, you will create two different game profiles for the same game in your account. This means that you will have different ad IDs for both versions and thus will be able to track and manage your ads per platform instead of all together.
Now that your game is added to your Unity Ads account, you will be presented with your game dashboard. There are a lot of controls, options, and information panels here, but we are only going to concern ourselves with the “Game ID”. This ID is how we can control the ads shown as well as get the credit for any views or clicks our users provide.
(This image is from a sample game I have created for this article. I highly recommend you create your own account and game and not use my sample Game ID or else I will get all of the monetization credit for your games. That being said, if you feel like using my Game ID, I won’t stop you. Who doesn’t like free money?)
Integrating Ads into Your Project
Now that you have your Unity Ads account set up, you can begin showing ads in your project. From within Unity, navigate to the Asset Store (Window->Asset Store) and download and import the Unity Ads package. NOTE: currently there will be a warning if you are trying to download the package with Unity 5. This warning is due to the package being uploaded from Unity 4 and is harmless. It just exists currently for backwards compatibility.
Once the assets are imported into your project, initializing and showing ads is very simple.
NOTE: Since ads are currently only supported for iOS and Android, your editor will have to be targeting one of those platforms in order to see any test ads. Before going further you may want to switch your platform to either iOS or Android in the Unity build settings (File->Build Settings).
In a project, ads are controlled through scripts and there are three lines of code that are absolutely required (and one line of code that should be considered required). The first line is where we give our script access to the Advertisements namespace:
Next we must initialize our ads. This can be done anywhere you like, but it must be done before you attempt to show any ads. The first argument of the Initialize() function is the Game ID we got from the Unity Ads website (remember, use your own and not mine). The second argument is whether or not your game is in “test mode”. Generally speaking, while your game is in development you should always leave this as true. We will discuss this more later.
[csharp]Advertisement.Initialize ("29239", true);[/csharp]
Finally we show an advertisement. If we are running our project in the editor we will see a generic billboard ad. If we are running this on device and test mode is set to true (see above) then we will see a placeholder ad. Finally, if we are on device and test mode is false we will see a real live ad.
Now, initializing the ad system isn’t instantaneous, so you will want to ensure that an ad is ready before attempting to show it. Doing so can prevent errors or bugs that may otherwise plague your experience. You can check that an ad is ready to be shown by using the function isReady()
So putting everything together, a simple script to just show an ad when your game starts up could look like this:
public class SimpleAdScript : MonoBehaviour
void Start ()
Advertisement.Initialize ("29239", true);
StartCoroutine (ShowAdWhenReady ());
while (!Advertisement.isReady ())
yield return null;
In the next post we will discuss more advanced ways of using ads as well as how we can build an ad structure using the concepts demonstrated here.
27 CommentsSubscribe to comments
Comments are closed.