BackgroundAdapter.ts 675 B

12345678910111213141516
  1. import { _decorator, Component, UITransform, screen, view, Size } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('BackgroundAdapter')
  4. export class BackgroundAdapter extends Component {
  5. onLoad() {
  6. let size = screen.windowSize;
  7. let nodeSize = this.node.getComponent(UITransform).contentSize;
  8. let srcScaleForShowAll = Math.min(size.width / nodeSize.width, size.height / nodeSize.height);
  9. let realWidth = nodeSize.width * srcScaleForShowAll;
  10. let realHeight = nodeSize.height * srcScaleForShowAll;
  11. let s = Math.max(size.width / realWidth, size.height / realHeight);
  12. this.node.setScale(s, s, s);
  13. }
  14. }