UI_Head_Icon.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { _decorator, Component, Node, Size, Sprite, SpriteFrame, UITransform, Vec2 } from 'cc';
  2. import { Hall } from '../../hall/Hall';
  3. import { ch } from 'db://assets/ch/ch';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('UI_Head_Icon')
  6. export class UI_Head_Icon extends Component {
  7. @property(Sprite)
  8. private head: Sprite;
  9. @property([SpriteFrame])
  10. private default_head: SpriteFrame[] = [];
  11. start() {
  12. }
  13. update(deltaTime: number) {
  14. }
  15. show(uid: string, head: string | null): void {
  16. const rc = this.default_head[ch.util.getRandomInt(0, this.default_head.length - 1)];
  17. Hall.getInstance().head_icon.showIcon(uid, head, this.head, rc).then((spf)=>{
  18. if(spf != null)
  19. this.adaptationIcon(this.head, spf, new Size(55,55));
  20. });
  21. }
  22. adaptationIcon(icon: Sprite, spriteFrameIcon: SpriteFrame, tSize: Size) {
  23. if (spriteFrameIcon) {
  24. let size = spriteFrameIcon.originalSize
  25. //适配图片
  26. let iconSize = new Size()
  27. let raito = size.width / size.height
  28. if (raito < 1) {
  29. iconSize.height = tSize.height
  30. iconSize.width = tSize.height * raito
  31. } else {
  32. iconSize.width = tSize.width
  33. iconSize.height = tSize.width / raito
  34. }
  35. icon.spriteFrame = spriteFrameIcon
  36. icon.getComponent(UITransform).setContentSize(iconSize)
  37. }
  38. }
  39. }