super-hero/assets/cc-common/cc-util/UmAdsManager.ts

111 lines
3.3 KiB
TypeScript

import { _decorator, Component, Node } from 'cc';
import { GameDefine } from '../../cc-game/scripts/config/GameDefine';
import { UmLog } from './UmLog';
import { UmNativeBridge } from './UmNativeBridge';
import { UmClientEvent } from './UmOneToMultiListener';
import { UmDeviceInfo } from './UmDeviceInfo';
const { ccclass, property } = _decorator;
@ccclass('UmAdsManager')
export class UmAdsManager extends Component {
public static Instance: UmAdsManager = null;
interstitialAdCallback: any = null;
rewardAdCallback: any = null;
//Event
EVENT_BANNER_AD = "EVENT_BANNER_AD";
EVENT_SHOW_INTERSTITIAL_AD = "EVENT_SHOW_INTERSTITIAL_AD";
EVENT_SHOW_REWARD_AD = "EVENT_SHOW_REWARD_AD";
EVENT_BANNER_AD_POSITION = "EVENT_BANNER_AD_POSITION";
//Action
SHOW_ACTION_KEY = "show";
HIDE_ACTION_KEY = "hide";
//Banner Position
public static BANNER_POSITION = {
"BANNER_BOTTOM_LEFT": "BOTTOM_LEFT",
"BANNER_BOTTOM_CENTER": "BOTTOM_CENTER",
"BANNER_BOTTOM_RIGHT": "BOTTOM_RIGHT",
"BANNER_TOP_LEFT":"TOP_LEFT",
"BANNER_TOP_CENTER": "TOP_CENTER",
"BANNER_TOP_RIGHT": "TOP_RIGHT",
}
public static ADS_RESULT = {
"SUCCESS": "success",
"FAIL": "fail",
}
onLoad(): void {
UmAdsManager.Instance = this;
UmClientEvent.on(this.EVENT_SHOW_INTERSTITIAL_AD, this.onInterstitialAdCallback.bind(this));
UmClientEvent.on(this.EVENT_SHOW_REWARD_AD, this.onRewardCallback.bind(this));
}
onDestroy(): void {
UmClientEvent.off(this.EVENT_SHOW_INTERSTITIAL_AD, this.onInterstitialAdCallback.bind(this));
UmClientEvent.off(this.EVENT_SHOW_REWARD_AD, this.onRewardCallback.bind(this));
}
//BANNER_ADS
public showBanner()
{
if (!UmDeviceInfo.isMobile) return;
UmNativeBridge.instance.onCallNative(this.EVENT_BANNER_AD, this.SHOW_ACTION_KEY);
}
public hideBanner()
{
if (!UmDeviceInfo.isMobile) return;
UmNativeBridge.instance.onCallNative(this.EVENT_BANNER_AD, this.HIDE_ACTION_KEY);
}
public setBannerPosition(position: string)
{
if (!UmDeviceInfo.isMobile) return;
UmNativeBridge.instance.onCallNative(this.EVENT_BANNER_AD_POSITION, position);
}
//INTERSTITIAL_ADS
public showInterstitialAd(callback: any)
{
this.interstitialAdCallback = callback;
if (!UmDeviceInfo.isMobile)
{
this.onInterstitialAdCallback("success");
return;
}
UmNativeBridge.instance.onCallNative(this.EVENT_SHOW_INTERSTITIAL_AD);
}
public onInterstitialAdCallback(params: string)
{
UmLog.log("onInterstitialAdCallback => ", params);
let callback = this.interstitialAdCallback;
callback?.(params);
this.interstitialAdCallback = null;
}
//REWARD_ADS
public showRewardAd(callback: any)
{
this.rewardAdCallback = callback;
if (!UmDeviceInfo.isMobile) {
this.onRewardCallback("success");
return;
}
UmNativeBridge.instance.onCallNative(this.EVENT_SHOW_REWARD_AD);
}
public onRewardCallback(params: string) {
UmLog.log("onRewardCallback => ", params);
let callback = this.rewardAdCallback;
callback?.(params);
this.rewardAdCallback = null;
}
}