@cc-component/cc-ex-component 1.2.6 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/assets/core/ReferenceComponent.ts +24 -17
  2. package/assets/core/ViewModel.ts +50 -6
  3. package/package.json +1 -1
  4. package/assets/platform/Interface.ts +0 -80
  5. package/assets/platform/Interface.ts.meta +0 -9
  6. package/assets/platform/android/AndroidModule.ts +0 -12
  7. package/assets/platform/android/AndroidModule.ts.meta +0 -9
  8. package/assets/platform/android/AndroidSDK.ts +0 -186
  9. package/assets/platform/android/AndroidSDK.ts.meta +0 -1
  10. package/assets/platform/android/AnyThinkAds/ATBanner.ts +0 -266
  11. package/assets/platform/android/AnyThinkAds/ATBanner.ts.meta +0 -9
  12. package/assets/platform/android/AnyThinkAds/ATInterstitial.ts +0 -185
  13. package/assets/platform/android/AnyThinkAds/ATInterstitial.ts.meta +0 -9
  14. package/assets/platform/android/AnyThinkAds/ATInterstitialAuto.ts +0 -180
  15. package/assets/platform/android/AnyThinkAds/ATInterstitialAuto.ts.meta +0 -9
  16. package/assets/platform/android/AnyThinkAds/ATNative.ts +0 -256
  17. package/assets/platform/android/AnyThinkAds/ATNative.ts.meta +0 -9
  18. package/assets/platform/android/AnyThinkAds/ATRewardedAutoVideo.ts +0 -175
  19. package/assets/platform/android/AnyThinkAds/ATRewardedAutoVideo.ts.meta +0 -9
  20. package/assets/platform/android/AnyThinkAds/ATRewardedVideo.ts +0 -243
  21. package/assets/platform/android/AnyThinkAds/ATRewardedVideo.ts.meta +0 -9
  22. package/assets/platform/android/AnyThinkAds/ATSDK.ts +0 -222
  23. package/assets/platform/android/AnyThinkAds/ATSDK.ts.meta +0 -9
  24. package/assets/platform/android/AnyThinkAds/ATSplash.ts +0 -155
  25. package/assets/platform/android/AnyThinkAds/ATSplash.ts.meta +0 -9
  26. package/assets/platform/android/AnyThinkAds/Android/ATAndroidBannerTS.ts +0 -71
  27. package/assets/platform/android/AnyThinkAds/Android/ATAndroidBannerTS.ts.meta +0 -9
  28. package/assets/platform/android/AnyThinkAds/Android/ATAndroidInterstitialAutoTS.ts +0 -52
  29. package/assets/platform/android/AnyThinkAds/Android/ATAndroidInterstitialAutoTS.ts.meta +0 -9
  30. package/assets/platform/android/AnyThinkAds/Android/ATAndroidInterstitialTS.ts +0 -41
  31. package/assets/platform/android/AnyThinkAds/Android/ATAndroidInterstitialTS.ts.meta +0 -9
  32. package/assets/platform/android/AnyThinkAds/Android/ATAndroidNativeTS.ts +0 -47
  33. package/assets/platform/android/AnyThinkAds/Android/ATAndroidNativeTS.ts.meta +0 -9
  34. package/assets/platform/android/AnyThinkAds/Android/ATAndroidRewardedVideoAutoTS.ts +0 -52
  35. package/assets/platform/android/AnyThinkAds/Android/ATAndroidRewardedVideoAutoTS.ts.meta +0 -9
  36. package/assets/platform/android/AnyThinkAds/Android/ATAndroidRewardedVideoTS.ts +0 -41
  37. package/assets/platform/android/AnyThinkAds/Android/ATAndroidRewardedVideoTS.ts.meta +0 -9
  38. package/assets/platform/android/AnyThinkAds/Android/ATAndroidSplashTS.ts +0 -42
  39. package/assets/platform/android/AnyThinkAds/Android/ATAndroidSplashTS.ts.meta +0 -9
  40. package/assets/platform/android/AnyThinkAds/Android/ATAndroidTS.ts +0 -73
  41. package/assets/platform/android/AnyThinkAds/Android/ATAndroidTS.ts.meta +0 -9
  42. package/assets/platform/android/AnyThinkAds/Android.meta +0 -9
  43. package/assets/platform/android/AnyThinkAds/iOS/ATiOSBannerTS.ts +0 -66
  44. package/assets/platform/android/AnyThinkAds/iOS/ATiOSBannerTS.ts.meta +0 -9
  45. package/assets/platform/android/AnyThinkAds/iOS/ATiOSInterstitiaAutolTS.ts +0 -54
  46. package/assets/platform/android/AnyThinkAds/iOS/ATiOSInterstitiaAutolTS.ts.meta +0 -9
  47. package/assets/platform/android/AnyThinkAds/iOS/ATiOSInterstitialTS.ts +0 -43
  48. package/assets/platform/android/AnyThinkAds/iOS/ATiOSInterstitialTS.ts.meta +0 -9
  49. package/assets/platform/android/AnyThinkAds/iOS/ATiOSNativeTS.ts +0 -47
  50. package/assets/platform/android/AnyThinkAds/iOS/ATiOSNativeTS.ts.meta +0 -9
  51. package/assets/platform/android/AnyThinkAds/iOS/ATiOSRewardedAutoVideoTS.ts +0 -55
  52. package/assets/platform/android/AnyThinkAds/iOS/ATiOSRewardedAutoVideoTS.ts.meta +0 -9
  53. package/assets/platform/android/AnyThinkAds/iOS/ATiOSRewardedVideoTS.ts +0 -43
  54. package/assets/platform/android/AnyThinkAds/iOS/ATiOSRewardedVideoTS.ts.meta +0 -9
  55. package/assets/platform/android/AnyThinkAds/iOS/ATiOSSplashTS.ts +0 -47
  56. package/assets/platform/android/AnyThinkAds/iOS/ATiOSSplashTS.ts.meta +0 -9
  57. package/assets/platform/android/AnyThinkAds/iOS/ATiOSTS.ts +0 -76
  58. package/assets/platform/android/AnyThinkAds/iOS/ATiOSTS.ts.meta +0 -9
  59. package/assets/platform/android/AnyThinkAds/iOS.meta +0 -9
  60. package/assets/platform/android/AnyThinkAds.meta +0 -9
  61. package/assets/platform/android/iOSSDK.ts +0 -14
  62. package/assets/platform/android/iOSSDK.ts.meta +0 -1
  63. package/assets/platform/android.meta +0 -11
  64. package/assets/platform/base/EditorSDK.ts +0 -30
  65. package/assets/platform/base/EditorSDK.ts.meta +0 -1
  66. package/assets/platform/base/PlatfprmModule.ts +0 -152
  67. package/assets/platform/base/PlatfprmModule.ts.meta +0 -9
  68. package/assets/platform/base/SDKBase.ts +0 -59
  69. package/assets/platform/base/SDKBase.ts.meta +0 -9
  70. package/assets/platform/base/SDKEnum.ts +0 -127
  71. package/assets/platform/base/SDKEnum.ts.meta +0 -9
  72. package/assets/platform/base/TTSDK.ts +0 -366
  73. package/assets/platform/base/TTSDK.ts.meta +0 -9
  74. package/assets/platform/base/WXSDK.ts +0 -149
  75. package/assets/platform/base/WXSDK.ts.meta +0 -1
  76. package/assets/platform/base.meta +0 -9
  77. package/assets/platform/wx/MiniSDK.ts +0 -1039
  78. package/assets/platform/wx/MiniSDK.ts.meta +0 -9
  79. package/assets/platform/wx/lib.douyin.d.ts +0 -168
  80. package/assets/platform/wx/lib.douyin.d.ts.meta +0 -9
  81. package/assets/platform/wx/wxmini.d.ts +0 -2452
  82. package/assets/platform/wx/wxmini.d.ts.meta +0 -9
  83. package/assets/platform/wx.meta +0 -9
  84. package/assets/platform.meta +0 -9
