BitmapFontC.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const { ccclass, property } = cc._decorator;
  2. /**
  3. * 位图文本
  4. * @author 袁浩
  5. */
  6. @ccclass
  7. export class BitmapFontC extends cc.Component {
  8. @property({ tooltip: "字符集配置", type: [cc.SpriteFrame], serializable: true })
  9. public dataList: cc.SpriteFrame[] = [];
  10. @property({ visible: false })
  11. private _string: string = '';
  12. @property({ tooltip: "文本内容字符串", serializable: true })
  13. public get string(): string {
  14. return this._string;
  15. }
  16. public set string(value: string) {
  17. this._string = value + "";
  18. for (let i = 0; i < this.string.length; i++) {
  19. let child = this.node.children[i];
  20. const chair = this.string[i];
  21. let data = this.getData(chair);
  22. if (data) {
  23. if (child) {
  24. child.active = true;
  25. child.getComponent(cc.Sprite).spriteFrame = data;
  26. }
  27. else {
  28. let node = new cc.Node(chair)
  29. let sprite = node.addComponent(cc.Sprite);
  30. sprite.spriteFrame = data;
  31. node.parent = this.node;
  32. }
  33. }
  34. else {
  35. console.error(`BitmapFont中缺少字符 ${chair} 的配置。`)
  36. }
  37. }
  38. for (let i = this.string.length; i < this.node.children.length; i++) {
  39. const child = this.node.children[i];
  40. child.active = false;
  41. }
  42. }
  43. private getData(chair: string) {
  44. for (let j = 0; j < this.dataList.length; j++) {
  45. const data = this.dataList[j];
  46. if (data.name == chair) {
  47. return data;
  48. }
  49. }
  50. return null;
  51. }
  52. start() {
  53. }
  54. }