MenuButtonAdapter.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { _decorator, Component, Node, UITransform, Widget,screen, find, view } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('MenuButtonAdapter')
  4. export class MenuButtonAdapter extends Component {
  5. protected onLoad(): void {
  6. // 开始适配
  7. let res = chsdk.getMenuButtonBoundingClientRect();
  8. if (!res) {
  9. return
  10. }
  11. console.log(res)
  12. let wid = this.getComponent(Widget) || this.addComponent(Widget)
  13. let rect = this.getComponent(UITransform).getBoundingBoxToWorld()
  14. // 这个是使该节点在菜单下面
  15. let ratio = screen.devicePixelRatio||1;
  16. let scale = view.getScaleY();
  17. let factor = (scale / ratio)||1;
  18. let frameHeight = screen.windowSize.height / screen.devicePixelRatio
  19. let top = frameHeight - (rect.y + rect.height) * factor;
  20. let c = wid.top+top - (res.bottom)
  21. if(c<0){
  22. wid.top = wid.top+Math.abs(c/factor)
  23. }
  24. }
  25. update(deltaTime: number) {
  26. }
  27. }