华为快游戏广告接入指南[小白快速上手]

版本要求

Cocos Creator – 3.1.1

支持的广告类型

  • 激励视频
  • 原生广告

相关参考链接

激励视频

playVideoAd(function callback)

加载并播放激励视频广告

参数

function callback

监听激励视频完成的回调方法

参数

boolean res

是否要发放激励视频奖励

示例代码

HuaweiQuickGame.getApp().videoPlay((res) => {
    if (res) {
        //给予用户激励视频奖励
    }
});

原生广告

getNativeData(function callback);

获取一组原生广告数据

参数

function callback

加载原生广告数据完成回调

参数

json data

返回的原生广告数据

示例代码

HuaweiQuickGame.getApp().getNativeData((data: any) => {
    
});

全部代码

import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('HuaweiQuickGame')
export class HuaweiQuickGame extends Component {
    static _app: HuaweiQuickGame | null = null;
    static getApp() {
        if (!HuaweiQuickGame._app) {
            HuaweiQuickGame._app = new HuaweiQuickGame();
        }
        return HuaweiQuickGame._app;
    }
    //原生广告ID
    nativeId: string = 'x2e3b5w2cy';
    //激励视频广告ID
    videoId: string = 'd5lutjqgdh';
    nativeObject: any = null;
    nativeCall: any = null;
    /**
     * 获取一组原生数据
     * @param id 原生广告ID
     * @param call 获取数据回调
     */
    getNativeData(call: Function = function () { }) {
        let qg = window.qg;
        if (!this.nativeObject) {
            this.nativeObject = qg.createNativeAd({
                adUnitId: this.nativeId,
                success: (code: any) => {
                    console.log("loadNativeAd loadNativeAd : success");
                },
                fail: (data: any, code: any) => {
                    console.log("loadNativeAd loadNativeAd fail: " + data + "," + code);
                },
                complete: () => {
                    console.log("loadNativeAd loadNativeAd : complete");
                }
            });
            this.nativeObject.onLoad((data: any) => {
                this.nativeCall(data.adList[0]);
            });
        }
        this.nativeCall = call;
        this.nativeObject.load();
    
    }
    setNativeShow(data: any) {
        if (this.nativeObject) {
            this.nativeObject.reportAdShow({ adId: data.adId });
        }
    }
    setNativeClick(data: any) {
        if (this.nativeObject) {
            this.nativeObject.reportAdClick({ adId: data.adId });
        }
    }
    videoCall: any = null;
    videoObject: any = null;
    playVideoAd(call: Function = function () { }) {
        let qg = window.qg;
        if (!this.videoObject) {
            this.videoObject = qg.createRewardedVideoAd({
                adUnitId: this.videoId,
                success: (code: any) => {
                    console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd: success");
                },
                fail: (data: any, code: any) => {
                    console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd fail: " + data + "," + code);
                },
                complete: () => {
                    console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd complete");
                }
            });
            this.videoObject.onLoad(() => {
                this.videoObject.show();
            });
            this.videoObject.onClose((res: any) => {
                this.videoCall(res.isEnded);
                console.log('ad onClose: ' + res.isEnded)
            })
        }
        this.videoCall = call;
        this.videoObject.load();
    }
}

给TA买糖
共{{data.count}}人
人已赞赏
程序代码

Cocos接入[穿山甲]+[友盟基础功能]指南

2021-7-2 9:45:29

程序代码计算机基础

Cocos Creator 打包vivo小游戏时,从后台切换回游戏,播放过的音效会重新播放的解决办法

2021-7-12 22:22:47

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索