123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- import { _decorator, SpriteFrame, sys } from "cc";
- import { gameManager } from "./run/gameManager";
- //管理广告、分享、SDK相关内容的组件
- export class SdkUtil {
- public static platform: string = 'cocos'; //平台
- public static imgAd: SpriteFrame = null!;
- public static imgShare: SpriteFrame = null!;
- public static isDebugMode: boolean = false;
- public static onlineInterval: number = -1;
- public static isEnableVibrate: boolean = true;
- public static isCheckOffline: boolean = false; //登录后会检查是否展示登录界面,而且只检查一次
- public static isWatchVideoAd: boolean = false;//是否正在播放广告
- public static isEnableMoving: boolean = false;//是否允许屏幕上下移动
- public static isEnableZoom: boolean = false;//是否允许屏幕缩放
- public static arrLockDiary = [];//未解锁日记
- public static vibrateInterval: number = 100;//两次震动之间的间隔,AppActivity里面的震动间隔也是100
- public static vibratePreTime: number = 0;//上次震动时间
- public static videoAd:any =null;
- public static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
- public static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
- private static ttGameRecorder:any = null; //抖音游戏录制
- private static ttRecordVideoPath:string = ''; //抖音录制视频路径
- private static ttTotalRecord:number = 300; //抖音总录制时间
- private static ttIsRecording:boolean = false; //抖音是否录制中
- /**
- * 自定义事件统计
- *
- * @param {string} eventType
- * @param {object} objParams
- */
- public static customEventStatistics(eventType: string, objParams?: any) {
- eventType = eventType.toString();
- if (!objParams) {
- objParams = {};
- }
- // console.log({'eventType': eventType},{'objParams': objParams});
- if (this.platform === 'wx') {
- //@ts-ignore
- if (window['wx'] && window['wx']['aldSendEvent']) {
- //@ts-ignore
- window.wx['aldSendEvent'](eventType, objParams);
- }
- }
- //@ts-ignore
- if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
- console.log("###统计", eventType, objParams);
- //@ts-ignore
- window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
- }
- }
- /**
- * 微信分享
- *
- * @static
- * @param {string} title
- * @param {string} imageUrl
- * @returns
- * @memberof SdkUtil
- */
- public static shareGame(title: string, imageUrl: string) {
- //@ts-ignore
- if (!window.wx) {
- return;
- }
- //@ts-ignore
- wx.showShareMenu({
- withShareTicket: true,
- complete: () => {
- }
- });
- //@ts-ignore
- if (wx.aldOnShareAppMessage) {
- //@ts-ignore
- wx.aldOnShareAppMessage(function () {
- // 用户点击了“转发”按钮
- return {
- title: title,
- imageUrl: imageUrl,
- };
- });
- } else {
- //@ts-ignore
- wx.onShareAppMessage(function () {
- // 用户点击了“转发”按钮
- return {
- title: title,
- imageUrl: imageUrl,
- };
- });
- }
- }
-
- /**
- * 抖音/微信激励视频
- *
- * @static
- * @param {string} _adUnitId
- * @param {Function} call_back
- * @returns
- * @memberof SdkUtil
- */
- public static showVideoAd(_adUnitId: string, call_back) {
- if(gameManager.isFreeAds()) {
- call_back({"isEnded":true})
- return
- }
- if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME||sys.platform==sys.Platform.WECHAT_GAME){
- gameManager.Singleton.showLoadingLevel()
- if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
- SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
- } else if(sys.platform==sys.Platform.WECHAT_GAME) {
- gameManager.Singleton.hideLoadingLevel()
- call_back({"isEnded":true})
- return
- // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
- }
- if(SdkUtil.videoAd==null){
- return
- }
- SdkUtil.videoAd.onLoad(() => {
- SdkUtil.videoAd.show();
- console.log("广告加载完成");
- });
- SdkUtil.videoAd.onClose((res) => {
- call_back(res)
- SdkUtil.videoAd.destroy()
- gameManager.Singleton.hideLoadingLevel()
- });
- SdkUtil.videoAd.onError((res) => {
- let errorString = res.errCode + '-' + res.errMsg
- call_back({isEnded:false,errorString:errorString})
- SdkUtil.videoAd.destroy()
- gameManager.Singleton.hideLoadingLevel()
- });
- SdkUtil.videoAd.load()
- }
- }
- /**
- * 抖音侧边栏
- *
- * @static
- * @memberof SdkUtil
- */
- public static ttRegisterSidebar() {
- if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
- tt.onShow((res) => {
- console.log('tt.onShow =', res)
- // console.log('res=',res)
- if(res.scene == '021036' || res.scene == '101036') {
- SdkUtil.tt_isToEnterFromSidebar = true
- }
- if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
- SdkUtil.tt_isToEnterFromSidebar = true
- }
- });
-
- tt.checkScene({
- scene: "sidebar",
- success: (res) => {
- console.log("check scene success: ", res.isExist);
- if(res.isExist != undefined || res.isExist != null) {
- SdkUtil.tt_isSupportSidebar = res.isExist
- }
- },
- fail: (res) => {
- console.log("check scene fail:", res);
- }
- });
- let options = tt.getLaunchOptionsSync()
- console.log('getLaunchOptionsSync=', options)
- if(options.scene == '021036' || options.scene == '101036') {
- SdkUtil.tt_isToEnterFromSidebar = true
- }
- }
- }
- // 抖音检测是否显示奖励
- public static ttCheckSceneShowRewards():boolean {
- return SdkUtil.tt_isSupportSidebar
- }
- // 抖音检测是否从侧边栏进入
- public static ttCheckToEnterFromSidebar():boolean {
- return SdkUtil.tt_isToEnterFromSidebar
- }
- // 抖音导航到侧边栏场景
- public static ttNavToSidebarScene() {
- if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
- tt.navigateToScene({
- scene: "sidebar",
- success: (res) => {
- // console.log("navigate to scene success");
- },
- fail: (res) => {
- // console.log("navigate to scene fail: ", res);
- },
- });
- }
- }
- // 抖音开启屏幕录制
- public static ttStartScreenRecording() {
- return
- if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
- return
- }
- if(this.ttIsScreenRecording()==true) {
- this.ttStopScreenRecording()
- }
-
- this.ttIsRecording = true
- if(!this.ttGameRecorder) {
- this.ttGameRecorder = tt.getGameRecorderManager()
- }
- this.ttGameRecorder.start({duration: this.ttTotalRecord})
- this.ttGameRecorder.onStart(()=> {
- console.log('GameRecorder onStart onStart onStart')
- })
- this.ttGameRecorder.onStop((res)=> {
- console.log('GameRecorder onStop onStop onStop=',res)
- this.ttIsRecording = false
- this.ttRecordVideoPath = res.videoPath
- SdkUtil.ttShareScreenRecordVideo('测试测试',res.videoPath)
- })
- this.ttGameRecorder.onError((e)=> {
- console.log('ttGameRecord error:',e)
- })
- }
- // 抖音关闭屏幕录制
- public static ttStopScreenRecording() {
- return
- if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
- return
- }
- if(this.ttGameRecorder == null) {
- return
- }
-
- this.ttGameRecorder.stop()
- }
- // 抖音是否屏幕录制
- public static ttIsScreenRecording():boolean {
- return this.ttIsRecording;
- }
- // 抖音获取屏幕录制视频文件
- public static ttGetScreenRecordingVideoPath():string {
- return this.ttRecordVideoPath;
- }
- // 抖音分享屏幕录制视频
- public static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
- tt.shareAppMessage({
- title: title,
- channel: "video",
- extra: {
- videoTopics: [],
- hashtag_list: [],
- videoPath: videoPath,
- withVideoId: true,
- },
- success: (res) => {
- console.log('抖音分享屏幕录制视频,成功=',res)
- onSuccess && onSuccess();
- },
- fail: (e) => {
- console.log('抖音分享屏幕录制视频,失败=',e)
- // if(this.checkAppName()){
- // onSuccess && onSuccess();
- // }else{
- // onFail && onFail();
- // }
- // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
- onFail && onFail()
- }
- })
- }
- }
|