DynamicAtlasViewer.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Label, Color, Component, _decorator, UITransform, SpriteFrame, Node, Sprite, builtinResMgr, dynamicAtlasManager, Widget } from 'cc';
  2. const { type, ccclass, requireComponent } = _decorator;
  3. const whitetexture = () => <any>builtinResMgr.get('white-texture');
  4. const assign = Object.assign;
  5. /**
  6. * 显示 dynamic atlas 和 Label atlas 内容
  7. * 点击右下文字(cur/total)可以切换当前查看的页面
  8. */
  9. @ccclass
  10. @requireComponent(UITransform)
  11. export class DynamicAtlasViewer extends Component {
  12. @type(Sprite) private sp: Sprite = null;
  13. @type(Label) private lb: Label = null;
  14. @type(Sprite) private bg: Sprite = null;
  15. private _i = 0;
  16. onLoad() {
  17. const { layer } = this.node;
  18. const texture = whitetexture();
  19. if (!this.bg) {
  20. const bg = assign(new Node, { layer });
  21. uniMargins(assign(bg.addComponent(Widget), { alignMode: Widget.AlignMode.ALWAYS }));
  22. this.bg = assign(bg.addComponent(Sprite), { sizeMode: Sprite.SizeMode.CUSTOM, color: new Color('#AAAAAAFF'), spriteFrame: assign(new SpriteFrame, { texture, packable: false }) });
  23. this.node.addChild(bg);
  24. }
  25. if (!this.sp) {
  26. const sp = assign(new Node, { layer });
  27. uniMargins(assign(sp.addComponent(Widget), { alignMode: Widget.AlignMode.ALWAYS }));
  28. this.sp = assign(sp.addComponent(Sprite), { sizeMode: Sprite.SizeMode.CUSTOM, spriteFrame: assign(new SpriteFrame, { texture, packable: false }) });
  29. this.node.addChild(sp);
  30. }
  31. if (!this.lb) {
  32. const lb = assign(new Node, { layer });
  33. assign(lb.addComponent(Widget), { isAlignBottom: true, isAlignRight: true, alignMode: Widget.AlignMode.ALWAYS });
  34. this.lb = assign(lb.addComponent(Label), { color: Color.RED, fontSize: 30, string: '0/0', cacheMode: Label.CacheMode.CHAR });
  35. this.node.addChild(lb);
  36. }
  37. }
  38. onEnable() {
  39. this.sp.spriteFrame.texture = whitetexture();
  40. this._i = 0;
  41. this.lb.node.on(Node.EventType.TOUCH_END, this._touchEnd, this);
  42. }
  43. onDisable() {
  44. this.sp.spriteFrame.texture = whitetexture();
  45. this.lb.node.off(Node.EventType.TOUCH_END, this._touchEnd, this);
  46. }
  47. update() {
  48. const { sp, lb, _i } = this;
  49. const atlases = dynamicAtlasManager['_atlases'];
  50. const tex = _i < atlases.length ? atlases[_i]?._texture : lb['_texture'];
  51. if (tex !== sp.spriteFrame.texture) {
  52. sp.spriteFrame.texture = tex ?? whitetexture();
  53. //需要通知 assembler 更新 texture 信息
  54. sp.setTextureDirty();
  55. sp.markForUpdateRenderData();
  56. }
  57. lb.string = (_i < atlases.length) ? (dynamicAtlasManager.atlasCount > 0 ? `${_i + 1}/${dynamicAtlasManager.atlasCount + 1}` : "disabled") : 'CHARS';
  58. }
  59. private _touchEnd() {
  60. this._i = (this._i + 1) % (dynamicAtlasManager.atlasCount + 1);
  61. }
  62. }
  63. export function uniMargins(widget: Widget, margin = 0): Widget {
  64. widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true;
  65. widget.left = widget.right = widget.top = widget.bottom = margin;
  66. return widget;
  67. }