@@ -1,1039 +0,0 @@
1
- import { EventTarget, Game, Node, Rect, Sprite, Texture2D, UITransform, game, rect, screen, sys, view } from 'cc';
2
- import { DEV } from 'cc/env';
3
- import { AdsCall } from '../base/SDKEnum';
4
- type StringCallback = (str?: string) => void;
5
-
6
-
7
- export interface IInit {
8
- // 视频广告ID
9
- videoID?: string,
10
- // Banner广告ID
11
- bannerID?: string,
12
- // 插屏广告ID
13
- interstitialID?: string,
14
-
15
- // 支付ID
16
- payID?: string,
17
- // 支付环境 [0:正式环境 1:测试环境]
18
- payEnv?: 0 | 1,
19
- // 支付环境 [0:正式环境 1:测试环境]
20
- payZone?: 0 | 1,
21
- }
22
-
23
- export interface IShare {
24
- title?: string,
25
- imageUrl?: string,
26
- query?: string
27
- }
28
-
29
- export type IOpenMenuShare = {
30
- title?: string,
31
- imageUrl?: string,
32
- query?: string
33
- } | (() => { title?: string, imageUrl?: string, query?: string });
34
-
35
- export interface IShowRewardedVideoAd {
36
- onShow?: (...any: any[]) => void,
37
- onError?: (...any: any[]) => void,
38
- onClose?: (success: boolean) => void
39
- }
40
-
41
- export interface ILoadRewardedVideoAd {
42
- onLoad?: (...any: any[]) => void,
43
- onError?: (...any: any[]) => void,
44
- }
45
-
46
- export interface INavigateToMiniProgram {
47
- appId: string,
48
- path?: string,
49
- extraData?: {},
50
- envVersion?: string,
51
- success?: (res?: any) => void, fail?: (res?: any) => void, complete?: (res?: any) => void
52
- }
53
-
54
- export interface ICreateUserInfoButton {
55
- type?: 'text' | 'image',
56
- text?: string,
57
- image?: string,
58
- style: {
59
- left: number,
60
- top: number,
61
- width: number,
62
- height: number,
63
- backgroundColor: string,
64
- borderColor: string,
65
- borderWidth: number,
66
- borderRadius: number,
67
- textAlign: 'left' | 'center' | 'right',
68
- fontSize: number,
69
- lineHeight: number
70
- } | Node,
71
- //是否带上登录态信息。当 withCredentials 为 true 时,要求此前有调用过 wx.login 且登录态尚未过期,此时返回的数据会包含 encryptedData, iv 等敏感信息;当 withCredentials 为 false 时,不要求有登录态,返回的数据不包含 encryptedData, iv 等敏感信息。
72
- withCredentials?: boolean,
73
- lang?: 'en' | 'zh_CN' | 'zh_TW'
74
- }
75
-
76
- export interface IShowBannerAd {
77
- node?: Node,
78
- rect?: Rect,
79
- anchor?: { x: number, y: number },
80
- collision?: Function
81
- }
82
-
83
- // 启动参数
84
- let launchOptions: wx.launchOption = null;
85
- if (window.wx && !wx.getEnterOptionsSync) wx.onShow(function (options: any) {
86
- launchOptions = options;
87
- });
88
-
89
- // 事件
90
- const SDKEvent = new EventTarget();
91
-
92
- export class MiniSDK {
93
- private static miniSDK: MiniSDK = null;
94
- static get inst() {
95
- if (!this.miniSDK) this.miniSDK = new MiniSDK();
96
- return this.miniSDK;
97
- }
98
-
99
- // 视频广告ID
100
- private videoID = '';
101
- // Banner广告ID
102
- private bannerID = '';
103
- // 插屏广告ID
104
- private interstitialID = '';
105
- // 支付ID
106
- private payID = '';
107
- // 支付环境 [0:正式环境 1:测试环境]
108
- private payEnv = 1;
109
- // 支付环境 [0:正式环境 1:测试环境]
110
- private payZone = 1;
111
-
112
- /**************************** 初始化 ****************************/
113
- constructor(params?: IInit) {
114
- if (params) this.init(params);
115
- }
116
-
117
- /**
118
- * 初始化
119
- */
120
- init(params: IInit = {}) {
121
- if (params.videoID) this.videoID = params.videoID;
122
- if (params.bannerID) this.bannerID = params.bannerID;
123
- if (params.interstitialID) this.interstitialID = params.interstitialID;
124
- if (params.payID) this.payID = params.payID;
125
- if (params.payEnv) this.payEnv = params.payEnv;
126
- if (params.payZone) this.payZone = params.payZone;
127
-
128
- return this;
129
- }
130
-
131
- /**************************** 环境判断 ****************************/
132
- public isIOS() {
133
- return sys.os === sys.OS.IOS;
134
- }
135
-
136
- public isAndroid() {
137
- return sys.os === sys.OS.ANDROID;
138
- }
139
-
140
- public isMobile() {
141
- return sys.isMobile;
142
- }
143
-
144
- public isNative() {
145
- return sys.isNative;
146
- }
147
-
148
- public isBrowser() {
149
- return sys.isBrowser && !!window['document'] && !!window['HTMLElement'];
150
- }
151
-
152
- public isMiniGame() {
153
- return !this.isBrowser() && (game.renderType === Game.RENDER_TYPE_CANVAS || game.renderType === Game.RENDER_TYPE_WEBGL);
154
- }
155
-
156
- /**
157
- * 是否是微信游戏
158
- * @returns
159
- */
160
- public isWechatMiniGame() {
161
- return sys.platform === sys.Platform.WECHAT_GAME;
162
- }
163
-
164
- /**
165
- * 是否是字节游戏
166
- * @returns
167
- */
168
- public isByteMiniGame() {
169
- return sys.platform === sys.Platform.BYTEDANCE_MINI_GAME;
170
- }
171
-
172
- /**************************** 事件监听 ****************************/
173
- private emit(key: string, ...args: any[]) {
174
- return SDKEvent.emit(key, ...args);
175
- }
176
- on(key: string, cb: (...any: any[]) => void, target: any) {
177
- return SDKEvent.on(key, cb, target);
178
- }
179
-
180
- once(key: string, cb: (...any: any[]) => void, target?: any) {
181
- return SDKEvent.once(key, cb as any, target);
182
- }
183
-
184
- off(key: string, cb?: (...any: any[]) => void, target?: any) {
185
- return SDKEvent.off(key, cb, target);
186
- }
187
-
188
- targetOff(target: any) {
189
- return SDKEvent.targetOff(target);
190
- }
191
-
192
- log(str: string, ...ags: any[]) {
193
- console.log(`[GameSDK] [log] ${str}`, ...ags);
194
- }
195
-
196
- error(str: string, ...ags: any[]) {
197
- console.log(`[GameSDK] [error] ${str}`, ...ags);
198
- }
199
-
200
- /**************************** 获取打开参数 ****************************/
201
- private getEnterOptionsSync(): wx.launchOption {
202
- if (!window.wx) return null;
203
-
204
- let options: wx.launchOption = null;
205
- if (wx.getEnterOptionsSync) {
206
- options = wx.getEnterOptionsSync() || null;
207
- } else if (wx.getLaunchOptionsSync) {
208
- options = wx.getLaunchOptionsSync() || null;
209
- }
210
-
211
- return options || launchOptions;
212
- }
213
-
214
- /**
215
- * 获取打开参数
216
- */
217
- public getQuery(): { [key in string]: any } {
218
- const options = this.getEnterOptionsSync();
219
- if (!options) return {};
220
-
221
- // https://developers.weixin.qq.com/minigame/dev/api/base/app/life-cycle/wx.getEnterOptionsSync.html
222
- if (options?.query && typeof options?.query === 'object') return options?.query;
223
- if (options?.referrerInfo?.extraData && typeof options?.referrerInfo?.extraData === 'object') return options?.referrerInfo?.extraData;
224
- return {};
225
- }
226
-
227
- /**************************** 分享 ****************************/
228
- /**
229
- * 分享
230
- */
231
- share(params: IShare = {}) {
232
- if (window.wx && wx.shareAppMessage) wx.shareAppMessage(params);
233
- }
234
-
235
- /**************************** 菜单分享 ****************************/
236
- private _onShareAppMessageData = null;
237
- // 更新菜单
238
- private _updateShareMenu() {
239
- wx.updateShareMenu({
240
- withShareTicket: true,
241
- success: function () { },
242
- fail: () => {
243
- setTimeout(() => this._updateShareMenu(), 1000);
244
- },
245
- });
246
- }
247
- // 开启菜单分享
248
- private _openShareMenu() {
249
- wx.showShareMenu({
250
- withShareTicket: true,
251
- menus: ['shareAppMessage', 'shareTimeline'],
252
- success: function () { },
253
- fail: () => {
254
- setTimeout(() => this._openShareMenu(), 1000);
255
- },
256
- });
257
- }
258
- // 监听系统分享
259
- private _onShareAppMessage() {
260
- wx.onShareAppMessage(() => {
261
- let shareData = null;
262
- if (typeof this._onShareAppMessageData === 'function') {
263
- shareData = this._onShareAppMessageData();
264
- } else {
265
- shareData = this._onShareAppMessageData;
266
- }
267
- if (shareData) {
268
- return {
269
- title: shareData.title,
270
- imageUrl: shareData.imageUrl,
271
- query: shareData.query
272
- };
273
- } else {
274
- return {};
275
- }
276
- });
277
- if (wx['onShareTimeline']) wx['onShareTimeline'](() => {
278
- let shareData = null;
279
- if (typeof this._onShareAppMessageData === 'function') {
280
- shareData = this._onShareAppMessageData();
281
- } else {
282
- shareData = this._onShareAppMessageData;
283
- }
284
- if (shareData) {
285
- return {
286
- title: shareData.title,
287
- imageUrl: shareData.imageUrl,
288
- query: shareData.query
289
- };
290
- } else {
291
- return {};
292
- }
293
- });
294
- }
295
- /**
296
- * 开启菜单分享
297
- */
298
- openMenuShare(params: IOpenMenuShare) {
299
- if (window.wx && wx.updateShareMenu && wx.showShareMenu && wx.onShareAppMessage && !this._onShareAppMessageData) {
300
- this._onShareAppMessageData = params;
301
- this._updateShareMenu();
302
- this._openShareMenu();
303
- this._onShareAppMessage();
304
- } else {
305
- this._onShareAppMessageData = params;
306
- }
307
- }
308
-
309
- /**************************** 视频广告 ****************************/
310
- // 视频广告单例
311
- private _rewardedVideoAd: wx.RewardedVideoAd = null;
312
- // 视频关闭回调
313
- private _rewardedVideoAdOnClose: Function = null;
314
- // 0未加载 1加载中 2加载完成
315
- private _rewardedVideoAdLoadState: 0 | 1 | 2 = 0;
316
- // 播放中
317
- private _rewardedVideoAdPlaying = false;
318
- /**
319
- * 判断激励视频广告组件在当前客户端版本中是否有效
320
- */
321
- checkRewardedVideoAd() {
322
- return !!window.wx && !!wx.createRewardedVideoAd;
323
- }
324
-
325
- /**
326
- * 激励视频广告组件,组建默认是关闭的
327
- */
328
- private _initRewardedVideoAd() {
329
- if (!this.videoID) return;
330
- if (this._rewardedVideoAd) return;
331
- if (!this.checkRewardedVideoAd()) return;
332
-
333
- this._rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: this.videoID });
334
-
335
- // 监听激励视频广告关闭事件
336
- this._rewardedVideoAd.onClose(res => {
337
- const rewardedVideoAdOnClose = this._rewardedVideoAdOnClose;
338
- // 重置关闭回调
339
- this._rewardedVideoAdOnClose = null;
340
- // 重置播放状态
341
- this._rewardedVideoAdPlaying = false;
342
- // 重新显示Banner广告
343
- this._showBannerAd();
344
-
345
- // 小于 2.1.0 的基础库版本,res 是一个 undefined
346
- if (res && res.isEnded || res === undefined) {
347
- // 正常播放结束,可以下发游戏奖励
348
- rewardedVideoAdOnClose && rewardedVideoAdOnClose(true);
349
- }
350
- else {
351
- // 播放中途退出,不下发游戏奖励
352
- rewardedVideoAdOnClose && rewardedVideoAdOnClose(false);
353
- }
354
- this.log('rewardedVideoAd onClose', res);
355
- });
356
-
357
- this._rewardedVideoAd.onError((res) => {
358
- console.error("res", res)
359
- // 出错了
360
- this._rewardedVideoAdLoadState = 0;
361
- });
362
- }
363
- /**
364
- * 加载激励视频
365
- */
366
- loadRewardedVideoAd({ onLoad, onError }: ILoadRewardedVideoAd = {}) {
367
- if (DEV) {
368
- return onLoad && onLoad();
369
- }
370
-
371
- this._initRewardedVideoAd();
372
- // 出错了,拿不到视频广告实例
373
- if (!this._rewardedVideoAd) return onError && onError();
374
- // 已经加载完毕了,直接返回
375
- if (this._rewardedVideoAdLoadState === 2) return onLoad && onLoad();
376
-
377
- // 未开始加载或已经在加载中了,先监听事件
378
- if (onLoad) this.once('RewardedVideoAdLoad', onLoad);
379
- if (onError) this.once('RewardedVideoAdError', onError);
380
-
381
- // 判断一下状态,确定是否需要去执行加载动作
382
- if (this._rewardedVideoAdLoadState !== 0) return;
383
- this._rewardedVideoAdLoadState = 1;
384
-
385
- // 执行加载动作
386
- this._rewardedVideoAd.load()
387
- .then(() => {
388
- this._rewardedVideoAdLoadState = 2;
389
- this.emit('RewardedVideoAdLoad');
390
- this.log('loadRewardedVideoAd', 'success');
391
- })
392
- .catch((res) => {
393
- this._rewardedVideoAdLoadState = 0;
394
- this.emit('RewardedVideoAdError');
395
- this.log('loadRewardedVideoAd', 'error', res);
396
- });
397
- }
398
-
399
- /**
400
- * 展示激励视频
401
- */
402
- showRewardedVideoAd({ onShow, onError, onClose }: IShowRewardedVideoAd = {}) {
403
- if (this._rewardedVideoAdPlaying) return;
404
- this._rewardedVideoAdPlaying = true;
405
-
406
- if (DEV) {
407
- setTimeout(() => {
408
- if (onShow) onShow();
409
- if (onClose) onClose(true);
410
- this._rewardedVideoAdPlaying = false;
411
- }, 100);
412
- return;
413
- }
414
-
415
- this.loadRewardedVideoAd({
416
- onLoad: () => {
417
- // 播放并绑定关闭回调
418
- this._rewardedVideoAdOnClose = onClose;
419
- this._rewardedVideoAd.show().then(() => {
420
- // 隐藏Banner广告
421
- this._hideBannerAd();
422
- this._rewardedVideoAdLoadState = 0;
423
- onShow && onShow();
424
- }).catch(() => {
425
- this._rewardedVideoAdPlaying = false;
426
- this._rewardedVideoAdLoadState = 0;
427
- onError && onError();
428
- });
429
- },
430
- onError: () => {
431
- this._rewardedVideoAdPlaying = false;
432
- onError && onError();
433
- }
434
- });
435
- }
436
-
437
-
438
-
439
- private _onRewardedAdClose: (res) => void;
440
- private _onRewardedAdError: (error) => void;
441
- /** 展示激励视频广告 */
442
- public showRewardedAd(call: AdsCall) {
443
- if (!this._onRewardedAdClose) {
444
- this._onRewardedAdClose = (res) => {
445
- if (res.isEnded) {
446
- call.success?.();
447
- } else {
448
- call.fail?.('未完整观看视频');
449
- }
450
- call.close?.(res);
451
- }
452
- this._onRewardedAdError = (err) => {
453
- call.error?.(err);
454
- }
455
- }
456
-
457
- let video = wx.createRewardedVideoAd({
458
- adUnitId: this.videoID
459
- });
460
- video.offClose(this._onRewardedAdClose);
461
- video.offError(this._onRewardedAdError);
462
- video.onClose(this._onRewardedAdClose);
463
- video.onError(this._onRewardedAdError);
464
- video.load().then(() => {
465
- video.show().then(() => {
466
- call.show?.()
467
- });
468
- });
469
- }
470
-
471
- /**************************** banner广告 ****************************/
472
- private _bannerAd = null;
473
- // banner是否加载完成
474
- private _bannerAdLoad = false;
475
- private _bannerAdError = false;
476
- // banner的实际宽高
477
- private _bannerAdSize = { width: 0, height: 0 };
478
- // banner是否应该显示
479
- private _bannerAdShow = false;
480
- // banner是否实际在在显示中
481
- private _bannerAdShowing = false;
482
- // banner的定位信息
483
- private _bannerAdNodeRect: Rect = new Rect(0, 0, 0, 0);
484
- // banner的锚点信息
485
- private _bannerAdNodeAnchor = { x: 0.5, y: 0.5 };
486
- // 碰撞回调
487
- private _bannerCollision = null;
488
-
489
- /**
490
- * 判断banner视频广告组件在当前客户端版本中是否有效
491
- */
492
- checkBannerAd() {
493
- return !!window.wx && !!wx.createBannerAd;
494
- }
495
-
496
- /**
497
- * banner派发的事件名称:
498
- * bannerCreate [私有] [创建banner资源阶段的事件,拉取资源成功或失败都会触发,参数为true代表加载成功,false代表加载失败]
499
- * bannerResize [私有] [banner的大小改变时触发]
500
- * bannerLoad [拉取banner资源成功时触发]
501
- * bannerShow [播放banner成功时会触发]
502
- * bannerError [拉取banner资源失败、播放banner资源失败时都会触发]
503
- * bannerDestroy [销毁banner时触发]
504
- */
505
-
506
- /**
507
- * 创建banner视频
508
- */
509
- private _createBannerAd({ onLoad, onError, onResize }: { onLoad?: Function, onError?: Function, onResize?: Function } = {}) {
510
- if (this.checkBannerAd() && this.bannerID) {
511
- if (this._bannerAd) {
512
- this.log('bannerCache');
513
- const banner = this._bannerAd;
514
- // 监听banner创建完成
515
- if (this._bannerAdLoad) {
516
- onLoad && onLoad();
517
- } else if (this._bannerAdError) {
518
- onError && onError();
519
- } else {
520
- this.once('bannerCreate', function (res) {
521
- if (banner !== this._bannerAd) return;
522
- res ? onLoad && onLoad() : onError && onError();
523
- }, this);
524
- }
525
- // 监听banner大小改变
526
- if (!this._bannerAdError) {
527
- if (this._bannerAdSize.width || this._bannerAdSize.height) {
528
- onResize && onResize(this._bannerAdSize);
529
- } else {
530
- this.once('bannerResize', function (res) {
531
- if (banner !== this._bannerAd) return;
532
- onResize && onResize(res);
533
- }, this);
534
- }
535
- }
536
- return;
537
- }
538
-
539
- this.log('bannerCreate');
540
-
541
- const width = screen.windowSize.width;
542
- // 创建banner视频组件
543
- const banner = this._bannerAd = wx.createBannerAd({
544
- adUnitId: this.bannerID,
545
- style: {
546
- left: 0,
547
- top: 2000,
548
- width: width,
549
- height: Math.floor(width * 9 / 16)
550
- }
551
- });
552
-
553
- // 加载完成
554
- this._bannerAdLoad = false;
555
- this._bannerAd.onLoad((res) => {
556
- this.log('bannerLoad', res);
557
- if (this._bannerAd === banner) {
558
- this._bannerAdLoad = true;
559
- this.emit('bannerCreate', true);
560
- onLoad && onLoad();
561
- }
562
- });
563
- // 加载失败
564
- this._bannerAdError = false;
565
- this._bannerAd.onError && this._bannerAd.onError((res) => {
566
- this.log('bannerError', res);
567
- if (this._bannerAd === banner) {
568
- this._bannerAdError = true;
569
- this.emit('bannerCreate', false);
570
- onError && onError();
571
- }
572
- });
573
- // 大小变化
574
- this._bannerAd.onResize((res) => {
575
- this.log('bannerResize', res);
576
- if (this._bannerAd === banner) {
577
- const width = res.width;
578
- const height = res.height || Math.floor(width * 9 / 16);
579
-
580
- // 记录banner大小
581
- this._bannerAdSize.width = width;
582
- this._bannerAdSize.height = height;
583
-
584
- this.emit('bannerResize', this._bannerAdSize);
585
- onResize && onResize(this._bannerAdSize);
586
- }
587
- });
588
- } else {
589
- this.error('无法创建banner');
590
- onError && onError();
591
- }
592
- }
593
-
594
- /**
595
- * 销毁banner视频
596
- */
597
- private _destroyBannerAd() {
598
- this.log('bannerDestroy');
599
- const bannerAd = this._bannerAd;
600
-
601
- this._bannerAd = null;
602
- this._bannerAdShow = false;
603
- this._bannerAdShowing = false;
604
- this._bannerAdLoad = false;
605
- this._bannerAdError = false;
606
- this._bannerAdSize = { width: 0, height: 0 };
607
- this._bannerCollision = null;
608
- this.emit('bannerDestroy');
609
- this.off('bannerCreate');
610
- this.off('bannerResize');
611
-
612
- if (bannerAd) {
613
- bannerAd.hide();
614
- bannerAd.destroy();
615
- }
616
- }
617
-
618
- /**
619
- * 根据节点设置banner的位置
620
- */
621
- setBannerAdPos(node?: Node) {
622
- this.log('bannerSetPos');
623
- if (this._bannerAd && this._bannerAdSize.width && this._bannerAdSize.height) {
624
- let btnRect = null;
625
- let anchorX = 0.5;
626
- let anchorY = 0.5;
627
- if (node && node instanceof Node) {
628
- btnRect = node.getComponent(UITransform).getBoundingBoxToWorld();
629
- anchorX = node.getComponent(UITransform).anchorX;
630
- anchorY = node.getComponent(UITransform).anchorY;
631
- } else {
632
- btnRect = this._bannerAdNodeRect;
633
- anchorX = this._bannerAdNodeAnchor.x;
634
- anchorY = this._bannerAdNodeAnchor.y;
635
- }
636
-
637
- // 屏幕分辨率
638
- let frameSize = screen.windowSize;
639
- // 游戏实际分辨率
640
- let winSize = view.getVisibleSize();
641
-
642
- // node位置,游戏分辨率下
643
- let nodeX = btnRect.xMin + anchorX * btnRect.width;
644
- let nodeY = btnRect.yMin + anchorY * btnRect.height;
645
-
646
- // 将node位置由游戏分辨率转成屏幕分辨率下
647
- let x = nodeX / winSize.width * frameSize.width;
648
- let y = (winSize.height - nodeY) / winSize.height * frameSize.height;
649
-
650
- // banner大小,屏幕分辨率下
651
- let bannerWidth = this._bannerAdSize.width || this._bannerAd.style.realWidth || this._bannerAd.style.width;
652
- let bannerHeight = this._bannerAdSize.height || this._bannerAd.style.realHeight || this._bannerAd.style.height || (bannerWidth * 9 / 16);
653
-
654
- // 计算最终结果前,进行碰撞测试,如果返回true(banner会和游戏UI产生碰撞),则不改变banner位置(banner仍处于屏幕外,达到不显示的效果)
655
- if (this._bannerCollision) {
656
- // 将banner大小由屏幕分辨率转成游戏分辨率下
657
- let width = bannerWidth / frameSize.width * winSize.width;
658
- let height = bannerHeight / frameSize.height * winSize.height;
659
-
660
- if (this._bannerCollision(rect(nodeX, nodeY, width, height))) {
661
- this._bannerAd.style.left = 0;
662
- this._bannerAd.style.top = 2000;
663
- return;
664
- }
665
- }
666
-
667
- let left = x - bannerWidth * anchorX;
668
- let top = y - bannerHeight * (1 - anchorY);
669
-
670
- this._bannerAd.style.left = left;
671
- this._bannerAd.style.top = top;
672
- }
673
- }
674
-
675
- /**
676
- * 展示banner视频
677
- * @param {Node} node 位置节点
678
- * @param {Rect} rect 包围盒
679
- * @param {{x: number, y: number}} anchor 使用围盒时有效,设置锚点
680
- * @param {Function} collision 碰撞测试,返回true不展示banner
681
- */
682
- private showBannerAd_({ node, rect, anchor, collision }: IShowBannerAd = {}) {
683
- this.log('bannerShow');
684
-
685
- this._bannerCollision = collision || null;
686
-
687
- // 记录banner位置信息和锚点信息
688
- if (node && node instanceof Node) {
689
- this._bannerAdNodeRect = node.getComponent(UITransform).getBoundingBoxToWorld();
690
- this._bannerAdNodeAnchor.x = node.getComponent(UITransform).anchorX;
691
- this._bannerAdNodeAnchor.y = node.getComponent(UITransform).anchorY;
692
- } else if (rect && node instanceof Rect) {
693
- this._bannerAdNodeRect = rect;
694
- this._bannerAdNodeAnchor.x = anchor ? anchor.x : 0.5;
695
- this._bannerAdNodeAnchor.y = anchor ? anchor.y : 0.5;
696
- }
697
-
698
- if (this._bannerAdShow && !this._bannerAdShowing) {
699
- return;
700
- }
701
- // 标记banner显示
702
- this._bannerAdShow = true;
703
-
704
- // 创建banner
705
- this._createBannerAd({
706
- onLoad: () => {
707
- // 发射bannerLoad事件
708
- this.emit('bannerLoad');
709
- this.log('bannerOnLoad');
710
- if (this._bannerAdShow) {
711
- if (this._bannerAdShowing) {
712
- setTimeout(() => {
713
- this.log('banner缓存显示成功');
714
- this.emit('bannerShow');
715
- }, 1);
716
- } else {
717
- this._bannerAd.show().then(() => {
718
- this._bannerAdShowing = true;
719
- this.log('banner广告显示成功');
720
- this.emit('bannerShow');
721
- }).catch(err => {
722
- this.log('banner广告显示失败', err);
723
- this.emit('bannerError', 'show');
724
- });
725
- }
726
- }
727
- },
728
- onError: () => {
729
- this.emit('bannerError', 'load');
730
- this.log('bannerOnError');
731
- },
732
- onResize: () => {
733
- this.log('bannerOnResize');
734
- this.setBannerAdPos();
735
- }
736
- });
737
- }
738
-
739
- private showBannerAdTimeOut = null;
740
- /**
741
- * 展示banner视频
742
- * @param {Node} node 位置节点
743
- * @param {Rect} rect 包围盒
744
- * @param {{x: number, y: number}} anchor 使用围盒时有效,设置锚点
745
- * @param {Function} collision 碰撞测试,返回true不展示banner
746
- */
747
- showBannerAd(params: IShowBannerAd = {}) {
748
- // 为了防止传入node时,有一些组件会在这一帧改变它的位置
749
- if (this.showBannerAdTimeOut) return;
750
- this.showBannerAdTimeOut = setTimeout(() => {
751
- this.showBannerAdTimeOut = null;
752
- this.showBannerAd_(params);
753
- }, 100);
754
- }
755
-
756
- /**
757
- * 隐藏banner视频
758
- */
759
- hideBannerAd() {
760
- if (this.showBannerAdTimeOut) {
761
- clearTimeout(this.showBannerAdTimeOut);
762
- this.showBannerAdTimeOut = null;
763
- }
764
-
765
- this.log('bannerHide');
766
- if (this._bannerAd && this._bannerAdShow) {
767
- this._destroyBannerAd();
768
- // 销毁banner时都预先创建一个备用,以便加快下次的显示速度
769
- this._createBannerAd({
770
- onLoad: () => {
771
- if (!this._bannerAdShow) this._bannerAd.show().then(() => {
772
- this._bannerAdShowing = true;
773
- });
774
- },
775
- onError: () => {
776
- if (!this._bannerAdShow) this._destroyBannerAd();
777
- }
778
- });
779
- }
780
- this._bannerAdShow = false;
781
- }
782
-
783
- /**
784
- * 隐藏Banner广告
785
- */
786
- private _hideBannerAd() {
787
- if (this._bannerAd && this._bannerAdShow) {
788
- this._bannerAd.hide();
789
- }
790
- this._bannerAdShowing = false;
791
- }
792
-
793
- /**
794
- * 重新显示Banner广告
795
- */
796
- private _showBannerAd() {
797
- if (this._bannerAd && this._bannerAdShow && !this._bannerAdShowing) {
798
- this._bannerAd.show();
799
- }
800
- this._bannerAdShowing = true;
801
- }
802
-
803
- /**************************** 插屏广告 ****************************/
804
- private _interstitialAd = null;
805
-
806
- /**
807
- * 判断放插屏广告组件在当前客户端版本中是否有效
808
- */
809
- checkInterstitialAd() {
810
- return !!window.wx && !!wx.createInterstitialAd;
811
- }
812
- /**
813
- * 检查能否播放插屏广告
814
- */
815
- canShowInterstitialAd() {
816
- return this.checkInterstitialAd();
817
- }
818
-
819
- /**
820
- * 展示插屏广告
821
- */
822
- showInterstitialAd() {
823
- if (this.checkInterstitialAd()) {
824
- if (this.isByteMiniGame()) {
825
- this._interstitialAd && this._interstitialAd.destroy();
826
- const interstitialAd = this._interstitialAd = window['tt'].createInterstitialAd({
827
- adUnitId: this.interstitialID
828
- });
829
-
830
- interstitialAd && interstitialAd.load()
831
- .then(() => {
832
- interstitialAd.show()
833
- .then(() => {
834
- this.emit('interstitialShow');
835
- this.log('插屏广告显示成功');
836
- })
837
- .catch((err) => {
838
- this.emit('interstitialError');
839
- this.log('插屏广告显示失败', err);
840
- });
841
- })
842
- .catch((err) => {
843
- this.emit('interstitialError');
844
- this.log('插屏广告显示失败', err);
845
- });
846
- } else {
847
- if (!this._interstitialAd) {
848
- this._interstitialAd = wx.createInterstitialAd({
849
- adUnitId: this.interstitialID
850
- });
851
- }
852
-
853
- this._interstitialAd && this._interstitialAd.show()
854
- .then(() => {
855
- this.emit('interstitialShow');
856
- this.log('插屏广告显示成功');
857
- })
858
- .catch((err) => {
859
- this.emit('interstitialError');
860
- this.log('插屏广告显示失败', err);
861
- });
862
- }
863
- }
864
- }
865
-
866
- /**************************** 工具方法 ****************************/
867
- /**
868
- * 将子域纹理传给参数texture
869
- */
870
- getSharedTexture(texture: Texture2D) {
871
- if (window['sharedCanvas'] && texture) {
872
- texture.uploadData(window['sharedCanvas']);
873
- // 2.4.x
874
- // texture.initWithElement(window['sharedCanvas']);
875
- // texture.handleLoadedTexture();
876
- }
877
- return texture;
878
- }
879
-
880
- /**
881
- * 轻微震动
882
- */
883
- vibrateShort(data = {}) {
884
- if (window.wx && wx.vibrateShort) {
885
- wx.vibrateShort(data);
886
- } else {
887
- this.log('vibrateShort', data);
888
- }
889
- }
890
-
891
- /**
892
- * 长震动
893
- */
894
- vibrateLong(data = {}) {
895
- if (window.wx && wx.vibrateLong) {
896
- wx.vibrateLong(data);
897
- } else {
898
- this.log('vibrateLong', data);
899
- }
900
- }
901
-
902
- /**
903
- * 跳转到其它小程序
904
- */
905
- navigateToMiniProgram(data: INavigateToMiniProgram) {
906
- if (window.wx && wx.navigateToMiniProgram) {
907
- wx.navigateToMiniProgram(data);
908
- } else {
909
- this.log('navigateToMiniProgram', data);
910
- }
911
- }
912
-
913
- /**
914
- * 获得游戏内节点对应屏幕坐标系下的位置的大小
915
- */
916
- toPosition(node: Node) {
917
- let btnRect = node.getComponent(UITransform).getBoundingBoxToWorld();
918
- // 屏幕尺寸
919
- let frameSize = screen.windowSize;
920
- // 实际分辨率
921
- let winSize = view.getVisibleSize();
922
-
923
- let x = btnRect.xMin / winSize.width * frameSize.width;
924
- let y = (winSize.height - btnRect.yMax) / winSize.height * frameSize.height;
925
- let width = btnRect.width / winSize.width * frameSize.width;
926
- let height = btnRect.height / winSize.height * frameSize.height;
927
-
928
- return { x, y, width, height };
929
- }
930
-
931
- /**
932
- * 创建用户信息按钮
933
- */
934
- createUserInfoButton(obj: ICreateUserInfoButton, callback: Function) {
935
- if (!window.wx || !wx.createUserInfoButton) {
936
- this.log('createUserInfoButton');
937
- return null;
938
- }
939
-
940
- let style = null;
941
- let image = null;
942
-
943
- if (obj.style) {
944
- style = obj.style;
945
- image = obj.image;
946
- } else {
947
- style = obj;
948
- }
949
-
950
- if (style && style instanceof Node) {
951
- if (!image) {
952
- image = style.getComponent(Sprite).spriteFrame.texture.nativeUrl;
953
- }
954
- let { x, y, width, height } = this.toPosition(style);
955
- style = {
956
- left: x,
957
- top: y,
958
- width: width,
959
- height: height,
960
- lineHeight: height,
961
- backgroundColor: '',
962
- color: '#ffffff',
963
- textAlign: 'center',
964
- fontSize: 16,
965
- borderRadius: 4
966
- };
967
- }
968
-
969
- const btn = wx.createUserInfoButton({
970
- type: 'image',
971
- image: image,
972
- style: style,
973
- withCredentials: obj.withCredentials === undefined ? true : obj.withCredentials,//默认true
974
- });
975
-
976
- btn && btn.onTap((uInfo) => {
977
- callback(uInfo);
978
- });
979
- return btn;
980
- }
981
- /** 创建游戏圈按钮。游戏圈按钮被点击后会跳转到小游戏的游戏圈。 */
982
- public createGameClubButton(node: Node) {
983
- const pos = this.toPosition(node)
984
- return this._createGameClubButton({ left: pos.x, top: pos.y, width: pos.width, height: pos.height });
985
- }
986
-
987
- /** 创建游戏圈按钮。游戏圈按钮被点击后会跳转到小游戏的游戏圈。 */
988
- _createGameClubButton(size?: { left?: number, right?: number, bottom?: number, top?: number, width?: number, height?: number }) {
989
- const systemInfo = wx.getSystemInfoSync();
990
- const top = systemInfo.windowHeight - size.top;
991
- let button = wx.createGameClubButton({
992
- type: "text",
993
- text: "前往游戏圈",
994
- //@ts-ignore
995
- style: {
996
- left: size.left,
997
- top: top,
998
- width: size.width,
999
- height: size.height,
1000
- fontSize: 16,
1001
- textAlign: "center",
1002
- color: "#ffffff00",
1003
- lineHeight: size.height,
1004
- }
1005
- })
1006
-
1007
- return button;
1008
- }
1009
- /**
1010
- * 自动更新游戏
1011
- */
1012
- autoUpdateGame() {
1013
- if (window.wx && typeof wx.getUpdateManager === 'function') {
1014
- const updateManager = wx.getUpdateManager();
1015
-
1016
- updateManager.onCheckForUpdate((res) => {
1017
- // 请求完新版本信息的回调
1018
- this.log('onCheckForUpdate', res.hasUpdate);
1019
- });
1020
-
1021
- updateManager.onUpdateReady(() => {
1022
- // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
1023
- updateManager.applyUpdate();
1024
- this.log('onUpdateReady');
1025
- });
1026
-
1027
- updateManager.onUpdateFailed(() => {
1028
- // 新的版本下载失败
1029
- this.log('onUpdateFailed');
1030
- });
1031
- }
1032
- }
1033
-
1034
-
1035
-
1036
-
1037
- }
1038
-
1039
- export const miniSDK = MiniSDK.inst;