init
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
import { _decorator, Component, director, game, Node, sys } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('DYSdk')
|
||||
export class DYSdk extends Component {
|
||||
|
||||
@property({ displayName: "bannner广告uid" })
|
||||
bannerId: string = "test";
|
||||
|
||||
@property({ displayName: "视频奖励广告uid" })
|
||||
videoUid: string = "test";
|
||||
|
||||
@property({ displayName: "插屏广告uid" })
|
||||
interstitialUid: string = "test";
|
||||
|
||||
@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: DYSdk;
|
||||
|
||||
protected onLoad(): void {
|
||||
DYSdk.instance = this
|
||||
}
|
||||
successCallback = null
|
||||
failedCallback = null
|
||||
|
||||
initBanner() {
|
||||
let winSize = window["tt"].getSystemInfoSync();//获取像素size
|
||||
// 创建 Banner 广告实例,提前初始化
|
||||
let bannerWidth = 300
|
||||
let bannerHeight = 80
|
||||
console.error("bannerId:", this.bannerId)
|
||||
this.bannerAdv = window["tt"].createBannerAd({
|
||||
adUnitId: this.bannerId,//传入自己的id,此处为banner广告位ID
|
||||
adIntervals: 30,//定时刷新,最低30S
|
||||
style: {
|
||||
left: (winSize.windowWidth - bannerWidth) / 2,
|
||||
top: winSize.windowHeight - bannerHeight,
|
||||
width: bannerWidth,
|
||||
},
|
||||
})
|
||||
//重新定banner位置
|
||||
this.bannerAdv.onResize((res) => {
|
||||
this.bannerAdv.style.top = winSize.windowHeight - this.bannerAdv.style.realHeight - 1;
|
||||
})
|
||||
|
||||
this.bannerAdv.onError((err) => {
|
||||
console.log("banner", err);
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
navigateToScene() {
|
||||
window["tt"].navigateToScene({
|
||||
scene: 'sidebar',
|
||||
fail: console.log,
|
||||
success: console.log
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
initVideo() {
|
||||
let self = this
|
||||
// 创建激励视频广告实例,提前初始化
|
||||
console.log("激励视频广告id:", this.videoUid)
|
||||
this.videoAdv = window["tt"].createRewardedVideoAd({
|
||||
adUnitId: this.videoUid//传入自己的id,此处为视频广告位ID
|
||||
})
|
||||
|
||||
this.videoAdv.onLoad(() => {
|
||||
console.log("广告加载完成");
|
||||
});
|
||||
|
||||
this.videoAdv.onError(() => {
|
||||
console.log("广告错误");
|
||||
if (self.failedCallback) {
|
||||
self.failedCallback();
|
||||
}
|
||||
});
|
||||
|
||||
function listener(res) {
|
||||
if (res && res.isEnded) {
|
||||
if (self.successCallback) {
|
||||
self.successCallback();
|
||||
self.successCallback = null
|
||||
}
|
||||
} else {
|
||||
if (self.failedCallback) {
|
||||
self.failedCallback();
|
||||
self.failedCallback = null
|
||||
}
|
||||
}
|
||||
}
|
||||
this.videoAdv.onClose(listener); //这里监听广告的close 事件
|
||||
}
|
||||
|
||||
initInterstitial() {
|
||||
if (window["tt"].createInterstitialAd) {
|
||||
console.error("插屏广告id:", this.interstitialUid)
|
||||
this.interstitialAd = window["tt"].createInterstitialAd({
|
||||
adUnitId: this.interstitialUid//传入自己的id,此处为插屏广告位ID
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
init() {
|
||||
if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
|
||||
|
||||
this.initBanner()
|
||||
this.initVideo()
|
||||
// 创建插屏广告实例,提前初始化
|
||||
this.initInterstitial()
|
||||
this.passivityShare()
|
||||
|
||||
|
||||
window["tt"].onShow(op => {
|
||||
if (op.location == "sidebar_card") {
|
||||
console.log("应用侧边栏启动");
|
||||
}
|
||||
console.log("监听应用启动", op);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 主动分享
|
||||
public initativeShare(callBack?: any) {
|
||||
let shareTitle = this.shareTilt
|
||||
let imageUrl = this.shareImageUrl
|
||||
// 主动拉起转发,进入选择通讯录界面
|
||||
window["tt"].shareAppMessage({
|
||||
// 转发标题, 没有则默认使用小游戏的昵称
|
||||
title: shareTitle,
|
||||
// 图片链接,
|
||||
imageUrl: imageUrl
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
passivityShare(callBack?: any) {
|
||||
// 授权显示菜单
|
||||
window["tt"].showShareMenu({
|
||||
withShareTicket: false,
|
||||
// 显示菜单
|
||||
menus: ['shareAppMessage', 'shareTimeline'],
|
||||
success: (res) => {
|
||||
console.log("开始被动转发shareMenu成功");
|
||||
},
|
||||
fail: () => {
|
||||
console.log("开始被动转发shareMenu失败");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
showBanner() {
|
||||
this.bannerAdv.show()
|
||||
}
|
||||
|
||||
hideBanner() {
|
||||
this.bannerAdv.hide()
|
||||
}
|
||||
|
||||
showInterstitial() {
|
||||
// 在适合的场景显示插屏广告
|
||||
if (this.interstitialAd) {
|
||||
this.interstitialAd.show().catch((err) => {
|
||||
console.error(err)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
showVideo(successCallback, failCallback) {
|
||||
// 用户触发广告后,显示激励视频广告
|
||||
let self = this
|
||||
|
||||
this.successCallback = successCallback
|
||||
this.failedCallback = failCallback
|
||||
|
||||
self.videoAdv.onError((res) => {
|
||||
console.log('广告出错监听', res)
|
||||
})
|
||||
// /**展示 */
|
||||
self.videoAdv
|
||||
.show()
|
||||
.then(() => {
|
||||
})
|
||||
.catch(err => {
|
||||
console.log('打印激励视频错误', err)
|
||||
// 可以手动加载一次
|
||||
self.videoAdv.load().then(() => {
|
||||
// 加载成功后需要再显示广告
|
||||
return self.videoAdv.show();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user