Video ads
The ad
module contains functionality for displaying video ads and for detecting adblockers.
Requirements for Advertisements
Please be sure to read our advertisement requirements, since your game will be rejected without any feedback if it doesn't follow them.
Getting started
Access the ad
module like this:
Info
For a demo, please consult the CrazySDK/Demo/AdModule
scene. You can run it directly in the Unity editor.
Make sure to read the introduction page on setting up your project.
Video ads
We support two different types of video ads: midgame
and rewarded
. Read more on our advertisement requirements.
- Midgame advertisements can happen when a user died, a level has been completed, etc.
- Rewarded advertisements can be requested by the user in exchange for a reward (An additional life, a retry when the user died, a bonus starting item, extra starting health, etc.).
To request a video ad:
const callbacks = {
adFinished: () => console.log("End midgame ad"),
adError: (error) => console.log("Error midgame ad", error),
adStarted: () => console.log("Start midgame ad"),
};
window.CrazyGames.SDK.ad.requestAd("midgame", callbacks);
// or
window.CrazyGames.SDK.ad.requestAd("rewarded", callbacks);
Warning
Make sure to mute the audio and pause the game when the ad starts (adStarted
callback), and to unmute the audio and continue the game when the ad finishes/fails to load (adError
and adFinished
callbacks)
CrazySDK.Ad.RequestAd(CrazyAdType.Midgame, () => // or CrazyAdType.Rewarded
{
// ad started
}, (error) =>
{
// ad error
}, () =>
{
// ad finished, for rewarded ads give reward here
});
Info
Your game will automatically be paused when the ad is being requested, and be continued when the ad finishes playing.
Passing callbacks is optional, so this is also a valid call:
Warning
Ad breaks are displayed as an overlay on top of your game. Your game continues running in the background. To ensure that the game does not interfere with the advertisement, your sound must be muted when the ad starts and turned back on when it has stopped. You can do this by customizing the gmcallback_crazy_callback()
function.
The three kinds of events are:
"crazy.break.started"
is emitted when an advertisement starts playing."crazy.break.finished"
is emitted when an advertisement stops playing."crazy.break.error"
is emitted when an error has occurred, or the ad wasn't filled. This event is also emitted when no advertisement is available.
await window.ConstructCrazySDK.ad.requestAd("midgame");
await window.ConstructCrazySDK.ad.requestAd("rewarded");
Warning
You’ll have to make sure to pause your game before playing the ad by setting the time scale to 0 (System action). Be sure there isn't any audio playing also while the ad is displayed. Then use the Wait for previous action
system event and resume the game when the ad finishes by setting the time scale back to 1. Your event sheet should look like this:
Before you can request ads, you need to be able to be able to react to an ad starting or finishing. The SDK offers three callbacks: adStarted
, adError
, and adFinished
. Read more below.
For each callback, create a Godot function with one argument. Within these functions, you can, for example, emit custom signals that other scripts can connect and react to. For example, you should pause the game and mute the audio during an ad.
# Example
signal ad_started
signal ad_finished
signal ad_error
func adStarted(args):
emit_signal("ad_started")
func adError(error):
emit_signal("ad_error", error)
func adFinished(args):
emit_signal("ad_done")
The JavaScript callbacks have to be declared in global scope like this:
var adStartedCallback = JavaScript.create_callback(self, "adStarted")
var adErrorCallback = JavaScript.create_callback(self, "adError")
var adFinishedCallback = JavaScript.create_callback(self, "adFinished")
Define a JavaScript object in global scope to encapsulate all three callbacks.
In the _ready
function, add the three callbacks like this:
adCallbacks["adFinished"] = adFinishedCallback
adCallbacks["adError"] = adErrorCallback
adCallbacks["adStarted"] = adStartedCallback
Now, to request an ad, call:
Callbacks
The adError
callback is also triggered if the ad is not filled or if something else goes wrong. Your game should be able to handle this.
CrazyGames provides fallback banners and house-ads to limit unfilled ads.
The returned errorData
object will look like this:
{
"code": "unfilled", // possible "code" values: ["unfilled", "other"]
"message": "No ad available"
}
Adblock detection
Info
We require games to function even when the user has an adblock. The detection is not foolproof, and it would be very frustrating for a user not running any adblock to get a non-functional game. You can block extra content, such as custom skins, or some levels, to motivate the user to turn off their adblock. Also, keep in mind that turning off the adblock usually requires a page refresh. Make sure that the progress is saved, or the user may just decide to stop playing your game.
You can use the code below to detect if the user has an adblocker.
There are 2 ways of checking if the user has an active adblocker:
Via property
Initially, the status will be AdblockStatus.Detecting
. It will switch soon to AdblockStatus.Present
(if there is an active adblocker) or AdblockStatus.Missing
.
Via callback
You can also subscribe a callback to be called when the adblock is detected. You can also call HasAdblock
even after the adblock is detected, and the provided callback will be called instantly.
There may be issues however if the scene changes in the meantime and the adblock detection is executed later, as now callbacks subscribed by the previous scenes may fail.
It is safer to use this method on singleton objects, or objects marked with DontDestroyOnLoad.
We detect whether the user has installed an adblock tool. We pass this information to the developer through the SDK.
The adblock detection is executed when crazy_init()
is called. You will have to implement an event listener on crazy.adblock.detected
which is fired when the adblock detection finished running. See the example at the end of this guide.
We detect whether the user has installed an adblock tool. We pass this information to the developer through the hasAdBlock()
function. That function returns a boolean that you can assign to a global variable (make sure that your variable type is also boolean):
To detect if the user has an adblocker, the ad
module offers the method hasAdblock
:
Define a function to receive the result. From there, you can emit a signal for other scripts to connect to, for example.
signal adblock_result
func adblockResult(args):
var error = args[0]
var result = args[1]
emit_signal("adblock_result", error, result)
Create a callback to it in global scope:
Through the callback, the SDK will return an array, the first element tells you whether an error occurred during adblock detection. If none occured, the second element tells you whether the user has an adblock active.