Example1.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import TextMeshPro from "./TextMeshPro";
  2. import TmpUtils from "./utils/TmpUtils";
  3. const { ccclass, property } = cc._decorator;
  4. @ccclass
  5. export default class Example1 extends cc.Component {
  6. @property(TextMeshPro)
  7. text1: TextMeshPro = null;
  8. @property(TextMeshPro)
  9. text2: TextMeshPro = null;
  10. @property(TextMeshPro)
  11. text3: TextMeshPro = null;
  12. protected start(): void {
  13. this.anim1();
  14. this.anim3();
  15. this.scheduleOnce(() => {
  16. for (let i = 0; i < this.text2.string.length; i++) {
  17. let result: cc.Vec2[] = this.text2.getPosVertices(i);
  18. this._initPos.push(result);
  19. }
  20. }, 0);
  21. }
  22. protected update(dt: number): void {
  23. this._time += dt;
  24. this.anim2();
  25. }
  26. private _time = 0;
  27. public _fScale: number = 1;
  28. public _xOffset: number = 0;
  29. private async anim1(): Promise<void> {
  30. await TmpUtils.waitCmpt(this, 1);
  31. this.text1.string = "这 是 一 段 测 试 文 字aaagghjsa;dzxmc;";
  32. this.text1.forceUpdateRenderData();
  33. this.text2.tmpUniform.outlineColor = cc.color(123, 223, 0);
  34. for (let i = 0; i < this.text1.string.length; i++) {
  35. this.text1.setVisible(i, false);
  36. }
  37. for (let i = 0; i < this.text1.string.length; i++) {
  38. this.text1.setVisible(i, true);
  39. if (!this.text1.isVisible(i)) {
  40. continue;
  41. }
  42. let result: cc.Vec2[] = this.text1.getPosVertices(i);
  43. let center = cc.v2();
  44. center.x = (result[0].x + result[1].x + result[2].x + result[3].x) / 4;
  45. center.y = (result[0].y + result[1].y + result[2].y + result[3].y) / 4;
  46. this._xOffset = -30;
  47. let updateCall = () => {
  48. let copy = [];
  49. copy.push(result[0].clone());
  50. copy.push(result[1].clone());
  51. copy.push(result[2].clone());
  52. copy.push(result[3].clone());
  53. for (let j = 0; j < 4; j++) {
  54. let delta: cc.Vec2 = copy[j].sub(center);
  55. delta.mulSelf(this._fScale).addSelf(cc.v2(this._xOffset, 0));
  56. copy[j] = center.add(delta);
  57. }
  58. this.text1.setPosVertices(i, copy as any);
  59. };
  60. cc.tween<Example1>(this)
  61. .to(0.05, { _fScale: 2, _xOffset: -15 }, { onUpdate: updateCall })
  62. .to(0.05, { _fScale: 1, _xOffset: 0 }, { onUpdate: updateCall })
  63. .start();
  64. await TmpUtils.waitCmpt(this, 0.1);
  65. }
  66. }
  67. private _initPos: cc.Vec2[][] = [];
  68. private anim2(): void {
  69. if (this._initPos.length <= 0) {
  70. return;
  71. }
  72. for (let i = 0; i < this.text2.string.length; i++) {
  73. if (!this.text2.isVisible(i)) {
  74. continue;
  75. }
  76. let result = [];
  77. for (let j = 0; j < 4; j++) {
  78. result.push(this._initPos[i][j].clone());
  79. result[j].y += Math.sin(0.5 * i + this._time * 5) * 10;
  80. }
  81. this.text2.setPosVertices(i, result as any);
  82. }
  83. }
  84. public alpha: number = 0;
  85. private async anim3(): Promise<void> {
  86. await TmpUtils.waitCmpt(this, 1);
  87. this.text3.string = "这 是 一 段 测 试 文 字aaagghjsa;dzxmc;";
  88. this.text3.forceUpdateRenderData();
  89. for (let i = 0; i < this.text3.string.length; i++) {
  90. this.text3.setVisible(i, false);
  91. }
  92. let time = 0.1;
  93. for (let i = 0; i < this.text3.string.length; i++) {
  94. this.text3.setVisible(i, true);
  95. if (!this.text3.isVisible(i)) {
  96. continue;
  97. }
  98. this.text3.setVisible(i, false);
  99. let result = this.text3.getColorExtraVertices(i);
  100. this.alpha = 0;
  101. cc.tween<Example1>(this)
  102. .to(time / 2, { alpha: 255 }, {
  103. onUpdate: () => {
  104. result[0].a = this.alpha;
  105. result[2].a = this.alpha;
  106. this.text3.setColorExtraVertices(i, result);
  107. }
  108. })
  109. .call(() => {
  110. this.alpha = 0;
  111. })
  112. .to(time / 2, { alpha: 255 }, {
  113. onUpdate: () => {
  114. result[1].a = this.alpha;
  115. result[3].a = this.alpha;
  116. this.text3.setColorExtraVertices(i, result);
  117. }
  118. })
  119. .start();
  120. await TmpUtils.waitCmpt(this, time);
  121. }
  122. }
  123. }