1.前言
首先要登录微信公众平台,进入对应项目,在左侧点击“流量主”。
开通条件:
(1)累计访客不低于1000人。
(2)如果小程序有过严重违规记录的,不给予开通流量主。
符合以上条件的,正常情况下微信小程序平台会给你内测链接。
要添加财务信息,比如公司的银行账号,公司取款人的名字等。
提交大约2天就可以通过,通过后可开始创建广告位。
要填写如下:
广告位类型
广告位名称
创建完成会显现如下:
广告位类型 广告位名称 广告位ID(由微信生成) 广告位状态
其中,广告位ID会在代码在用到。
接口:
https://developers.weixin.qq.com/minigame/dev/tutorial/ad/rewarded-video-ad.html
2.代码
Common.js-------------
compareVersion: function (v1, v2) {//比较版本
v1 = v1.split('.');
v2 = v2.split('.');
var len = Math.max(v1.length, v2.length);
while (v1.length < len) {
v1.push('0');
}
while (v2.length < len) {
v2.push('0');
}
for (var i = 0; i < len; i++) {
var num1 = parseInt(v1[i]);
var num2 = parseInt(v2[i]);
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
},
isCanShowVideo: function(){
var ret = false;
if(cc.sys.platform === cc.sys.WECHAT_GAME){
var current_version = wx.getSystemInfoSync().SDKVersion;
if (this.compareVersion(current_version, "2.0.4") === -1){
console.log('=====版本不够2.0.4,视频广告不能用')
}else{
ret = true;
}
}
return ret;
},
createVideo: function(self){ //创建视频
if(!this.isCanShowVideo()) return;
if(!this.videoAd){
this.videoAd = wx.createRewardedVideoAd({adUnitId: 'adunit-e0dd75038579c0c7'});
}
this.videoAd.bind_this = self;
this.videoAd.onClose(this.videoAd.bind_this.adSuccessPlay);
},
releaseVideo: function(){ //释放视频
if(!this.isCanShowVideo()) return;
if(this.videoAd && this.videoAd.bind_this){
this.videoAd.offClose(this.videoAd.bind_this.adSuccessPlay);
this.videoAd.bind_this = null;
}
},
loadVideo: function(){ //加载并播放视频
if(!this.isCanShowVideo()) return;
if(this.isSoundOn == 1 && this.bgmId){
cc.audioEngine.pause(this.bgmId); //停止背景音乐
}
this.videoAd.load().then(() => this.videoAd.show().catch(err => console.log(err)));
},
level205.js-------------
onDestory:function(){
common.releaseVideo();
},
onPlayVideoBtn: function(){ //播放视频按钮
if(!this.only2 || !this.only3 || !(cc.sys.platform === cc.sys.WECHAT_GAME)) return;
this.only3 = false;
this.playBtnSound();
common.createVideo(this); //创建视频
common.loadVideo();
},
adSuccessPlay: function (res) {
console.log('=============205关播放回调',res);
var current_version = wx.getSystemInfoSync().SDKVersion;
if (common.compareVersion(current_version, "2.1.0") === -1){
console.log("播放完视频1");
common.videoAd.bind_this.afterLookVideo(0);
}else{
console.log("播放完视频2:", res);
if (res && res.isEnded || res === undefined) { // 正常播放结束,可以下发游戏奖励
console.log("播放完视频3");
common.videoAd.bind_this.afterLookVideo(0);
} else { // 播放中途退出,不下发游戏奖励播放中途退出,不下发游戏奖励
console.log("播放完视频4");
common.videoAd.bind_this.afterLookVideo(1);
}
}
if(common.isSoundOn == 1 && common.bgmId){
var state = cc.audioEngine.getState(common.bgmId); //-1error,0initalzing,1playing,2paused
if(state == 2){ //暂停状态
cc.audioEngine.resume(common.bgmId); //恢复背景音乐
}
}
common.videoAd.offClose(common.videoAd.bind_this.adSuccessPlay);
},
afterLookVideo: function(){ //看完视频
if(!this.only4 || !this.btnNode[0] || !this.shadeNode) return;
this.only4 = false;
console.log("205关-看完视频");
this.btnNode[1].active = false;
this.btnNode[2].active = true;
this.shadeNode.active = false;
this.scheduleOnce(function(){
this.only3 = true;
this.only4 = true;
console.log("205关看视频-only归零");
}, 0.5);
},