BlinkScaleAnimator.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 = false; // 默认不自动播放,避免误触发
  18. private _originalScale: Vec3 = Vec3.ONE.clone();
  19. private _blinkTween: Tween<Node> | null = null;
  20. private _isPlaying: boolean = false;
  21. onLoad() {
  22. this._originalScale = this.node.scale.clone();
  23. }
  24. onEnable() {
  25. if (this.playOnEnable) {
  26. this.play();
  27. }
  28. }
  29. onDisable() {
  30. this.stop();
  31. }
  32. onDestroy() {
  33. this.stop();
  34. }
  35. public play(): void {
  36. if (this._isPlaying) {
  37. return;
  38. }
  39. this.stop();
  40. this._isPlaying = true;
  41. const targetScale = new Vec3(
  42. this._originalScale.x * this.scaleFactor,
  43. this._originalScale.y * this.scaleFactor,
  44. this._originalScale.z
  45. );
  46. this._blinkTween = tween(this.node)
  47. .to(this.upDuration, { scale: targetScale }, { easing: this.easingUp })
  48. .to(this.downDuration, { scale: this._originalScale }, { easing: this.easingDown })
  49. .union()
  50. .repeatForever()
  51. .start();
  52. }
  53. public stop(): void {
  54. this._isPlaying = false;
  55. if (this._blinkTween) {
  56. this._blinkTween.stop();
  57. this._blinkTween = null;
  58. }
  59. Tween.stopAllByTarget(this.node);
  60. // 恢复初始缩放
  61. this.node.scale = this._originalScale.clone();
  62. }
  63. }