mzSDK-根据公司给的对接模板修改的,可借鉴,照搬不一定有用,(里面有些小bug,你们自己修改下吧)
export default class mzSdk {
static bannerAdId = '';//banner广告id
static rewardAdId = '';//激励视频广告id
static pageAdId = '';//插屏广告id
static bannerAd = null;
static videoInstance = null;
/**
* 获取游戏对象
*/
static mzObj: any = Laya.Browser.window.qg;
static isDevtools: Boolean = false;
static createBannerAd() {
let mz = mzSdk.mzObj;
if (mz != undefined) {
var screenHeight = qg.getSystemInfoSync().screenHeight;
var screenWidth = qg.getSystemInfoSync().screenWidth;
mzSdk.bannerAd = mz.createBannerAd({
adUnitId: mzSdk.bannerAdId,
style: {
left: 0,
top: screenHeight - screenWidth / 6.7,
width: screenWidth, // 设置banner需要的宽度,横屏游戏宽度建议使用参考值1440,必须设置
height: screenWidth / 6.7 // 广告期望高度,在onResize里面可以根据广告的高度重新设置高度
}
});
mzSdk.bannerAd.onResize((res) => {
console.log("Banner 尺寸改变");
let screenHeight = qg.getSystemInfoSync().screenHeight;
//重新修改位置
mzSdk.bannerAd.style={
top:screenHeight - res.height,
left:0,
width:res.width,
height:res.height
}
});
mzSdk.bannerAd.onLoad(function () {
console.log("banner 广告加载成功");
})
}
}
static showBannerAd() {
mzSdk.bannerAd.show();
}
//播放banner广告
static playBannerAd: Function = (): void => {
let mz = mzSdk.mzObj;
if (mz != undefined) {
var screenHeight = qg.getSystemInfoSync().screenHeight;
var screenWidth = qg.getSystemInfoSync().screenWidth;
if (mzSdk.bannerAd) {
mzSdk.bannerAd.hide();
}
// 创建一个居于屏幕底部正中的广告
mzSdk.bannerAd = mz.createBannerAd({
adUnitId: mzSdk.bannerAdId,
style: {
left: 0,
top: screenHeight - screenWidth / 6.7,
// 设置banner需要的宽度,横屏游戏宽度建议使用参考值1440,必须设置
width: screenWidth,
// 广告期望高度,在onResize里面可以根据广告的高度重新设置高度
height: screenWidth / 6.7
}
});
mzSdk.bannerAd.onResize((res) => {
console.log("Banner 尺寸改变");
let screenHeight = qg.getSystemInfoSync().screenHeight;
//确定左上角位置,为底部位置
mzSdk.bannerAd.style={
top:screenHeight - res.height,
left:0,
width:res.width,
height:res.height
}
});
mzSdk.bannerAd.onLoad(function () {
console.log("banner 广告加载成功");
mzSdk.bannerAd.show();
})
}
}
static hideBannerAd() {
if (this.bannerAd) {
console.log("banner 广告隐藏");
this.bannerAd.hide();
}
}
/**
* 播放视频广告
*/
static playVideoAd: any = (complete?: any, close?: any): void => {
let mz = mzSdk.mzObj;
if (mz != undefined) {
mzSdk.videoInstance = mz.createRewardedVideoAd({
adUnitId: mzSdk.rewardAdId,
});
// 显示广告
mzSdk.videoInstance.load();
mzSdk.videoInstance.show();
mzSdk.videoInstance.onClose(() => {
console.log("激励视频观看完毕,发放奖励");
// code here
if (complete) {
complete();
}
if (close) {
close();
}
mzSdk.clearTheVideoAd();
});
} else {
//播放广告视频
if (complete) {
complete();
}
if (close) {
close();
}
}
}
public static clearTheVideoAd() {
if (mzSdk.videoInstance != null) {
mzSdk.videoInstance.offLoad(() => { });
mzSdk.videoInstance.offClose(() => { });
mzSdk.videoInstance.destroy();
mzSdk.videoInstance = null;
}
}
/**
* 播放插屏广告
* @param complete
*/
static playPageAd: Function = (): void => {
console.log("插屏广告显示");
let mz = mzSdk.mzObj;
if (mz != undefined) {
let insertAd = mz.createInsertAd({
adUnitId: mzSdk.pageAdId,
});
insertAd.load();
insertAd.onLoad(function () {
console.log("插屏广告加载成功");
insertAd.show();
})
}
}
/**
*获取平台信息
*
* @memberof mzSdk
*/
static getSystemInfoSync() {
let mz = mzSdk.mzObj;
let res = mz.getSystemInfoSync();
if (res.platform == "devtools") {
this.isDevtools = true;
} else {
this.isDevtools = false;
}
console.log(res, "系统信息");
return res;
}
/**
* 埋点
* @param key
* @param value
*/
static ReportAnalytics(key: string, value: { [key: string]: number | boolean | string }) {
let mz = mzSdk.mzObj;
const systemInfo = mz.getSystemInfoSync();
let sdkVersion: string = systemInfo.SDKVersion;
let tempArr = sdkVersion.split('.');
if (parseInt(tempArr[0]) < 1 || parseInt(tempArr[1]) < 8) {
return;
}
mz.reportAnalytics(key, value);
}
}