CocosHandler.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-04-07 17:49:55
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-11 11:13:59
  6. * @Description:
  7. */
  8. // CocosHandler.ts
  9. // 封装对外模块调度(统一入口)
  10. import { native } from 'cc';
  11. import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
  12. export type CocosHandlerType = {
  13. method: string;
  14. param?: string;
  15. };
  16. export class CocosHandler {
  17. private static _inst: CocosHandler;
  18. public static get inst(): CocosHandler {
  19. if (!this._inst) {
  20. this._inst = new CocosHandler();
  21. }
  22. return this._inst;
  23. }
  24. public async sendMessageToAndroid(data: CocosHandlerType, logTag: string = '') {
  25. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  26. const result = await native.reflection.callStaticMethod(
  27. 'com/cocos/game/AtmobCocosCaller',
  28. 'call',
  29. '(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;',
  30. data.method,
  31. data.param
  32. );
  33. if (logTag) {
  34. console.log(`[${logTag}] Android 返回:`, result);
  35. }
  36. return result;
  37. }
  38. return null;
  39. }
  40. }