版本要求
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();
}
}