Laya-小米快游戏对接文件

export default class xmSdk {
    static AdId = '12345';//广告id

    static bannerAd = null;
    static videoInstance = null;

    /**
     * 获取对象
     */
    static xmObj: any = Laya.Browser.window.qg;

    // static isDevtools: Boolean = false;

    //播放banner广告
    static playBannerAd: Function = (complete?: Function, close?: Function): void => {
        let xm = xmSdk.xmObj;
        // console.log('创建bannerad', xm);
        if (xm != undefined) {
            let width = Laya.Browser.clientWidth;
            let height = Laya.Browser.clientHeight;
            var targetBannerAdWidth = 128;
            if (xmSdk.bannerAd) {
                xmSdk.bannerAd.hide();
                // xmSdk.bannerAd.destory();
            }
            // console.log('创建bannerad')
            // 创建一个居于屏幕底部正中的广告
            xmSdk.bannerAd = xm.createBannerAd({
                adUnitId: xmSdk.AdId,
                style: {
                    width: targetBannerAdWidth,
                    top: height - (targetBannerAdWidth / 16) * 9, // 根据系统约定尺寸计算出广告高度
                },
            });
            // console.log('创建bannerad完成',xmSdk.bannerAd)

            xmSdk.bannerAd.onResize((size) => {
                // console.log(size.width, size.height);
                xmSdk.bannerAd.style.top = height - size.height;
                xmSdk.bannerAd.style.left = (width - size.width) / 2;
                
            });
            // console.log('bannerad重设大小完成',xmSdk.bannerAd.style.top,xmSdk.bannerAd.style.left)
            xmSdk.bannerAd.onLoad(function () {
                console.log('load success')
            });
            xmSdk.bannerAd.onError(data => {
                console.log(`Banner Error: errorMsg: ${data.msg}, erroCode: ${data.code}`)
            });
            xmSdk.bannerAd.show();
            // console.log('显示成功')
        } else {
            if (complete) {
                complete();
            }
            if (close) {
                close();
            }
        }
    }
    static hideBannerAd() {
        if (this.bannerAd) {
            this.bannerAd.hide();
        }
    }

    /**
     * 播放激励视频广告
     */
    static playVideoAd: any = (complete?: any, close?: any): void => {
        let xm = xmSdk.xmObj;
        if (xm != undefined) {
            xmSdk.videoInstance = xm.createRewardedVideoAd({
                adUnitId: xmSdk.AdId,
            });
            // 显示激励广告
            xmSdk.videoInstance.show()
                .then(() => {
                    console.log("激励广告显示成功");
                    //播放激励广告视频
                }).catch((err) => {
                    console.log("激励广告组件出现问题", err);
                    if (err.errCode != 1004) {
                        // 这里需要调用组件提示
                        // code here

                    }
                    if (close) {
                        close();
                    }

                });
            xmSdk.videoInstance.onClose((res) => {
                if (res.isEnded) {
                    // 给予奖励
                    if (complete) {
                        complete();
                        complete = null;
                    }
                } else {

                    if (close) {
                        close();
                    }
                }
                xmSdk.clearTheVideoAd();
                xmSdk.videoInstance = null;
            });
        } else {
            //播放广告视频
            if (complete) {
                complete();
            }
            if (close) {
                close();
            }
        }
    }

    public static clearTheVideoAd() {
        if (xmSdk.videoInstance != null) {
            xmSdk.videoInstance.offLoad(() => { });
            xmSdk.videoInstance.offClose(() => { });
            xmSdk.videoInstance.close();
            xmSdk.videoInstance = null;
        }
    }

    /**
     * 播放插屏广告
     * @param complete 
     */
    static playPageAd: Function = (complete?: Function, error?: Function): void => {
        console.log("调用playPageAd()展示插屏广告");
        let xm = xmSdk.xmObj;
        if (xm != undefined) {
            let interstitialAd = xm.createInterstitialAd({
                adUnitId: xmSdk.AdId,
            });
            interstitialAd.show().then(() => {
                console.log("插屏广告显示成功");
                //播放激励广告视频
            }).catch((err) => {
                console.log("插屏广告组件出现问题", err);
                if (err.errCode != 1004) {//这里的errCode是其他模板里的,自行修改吧
                    // 这里需要调用组件提示
                    // code here

                }
                if (close) {
                    close();
                }

            });
        }
    }




    /**
     * 
     * @param callback 
     */
    static onHide(callback?: any) {
        this.xmObj.onHide(() => {
            if (callback) callback();
        });
    }
    static login() {
        this.xmObj.login({
            success(res) {
                console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
            },
            fail(res) {
                console.log(`login 调用失败`);
            },
        });
    }


}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值