目录— 个文件
⌕
目录加载中…
…/module/ani/AutoBreath.ts
1import { _decorator, Component, Node, tween, Tween, Vec3 } from 'cc';
2
3const { ccclass, property } = _decorator;
4
5/**
6 * 挂在节点上,让节点做循环「呼吸」缩放动画(适合按钮、图标、奖励入口)
7 * - 以启用时的原始缩放为基准
8 * - 先放大再缩回,支持首帧延迟与循环间隔
9 */
10@ccclass('AutoBreath')
11export class AutoBreath extends Component {
12
13 @property({
14 tooltip: '相对原始缩放的放大倍率(1=不变化,1.08=放大8%)',
15 min: 1.0,
16 })
17 public maxScaleMultiplier: number = 1.08;
18
19 @property({
20 tooltip: '放大阶段时长(秒)',
21 min: 0.01,
22 })
23 public scaleUpDuration: number = 0.5;
24
25 @property({
26 tooltip: '缩回阶段时长(秒)',
27 min: 0.01,
28 })
29 public scaleDownDuration: number = 0.5;
30
31 @property({
32 tooltip: '每轮呼吸完成后的间隔时长(秒)',
33 min: 0.0,
34 })
35 public interval: number = 0.0;
36
37 @property({
38 tooltip: '组件启用后首次开始呼吸前延迟(秒)',
39 min: 0.0,
40 })
41 public startDelay: number = 0.0;
42
43 private _originScale: Vec3 = new Vec3();
44 private _breathTween: Tween<Node> | null = null;
45
46 onEnable() {
47 if (!this.node) {
48 return;
49 }
50 this._originScale = this.node.scale.clone();
51 this.StartBreathTween();
52 }
53
54 onDisable() {
55 this._breathTween?.stop();
56 this._breathTween = null;
57
58 if (this.node && this.node.isValid) {
59 this.node.setScale(this._originScale);
60 }
61 }
62
63 /** 开始循环呼吸缩放 */
64 private StartBreathTween(): void {
65 if (!this.node || !this.node.isValid) {
66 return;
67 }
68
69 this._breathTween?.stop();
70 this._breathTween = null;
71
72 const node = this.node;
73 const base = this._originScale.clone();
74 const target = new Vec3(
75 base.x * this.maxScaleMultiplier,
76 base.y * this.maxScaleMultiplier,
77 base.z * this.maxScaleMultiplier,
78 );
79 const upDuration = Math.max(this.scaleUpDuration, 0.01);
80 const downDuration = Math.max(this.scaleDownDuration, 0.01);
81 const gap = Math.max(this.interval, 0);
82 const delayAtStart = Math.max(this.startDelay, 0);
83
84 // 平滑呼吸:首轮前仅延迟一次;每轮使用缓动曲线,避免硬切与“机械感”
85 const oneCycle = tween(node)
86 .to(upDuration, { scale: target }, { easing: 'sineInOut' })
87 .to(downDuration, { scale: base }, { easing: 'sineInOut' })
88 .delay(gap);
89
90 this._breathTween = tween(node)
91 .call(() => {
92 node.setScale(base);
93 })
94 .delay(delayAtStart)
95 .repeatForever(oneCycle);
96
97 this._breathTween.start();
98 }
99}
100
讲解101 行
呼吸缩放人工通读
AutoBreath.ts
组件每次启用时复制当前缩放作为基准,交给 Cocos tween 循环放大再缩回;首轮延迟只执行一次,每轮末尾另有间隔。动画句柄与基准缩放保存在实例,禁用会停掉自身动画并恢复基准,不会停止节点上的其他 tween。
依次看 onEnable 的基准采样、StartBreathTween 的循环构造和 onDisable 恢复。
特殊处理与易错点
- 时序每次重新启用都会重新采样缩放,运行中外部改缩放不会改变本轮基准。
- 销毁禁用停止保存的 tween 并恢复原缩放。
10注释挂在节点上,让节点做循环「呼吸」缩放动画(适合按钮、图标、奖励入口) - 以启用时的原始缩放为基准 - 先放大再缩回,支持首帧延迟与循环间隔
11注释AutoBreath:挂在节点上,让节点做循环「呼吸」缩放动画(适合按钮、图标、奖励入口) - 以启用时的原始缩放
17属性最大 Scale Multiplier(number):该模块的运行态字段。
23属性scale Up Duration(number):该模块的运行态字段。
29属性scale Down Duration(number):该模块的运行态字段。
35属性interval(number):该模块的运行态字段。
41属性开始 Delay(number):该模块的运行态字段。
43属性本次启用时复制的缩放基准,也是禁用恢复值。
44属性只保存本组件创建的循环缩放 tween。
46方法采样启用时的节点缩放并启动循环呼吸动画。
54方法停止自身呼吸动画并恢复启用时保存的缩放。
64方法重建含首轮延迟与轮间间隔的往返缩放动画。
85注释平滑呼吸:首轮前仅延迟一次;每轮使用缓动曲线,避免硬切与“机械感”