Adaptation.ts 993 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { _decorator, Component, Node, Size, UITransform } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('Adaptation')
  4. export class Adaptation extends Component {
  5. @property(UITransform)
  6. targetNode:UITransform
  7. start() {
  8. let mySize = this.getComponent(UITransform).contentSize
  9. let tSize = this.targetNode.contentSize
  10. this.adaptationIcon(mySize,tSize)
  11. }
  12. protected onEnable(): void {
  13. }
  14. adaptationIcon(mySize: Size, tSize: Size) {
  15. let size = mySize
  16. //适配图片
  17. let iconSize = new Size()
  18. let raito = size.width / size.height
  19. let raito1 = tSize.width / tSize.height
  20. if (raito < raito1) {
  21. iconSize.height = tSize.height
  22. iconSize.width = tSize.height * raito
  23. } else {
  24. iconSize.width = tSize.width
  25. iconSize.height = tSize.width / raito
  26. }
  27. this.getComponent(UITransform).setContentSize(iconSize)
  28. }
  29. }