BgAdaptor.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /** @format */
  2. import {Mgr} from '../GameControl'
  3. import {EVENT} from '../enums/Enum'
  4. import {ConstValue} from '../data/ConstValue'
  5. import property = cc._decorator.property
  6. const {ccclass, disallowMultiple, menu} = cc._decorator
  7. @ccclass
  8. @disallowMultiple()
  9. @menu('自定义组件/BgAdaptor')
  10. export default class BgAdaptor extends cc.Component {
  11. @property({
  12. displayName: '宽度自适应',
  13. })
  14. widthFit: boolean = true
  15. @property({
  16. displayName: '高度自适应',
  17. })
  18. heightFit: boolean = true
  19. protected onLoad() {
  20. this.changeSize()
  21. }
  22. public onEnable() {
  23. Mgr.event.add(EVENT.canvasResize, this, this.changeSize)
  24. }
  25. public onDisable() {
  26. Mgr.event.removeAll(this)
  27. }
  28. changeSize() {
  29. // 只拉伸,适配黑边
  30. if (ConstValue.REAL_WIDTH > this.node.width && this.widthFit) this.node.width = ConstValue.REAL_WIDTH
  31. if (ConstValue.REAL_HEIGHT > this.node.height && this.heightFit) this.node.height = ConstValue.REAL_HEIGHT
  32. }
  33. }