BlinkScaleAnimator.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { _decorator, Component, Node, tween, Tween, Vec3 } from 'cc';
  2. import type { TweenEasing } from 'cc';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('BlinkScaleAnimator')
  5. export default class BlinkScaleAnimator extends Component {
  6. @property({ tooltip: '放大比例因子(相对于初始缩放)' })
  7. public scaleFactor: number = 1.3;
  8. @property({ tooltip: '放大阶段时长(秒)' })
  9. public upDuration: number = 0.15;
  10. @property({ tooltip: '缩小阶段时长(秒)' })
  11. public downDuration: number = 0.15;
  12. @property({ tooltip: '放大阶段缓动' })
  13. public easingUp: TweenEasing | ((k: number) => number) = 'sineOut';
  14. @property({ tooltip: '缩小阶段缓动' })
  15. public easingDown: TweenEasing | ((k: number) => number) = 'sineIn';
  16. @property({ tooltip: '启用组件时自动开始闪烁' })
  17. public playOnEnable: boolean = true;
  18. private _originalScale: Vec3 = Vec3.ONE.clone();
  19. private _blinkTween: Tween<Node> | null = null;
  20. onLoad() {
  21. this._originalScale = this.node.scale.clone();
  22. }
  23. onEnable() {
  24. if (this.playOnEnable) {
  25. this.play();
  26. }
  27. }
  28. onDisable() {
  29. this.stop();
  30. }
  31. onDestroy() {
  32. this.stop();
  33. }
  34. public play(): void {
  35. this.stop();
  36. const targetScale = new Vec3(
  37. this._originalScale.x * this.scaleFactor,
  38. this._originalScale.y * this.scaleFactor,
  39. this._originalScale.z
  40. );
  41. this._blinkTween = tween(this.node)
  42. .to(this.upDuration, { scale: targetScale }, { easing: this.easingUp })
  43. .to(this.downDuration, { scale: this._originalScale }, { easing: this.easingDown })
  44. .union()
  45. .repeatForever()
  46. .start();
  47. }
  48. public stop(): void {
  49. if (this._blinkTween) {
  50. this._blinkTween.stop();
  51. this._blinkTween = null;
  52. }
  53. Tween.stopAllByTarget(this.node);
  54. // 恢复初始缩放
  55. this.node.scale = this._originalScale.clone();
  56. }
  57. // 自动挂载并启动闪烁
  58. public static ensure(node: Node, options?: Partial<{
  59. scaleFactor: number,
  60. upDuration: number,
  61. downDuration: number,
  62. easingUp: TweenEasing | ((k: number) => number),
  63. easingDown: TweenEasing | ((k: number) => number),
  64. playOnEnable: boolean,
  65. }>): BlinkScaleAnimator {
  66. let comp = node.getComponent(BlinkScaleAnimator);
  67. if (!comp) {
  68. comp = node.addComponent(BlinkScaleAnimator);
  69. }
  70. if (options) {
  71. if (options.scaleFactor !== undefined) comp.scaleFactor = options.scaleFactor;
  72. if (options.upDuration !== undefined) comp.upDuration = options.upDuration;
  73. if (options.downDuration !== undefined) comp.downDuration = options.downDuration;
  74. if (options.easingUp !== undefined) comp.easingUp = options.easingUp;
  75. if (options.easingDown !== undefined) comp.easingDown = options.easingDown;
  76. if (options.playOnEnable !== undefined) comp.playOnEnable = options.playOnEnable;
  77. }
  78. comp.play();
  79. return comp;
  80. }
  81. }