import { Vec3 } from 'cc'; import { Tween } from 'cc'; import { v3 } from 'cc'; import { tween } from 'cc'; import { _decorator, Component } from 'cc'; const { ccclass, property, executeInEditMode } = _decorator; @ccclass('TweenBreathe') @executeInEditMode export class TweenBreathe extends Component { @property speed: number = 1; @property minScale: Vec3 = v3(0.9, 0.9, 0.9); @property maxScale: Vec3 = v3(1.1, 1.1, 1.1); @property useOpacity = true; @property opacity: number = 255; @property minOpacity: number = 100; @property({tooltip:'几秒钟后暂停-1表示不暂停' }) timePause: number = -1; oScale: Vec3 = v3(1, 1, 1); oOPacity: number = 255; protected onLoad(): void { this.oScale = this.node.scale.clone(); this.oOPacity = this.node.opacity; } protected onEnable(): void { Tween.stopAllByTarget(this.node); this.node.setScale(this.oScale); this.play(); } protected onDisable(): void { Tween.stopAllByTarget(this.node); } start() { } update(deltaTime: number) { } play() { const ox = this.oScale.x; const oy = this.oScale.y; const oz = this.oScale.z; const maxTarget = v3(this.maxScale.x * ox, this.maxScale.y * oy, this.maxScale.z * oz); const minTarget = v3(this.minScale.x * ox, this.minScale.y * oy, this.minScale.z * oz); let t = 1 / this.speed / 2; tween(this.node) .to(t, { scale: maxTarget }) .to(t, { scale: minTarget }) .union() .repeatForever() .start() if (this.useOpacity) tween(this.node) .to(t, { opacity: this.opacity }) .to(t, { opacity: this.minOpacity }) .union() .repeatForever() .start() if (this.timePause > 0) { this.scheduleOnce(() => { this.stop(); }, this.timePause); } } stop() { Tween.stopAllByTarget(this.node); this.node.setScale(this.oScale); this.node.opacity = this.oOPacity; } }