unity官方或者谷歌adSence 并没有支持相关接入,只有admob的接入。网上没有找到一篇相关文章和案例,没办法,只能自己研究。
但是可以通过 unity 调用js的方式来实现 adSence 广告播放。
首先在打好包的文件夹内找到index.html,将谷歌 adSence script引入
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XXXXXX index.html 简略</title>
//谷歌sdk 记得替换广告id
<script async data-adbreak-test="on" data-admob-rewarded-slot="ca-app-pub-1234567890123456/22222222" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234567890123456&data-admob-rewarded-slot" crossorigin="anonymous"></script>
<script>window.adsbygoogle = window.adsbygoogle || []; var adBreak = adConfig = function(o) {adsbygoogle.push(o);} </script>
</head>
<body>
</body>
<html/>
然后unity调用js 控制广告播放和状态获取就行了
JS代码
//进入游戏调用/或者可以放在 index.html中
function initAd() {
console.log('google init ad....')
let adsbygoogle = window['adsbygoogle'] || [];
if (adsbygoogle) {
window['adConfig']({
sound: 'on',
preloadAdBreaks: 'on',
onReady: () => {
console.log("AdSense onReady");
},
})
}
}
//激励视频广告 call,fail 成功回调
function showRewardAd(call, fail) {
let isReward = false;
window["adBreak"] && window["adBreak"]({
type: 'reward', // ad shows at start of next level
name: 'restart-game',
beforeAd: () => {
//激励视频开始播放
}, // You may also want to mute the game's sound.
afterAd: () => {
if (isReward === true) {
console.log('发放奖励');
call();
}
}, // resume the game flow.
beforeReward: (showAdFn) => {
showAdFn && showAdFn();
},
adDismissed: () => {
fail();
//中途关闭广告
},
adViewed: () => {
isReward = true;
},
adBreakDone: () => {
//Always called (if provided) even if an ad didn't show(始终调用,即使广告展示失败了)
}
});
}
function showTailAd() {
window["adBreak"]({
type: 'preroll',
adBreakDone: (e) => {
console.log("前贴片显示失败:", e);
},
})
}
function showInterstitialAd() {
window["adBreak"] && window["adBreak"]({
type: 'browse',
name: 'restart-game',
beforeAd: () => {
console.log("插屏");
}, // You may also want to mute the game's sound.
afterAd: () => {
console.log("插屏");
}, // resume the game flow.
});
}