Ads Wrapper allows:
- Use the same interface for working with different advertising services.
- Advertising mediation - allows you to show ads from different sources.
Supported services:
- Admob
- Unity Ads
- Poki
- Yandex
- Yandex Mobile Ads
- Vk Bridge
- Applovin Max
- GameDistribution
- IronSource
Ads Wrapper also allows you to run multiple networks at the same time. For example Admob and Unity Ads.
You can find more information on the networks
You can use it in your own project by adding this project as a Defold library dependency. Open your game.project
file and in the dependencies field add a link to the ZIP file of a specific release.
First you need to make a require for ads_wrapper.ads_wrapper
and ads_wrapper.events
.
local ads_wrapper = require("ads_wrapper.ads_wrapper")
local events = require("ads_wrapper.events")
Next, you need to register the networks. More details about networks can be found here.
-- Getting our module with networks
local test = require("ads_wrapper.ads_networks.test")
-- Register the first network
local test_1_id = ads_wrapper.register_network(test.network1, {param = "test_param 1"})
-- Register the second network
local test_2_id = ads_wrapper.register_network(test.network2, {param = "test_param 2"})
Next, you need to configure the mediators you need. There are two types: Video and Banner. Video mediator refers to the functions associated with interstitials and rewarded videos. Banner mediator refers to the functions associated with banners. You need to set up only the mediator that you need. For example, Poki does not support banners.
-- Setup video mediator
ads_wrapper.setup_video({{id = test_2_id, count = 2}, {id = test_1_id, count = 2}}, 4)
-- Setup banner mediator
ads_wrapper.setup_banner({{id = test_1_id, count = 1}})
You can read more about mediators here.
Next, you need to initialize ads wrapper.
ads_wrapper.init(true, true, function(response)
if response.result == events.SUCCESS then
pprint("Ads wrapper is initialized", response)
else
pprint("Something bad happened", response)
end
end)
In the first two parameters, you can specify the need to initialize the network.
First - initilize_video
, Second - initilize_banner
. This may take time depending on the service.
Further, they can be initialized separately, if not done immediately.
When an advertisement is called, they will be initialized automatically
if it has not been done before.
Almost all functions have a callback
parameter.
callback
- function which takes one response
parameter.
The response
can be of two types: success
and error
.
Response is a table:
- result hash required May be
events.SUCCESS
orevents.ERROR
- name string optional Network name
- message string optional Additional information. For example, consists info about error or it may report that the network has already been initialized when
init_video_networks
is called again. - code hash optional Response code. Information that convient to track by code.
- responses table optional Contains all responses if the function is called for multiple networks. This may be during initialization.
- was_open table optional Some networks report whether ads was open when calling
show_interstitial
orshow_rewarded
.
Also, the response may contain additional information from a specific network.
Module ads_wrapper.events
contains useful variables:
events.SUCCESS
hash("SUCCESS")events.ERROR
hash("ERROR")
The response table may have the code. All of them are contained in the ads_wrapper.events
module, like events.C_SKIPPED
. There are types of codes:
events.C_SKIPPED
hash("C_SKIPPED") - if the rewarded advertisement was skippedevents.C_ERROR_UNKNOWN
hash("C_ERROR_UNKNOWN") - an unknown error type is occuredevents.C_ERROR_AD_BLOCK
hash("C_ERROR_AD_BLOCK") - an error is related to the adblockevents.C_ERROR_NO_CONNECTION
hash("C_ERROR_NO_CONNECTION") - no internet connection
Mediators are configured with two functions: ads_wrapper.setup_video
and ads_wrapper.setup_banner
.
The function sets the order which creates the queue.
Options:
- order table required Ad display order. This is an array that contains objects like
{id = network_id, count = 2}
.- id number required Id that you get when registering a network using
ads_wrapper.register_network
. - count number optional how many times you need to show ads in a row in a queue. Default
1
- id number required Id that you get when registering a network using
- repeat_cut number optional specified if after the first cycle in queue it is necessary to cut off a part of the order. Default: the total number of all networks. Below are examples.
Examples:
-
Single network
All other parameters are not needed for one network.
ads_wrapper.setup_video({{id = test_id_1}})
Queue:
test_1->test_1->test_1->test_1->test_1->
-
Multiple networks with
repeat_cut
parameterads_wrapper.setup_video({{id = test_id_1, count = 2}, {id = test_id_2, count = 2}, {id = test_id_1, count = 1}}, 3)
Queue:
1: test_1->test_1->test_2->test_2->test_1->
2: test_2->test_2->test_1->
3: test_2->test_2->test_1->
- And so on
Part of the order is cut off after the first cycle.
-
Multiple networks without
repeat_cut
parameterrepeat_cut
automatically becomes3
.ads_wrapper.setup_video({{id = test_id_1, count = 1}, {id = test_id_2, count = 2}})
Queue:
1: test_1->test_2->test_2->
2: test_1->test_2->test_2->
3: test_1->test_2->test_2->
- And so on
In many functions there is a queue of calls. Before calling show_intertitial
ads_wrapper
will check if the ad has been loaded, and if not, it will load it first. If there is an error somewhere, the queue will break.
Registers network. Returns id.
Parameters
network
table required network moduleparams
table required network options. They are different for every network. They can be found in the networks section.
Return
- id number Network id. It is needed to set up the mediator.
Setups interstitial and reward mediator. More info here.
Parameters
- order table required Ad display order. This is an array that contains objects like
{id = network_id, count = 2}
. - repeat_cut number optional specified if after the first cycle in queue it is necessary to cut off a part of the order. Default: the total number of all networks.
Setups banner mediator. More info here.
Parameters
- order table required Ad display order. This is an array that contains objects like
{id = network_id, count = 2}
. - repeat_cut number optional specified if after the first cycle in queue it is necessary to cut off a part of the order. Default: the total number of all networks.
- _banner_auto_hide boolean optional The banner will be automatically hidden if
hide_banner
was called aftershow_banner
, but the banner did not have time to load. Default:false
Initializes ads_wrapper. Callback consists responses
field.
Queue: check_connection->request_idfa->init
Parameters
- initilize_video boolean optional check if need to initialize video networks
- initilize_banner boolean optional check if need to initialize banner networks
- callback function optional callback with response.
Initialize video networks.
Queue: check_connection->request_idfa->init
Parameters
- callback function optional callback with response
Initialize banner networks.
Queue: check_connection->request_idfa->init
Parameters
- callback function optional callback with response
Loads rewarded ads for next network.
Queue: check_connection->request_idfa->init->load_rewarded
Parameters
- callback function optional callback with response
Return
- id integer|nil id to cancel execution
Shows rewarded ads for next network. Callback contain a special code
field with events.C_SKIPPED if the user skipped the ad.
Queue: check_connection->request_idfa->init->load_rewarded->show_rewarded
Parameters
- callback function optional callback with response.
Return
- id integer|nil id to cancel execution
Loads interstitial ads for next network.
Queue: check_connection->request_idfa->init->load_interstitial
Parameters
- callback function optional callback with response
Return
- id integer|nil id to cancel execution
Shows interstitial ads for next network.
Queue: check_connection->request_idfa->init->load_interstitial->show_interstitial
Parameters
- callback function optional callback with response
Return
- id integer|nil id to cancel execution
Loads banner for for next network.
Queue: check_connection->request_idfa->init->load_banner
Parameters
- callback function optional callback with response
Return
- id integer|nil id to cancel execution
Shows setup banner for next network. Hides the previous banner if it was displayed.
Queue: check_connection->request_idfa->init->load_banner->show_banner
Parameters
- callback function optional callback with response
Return
- id integer|nil id to cancel execution
Hides setup banner for current network.
Parameters
- callback function optional callback with response
Unloads banner for current networks.
Parameters
- callback function optional callback with response
Check if the banner mediator is set up
Return
- value boolean
Check if the interstitial and rewarded video mediator is set up
Return
- value boolean
Check if ads wrapper is initiailzed
Return
- value boolean
Checks for internet connection
Return
- value boolean
Check if the interstitial video is loaded.
Default checks the next
network in mediator.
Parameters
- check_current boolean optional if need check current network. Default
false
Return
- value boolean
Check if the rewarded video is loaded.
Default checks the next
network in mediator.
Parameters
- check_current boolean optional if need check current network. Default
false
Return
- value boolean
Check if the banner is loaded.
Default checks the next
network in mediator.
Parameters
- check_current boolean optional if need check current network. Default
false
Return
- value boolean
Returns the current network pointed to by mediator Default returns for the video mediator
Parameters
- check_banner boolean optional if need to return mediator for banners. Default
false
Return
- network table|nil
Returns the next network pointed to by mediator Default returns for the video mediator
Parameters
- check_banner boolean optional if need to return mediator for banners. Default
false
Return
- network table|nil
Remove all registered networks
Сancel execution of the next step in the queue. For example, if you cancel show_interstitial while the ad is loading, then it will not be shown.
Parameters
- id number required queue id received from the function
Constants are used to set network parameters
- T_REWARDED hash("T_REWARDED")
- T_INTERSTITIAL hash("T_INTERSTITIAL")
- T_BANNER hash("T_BANNER")
This module can be used in network settings.
local platform = require("ads_wrapper.platform")
Constants:
- PL_ANDROID hash("PL_ANDROID")
- PL_IOS hash("PL_IOS")
- PL_HTML5 hash("PL_HTML5")
- PL_OTHER hash("PL_OTHER")
Parameters
- pl_value hash required One of the constants
Сheck if the platform is correct
Return
- value boolean
Returns current hash platform
Return
- value hash
You can use existing networks or create your own. More information can be found at the links:
- Test
- Admob
- Unity Ads
- Poki
- Yandex
- Yandex Mobile Ads
- Vk Bridge
- Applovin Max
- Admob and Unity Ads
- GameDistribution
- IronSource
You can create your own network. It is best to look at already made networks.
You can include output to the console of all operations that are done when calling functions with the queue.
local events = require("ads_wrapper.events")
local queue = require("ads_wrapper.queue")
queue.set_verbose_mode(events.V_ALL)
Constants:
- events.V_NONE hash("V_NONE") Output nothing. Default value.
- events.V_ERROR hash("V_ERROR") Show all errors
- events.V_SUCCESS hash("V_SUCCESS") Show all success responses
- events.V_ALL hash("V_ALL") Show all messages
Made by PotatoJam.
For example used:
MIT license.