终于接入成功了,给分享一下
首先先去快手开放平台申请对应小游戏的激励视频广告位和插屏广告位,banner我没接就不写了
拿到广告位ID后,在你项目里的广告脚本里加一个ksSdk脚本,名字随意
创建2个广告的变量备用
分别写2个创建广告的函数,adUnitId那边放你对应的广告位id
// 创建插屏广告
createChapingAd() {
// 创建插屏广告实例,提前初始化
this.chaPingAd = this.ks.createInterstitialAd({ adUnitId: adConfig.ksChaPingId })
}
/**创建激励广告 */
createRewardedVideoAd() {
let rewardedVideoAd = this.ks.createRewardedVideoAd({ adUnitId: adConfig.ksVideoId })
if (rewardedVideoAd) {
this.videoAd = rewardedVideoAd;
}
}
然后写2个展示广告的函数
/**
* 插屏广告
*/
showChapingAd(delay) {
// 延迟显示
this.scheduleOnce(() => {
let p = this.chaPingAd.show();
p.then(function (result) {
// 插屏广告展示成功
console.log(`show interstitial ad success, result is ${result}`)
}).catch(function (error) {
// 插屏广告展示失败
console.log(`show interstitial ad failed, error is ${error.code}`)
if (error.code === -10005) {
// 表明当前app版本不支持插屏广告,可以提醒用户升级app版本
console.log(`你的app版本不支持插屏广告`);
}
if (error.code === -20037) {
// 表明当前app版本不支持插屏广告,可以提醒用户升级app版本
console.log(`30秒后才可再展示插屏广告`);
}
})
}, delay)
}
/**
* 激励视频广告
*/
showVideoAd() {
return new Promise((resolve, reject) => {
this.videoAd.load().then(() => {
this.videoAd.show();
});
let closeListener = (res: { isEnded: boolean }) => {
let isComplete: boolean;
// 小于 2.1.0 的基础库版本,res 是一个 undefined
if (res && res.isEnded || res === undefined) {
isComplete = true;
resolve(isComplete);
} else {
isComplete = false;
resolve(isComplete);
}
// this.closeRewardVideo();
this.videoAd.offClose(closeListener);
};
let errorListener = (res: { errMsg: string; errCode: number }) => {
console.log('=====> @framework, 奖励视频发生错误:', res);
// 再拉一次视频
this.videoAd.load()
.then(() => {
return this.videoAd.show();
})
.then(() => {
this.videoAd.offError(errorListener);
})
.catch(() => {
reject();
this.videoAd.offError(errorListener);
});
};
this.videoAd.onClose(closeListener);
this.videoAd.onError(errorListener);
})
}
到这一步就完成80%了,在游戏创建的时候(反正在你展示广告前都行)调用一次前面创建广告的函数
然后就可以播放广告啦!
最后有2点说一下
1.快手插屏广告得在游戏运行后30秒后才能调用成功,不然会报错(我刚开始还以为是写错了)
2.快手的api ks. 在调用的时候会报错,可以不管,在实际快手平台运行的时候不会报错的,只是在你的编辑器里没有这个东西所以报错,介意的话在你调用到的脚本里加一个public ks = window['ks'];,然后用this.ks 代替ks就好
新人菜鸡第一次发贴,写的不好请多担待