SafeAreaAdapter.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { _decorator, Component, Node, sys, screen, view, Widget } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. declare var wx: any;
  4. declare var tt: any;
  5. @ccclass('SafeAreaAdapter')
  6. export class SafeAreaAdapter extends Component {
  7. protected onLoad(): void {
  8. // 开始适配
  9. // let res = this.getWindowInfo();
  10. // if (!res) {
  11. // return
  12. // }
  13. // if (!res.safeArea) {
  14. // return
  15. // }
  16. // console.log(res)
  17. let widget = this.getComponent(Widget) || this.addComponent(Widget)
  18. // // 这个是使该节点在菜单下面
  19. // let scale = view.getScaleY();
  20. // let ratio = screen.devicePixelRatio || 1;
  21. // let factor = (scale / ratio) || 1;
  22. // let len = Math.abs(res.safeArea.top)
  23. widget.top =Math.max(getSafeLen(),widget.top)
  24. }
  25. // getWindowInfo() {
  26. // if (sys.platform === sys.Platform.WECHAT_GAME) {
  27. // return wx.getWindowInfo()
  28. // } else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME) {
  29. // return tt.getSystemInfoSync()
  30. // } else {
  31. // }
  32. // }
  33. }
  34. export function getSafeLen():number{
  35. let getWindowInfo =() =>{
  36. if (sys.platform === sys.Platform.WECHAT_GAME) {
  37. return wx.getWindowInfo()
  38. } else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME) {
  39. return tt.getSystemInfoSync()
  40. } else {
  41. }
  42. }
  43. let res = getWindowInfo();
  44. if (!res) {
  45. return 0
  46. }
  47. if (!res.safeArea) {
  48. return 0
  49. }
  50. console.log(res)
  51. // 这个是使该节点在菜单下面
  52. let scale = view.getScaleY();
  53. let ratio = screen.devicePixelRatio || 1;
  54. let factor = (scale / ratio) || 1;
  55. let len = Math.abs(res.safeArea.top)
  56. return len / factor
  57. }