init
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
import { _decorator, Component, director, game, Node, sys } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('WXSdk')
|
||||
export class WXSdk extends Component {
|
||||
|
||||
@property({ displayName: "原生广告uid" })
|
||||
bannerId: string = "";
|
||||
|
||||
@property({ displayName: "视频奖励广告uid" })
|
||||
videoUid: string = "";
|
||||
|
||||
@property({ displayName: "插屏广告uid" })
|
||||
interstitialUid: string = "";
|
||||
|
||||
@property({ displayName: "分享图url" })
|
||||
shareImageUrl: string = "https://mmocgame.qpic.cn/wechatgame/fNvVoACuBEOmNhzE802RLqnUuQyZ4rXTicDqz7OlVKypjYcHHo7nyfoYe7Urvdk6T/0";
|
||||
|
||||
@property({ displayName: "分享tile文本" })
|
||||
shareTilt: string = "超好玩解压停车场";
|
||||
|
||||
bannerAdv = null
|
||||
videoAdv = null
|
||||
interstitialAd = null
|
||||
|
||||
|
||||
static instance: WXSdk;
|
||||
|
||||
protected onLoad(): void {
|
||||
WXSdk.instance = this
|
||||
}
|
||||
successCallback = null
|
||||
|
||||
initBanner() {
|
||||
let winSize = wx.getSystemInfoSync();//获取像素size
|
||||
let bannerWidth = 300
|
||||
let bannerHeight = 80
|
||||
// 创建 原生模板 广告实例,提前初始化
|
||||
let CustomAd = wx.createCustomAd({
|
||||
adUnitId: 'adunit-dbdd10d1ca3690d2',
|
||||
style: {
|
||||
left: (winSize.windowWidth - bannerWidth) / 2,
|
||||
top: winSize.windowHeight - bannerHeight,
|
||||
width: bannerWidth,
|
||||
}
|
||||
})
|
||||
// // 在适合的场景显示 原生模板 广告
|
||||
// CustomAd.show()
|
||||
|
||||
// 监听 原生模板 广告错误事件
|
||||
CustomAd.onError(err => {
|
||||
console.error(err.errMsg)
|
||||
});
|
||||
|
||||
this.bannerAdv = CustomAd
|
||||
|
||||
}
|
||||
|
||||
initVideo() {
|
||||
// 创建激励视频广告实例,提前初始化
|
||||
this.videoAdv = wx.createRewardedVideoAd({
|
||||
adUnitId: this.videoUid//传入自己的id,此处为视频广告位ID
|
||||
})
|
||||
}
|
||||
|
||||
initInterstitial() {
|
||||
if (wx.createInterstitialAd) {
|
||||
this.interstitialAd = wx.createInterstitialAd({
|
||||
adUnitId: this.interstitialUid//传入自己的id,此处为插屏广告位ID
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
init() {
|
||||
if (sys.platform == sys.Platform.WECHAT_GAME) {
|
||||
|
||||
this.initBanner()
|
||||
this.initVideo()
|
||||
// 创建插屏广告实例,提前初始化
|
||||
this.initInterstitial()
|
||||
this.passivityShare()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 主动分享
|
||||
public initativeShare(callBack?: any) {
|
||||
let shareTitle = this.shareTilt
|
||||
let imageUrl = this.shareImageUrl
|
||||
// 主动拉起转发,进入选择通讯录界面
|
||||
wx.shareAppMessage({
|
||||
// 转发标题, 没有则默认使用小游戏的昵称
|
||||
title: shareTitle,
|
||||
// 图片链接,
|
||||
imageUrl: imageUrl
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
passivityShare(callBack?: any) {
|
||||
// 授权显示菜单
|
||||
wx.showShareMenu({
|
||||
withShareTicket: false,
|
||||
// 显示菜单
|
||||
menus: ['shareAppMessage', 'shareTimeline'],
|
||||
success: (res) => {
|
||||
console.log("开始被动转发shareMenu成功");
|
||||
},
|
||||
fail: () => {
|
||||
console.log("开始被动转发shareMenu失败");
|
||||
}
|
||||
});
|
||||
let shareTitle = this.shareTilt
|
||||
let imageUrl = this.shareImageUrl
|
||||
// 好友点击回调
|
||||
wx.onShareAppMessage(() => {
|
||||
return {
|
||||
// 转发标题, 没有则默认使用小游戏的昵称
|
||||
title: shareTitle,
|
||||
// 图片链接,
|
||||
imageUrl: imageUrl
|
||||
};
|
||||
});
|
||||
// 朋友圈点击回调
|
||||
wx.onShareTimeline(() => {
|
||||
return {
|
||||
title: shareTitle,
|
||||
imageUrl: imageUrl
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
showBanner() {
|
||||
this.bannerAdv.show()
|
||||
}
|
||||
|
||||
hideBanner() {
|
||||
this.bannerAdv.hide()
|
||||
}
|
||||
|
||||
showInterstitial() {
|
||||
// 在适合的场景显示插屏广告
|
||||
if (this.interstitialAd) {
|
||||
this.interstitialAd.show().catch((err) => {
|
||||
console.error(err)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
showVideo(successCallback, failCallback) {
|
||||
// 用户触发广告后,显示激励视频广告
|
||||
|
||||
if (this.videoUid == "test" || this.videoUid == "") {
|
||||
successCallback()
|
||||
return
|
||||
}
|
||||
let self = this
|
||||
self.videoAdv.show().catch(() => {
|
||||
// 失败重试
|
||||
self.videoAdv.load()
|
||||
.then(() => self.videoAdv.show())
|
||||
.catch(err => {
|
||||
console.log('激励视频 广告显示失败')
|
||||
failCallback()
|
||||
})
|
||||
})
|
||||
//拉取异常处理
|
||||
self.videoAdv.onError((err) => {
|
||||
console.log(err);
|
||||
failCallback()
|
||||
})
|
||||
self.videoAdv.onClose((res) => {
|
||||
if (!self.videoAdv) return;
|
||||
self.videoAdv.offClose();//需要清除回调,否则第N次广告会一次性给N个奖励
|
||||
//关闭
|
||||
if (res && res.isEnded || res === undefined) {
|
||||
//正常播放结束,需要下发奖励
|
||||
successCallback()
|
||||
} else {
|
||||
//播放退出,不下发奖励
|
||||
failCallback()
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user