【Defold】admob アセットのラッパーモジュールを作成しました
Google の admob 広告配信アセット admob の操作を少し楽にするラッパーモジュールです。
local M = {}
--[[
初期化。
]]
function M.init()
M.admob_ids =
{
['iPhone OS'] =
{
banner = 'ca-app-pub-3940256099942544/2934735716',
interstitial = 'ca-app-pub-3940256099942544/4411468910',
rewarded = 'ca-app-pub-3940256099942544/1712485313',
},
Android =
{
banner ='ca-app-pub-3940256099942544/6300978111',
interstitial = 'ca-app-pub-3940256099942544/1033173712',
rewarded = 'ca-app-pub-3940256099942544/5224354917',
}
}
local system_name = sys.get_sys_info().system_name
M.admob_ids = M.admob_ids[system_name] or {}
local function listener(event)
print('admob event type', event.type)
print('admob event phase', event.phase)
print(event)
if event.type == 'init' then
M.load_banner(event)
elseif event.type == 'banner' then
M.listener_banner_event(event)
elseif event.type == 'interstitial' then
M.listener_interstitial_event(event)
elseif event.type == 'rewarded' then
M.listener_rewarded_event(event)
end
end
admob.init{ test = true, listener = listener}
-- admob.enable_debug()
-- status reset.
M.reset()
end
--[[
初期化。
]]
function M.reset()
-- banner
M.banner_close = false
M.banner_failed_to_load = false
M.banner_left_application = false
M.banner_loaded = false
M.banner_opened = false
-- interstitial
M.interstitial_close = false
M.interstitial_failed_to_load = false
M.interstitial_left_application = false
M.interstitial_loaded = false
M.interstitial_opened = false
-- reward
M.reward_close = false
M.reward_failed_to_load = false
M.reward_left_application = false
M.reward_loaded = false
M.reward_opened = false
M.reward_complete = false
M.reward_started = false
end
--[[
バナーイベント広告イベント
]]
function M.listener_banner_event(event)
if event.phase == 'closed' then
M.banner_close = true
elseif event.phase == 'failed_to_load' then
M.banner_failed_to_load = true
elseif event.phase == 'left_application' then
M.banner_left_application = true
elseif event.phase == 'loaded' then
M.banner_loaded = true
elseif event.phase == 'opened' then
M.banner_opened = true
end
end
--[[
インタースティシャル広告イベント
]]
function M.listener_interstitial_event(event)
if event.phase == 'closed' then
M.interstitial_close = true
elseif event.phase == 'failed_to_load' then
M.interstitial_failed_to_load = true
elseif event.phase == 'left_application' then
M.interstitial_left_application = true
elseif event.phase == 'loaded' then
M.interstitial_loaded = true
elseif event.phase == 'opened' then
M.interstitial_opened = true
end
end
--[[
リワード広告イベント
]]
function M.listener_rewarded_event(event)
if event.phase == 'closed' then
M.reward_close = true
elseif event.phase == 'failed_to_load' then
M.reward_failed_to_load = true
elseif event.phase == 'left_application' then
M.reward_left_application = true
elseif event.phase == 'loaded' then
M.reward_loaded = true
elseif event.phase == 'opened' then
M.reward_opened = true
elseif event.phase == 'rewarded' then
M.reward_complete = true
elseif event.phase == 'started' then
M.reward_started = true
end
end
function M.load_interstitial()
M.interstitial_close = false
M.interstitial_failed_to_load = false
M.interstitial_left_application = false
M.interstitial_loaded = false
M.interstitial_opened = false
admob.load{
type = 'interstitial',
id = M.admob_ids.interstitial,
immersive = true,
keywords = {'puzzle', 'game'},
non_personalized = true
}
end
function M.load_rewarded_video()
M.reward_close = false
M.reward_failed_to_load = false
M.reward_left_application = false
M.reward_loaded = false
M.reward_opened = false
M.reward_complete = false
M.reward_started = false
admob.load{
type = 'rewarded',
id = M.admob_ids.rewarded,
immersive = true,
keywords = {'puzzle', 'game'}
}
end
-- Show.
function M.show_interstitial()
admob.show('interstitial')
end
function M.show_rewarded_video()
admob.show('rewarded')
end
-- Is loaded?
function M.is_banner_loaded()
return admob.is_loaded('banner')
end
function M.is_interstitial_loaded()
return admob.is_loaded('interstitial')
end
function M.is_rewarded_loaded()
return admob.is_loaded('rewarded')
end
-- Banner.
function M.load_banner()
admob.load{
type = 'banner',
id = M.admob_ids.banner,
size = 'smart',
position = 'bottom',
keywords = {'puzzle', 'game'}
}
end
function M.hide_banner()
admob.hide_banner()
end
function M.is_rewarded_complete()
return M.reward_complete
end
function M.is_rewarded_close()
return M.reward_close
end
function M.is_reward_failed_to_load()
return M.reward_failed_to_load
end
return M
init の admob_ids はテスト用広告を設定しています。リリース時には変更すること。