Laya-MEIZU 对接文件

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);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值