Files
Max-Cocos-Demo/assets/scripts/HotupdateLoading.ts
2025-10-28 21:55:41 +08:00

80 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { _decorator, Component, Label, ProgressBar } from "cc";
const { ccclass, property } = _decorator;
@ccclass("HotupdateLoading")
export class HotupdateLoading extends Component {
@property(Label)
label: Label = null;
@property(ProgressBar)
progressBar: ProgressBar = null;
/** 动画定时器ID */
private animationTimer: NodeJS.Timeout | null = null;
/** 当前动画状态 */
private animationState: number = 0;
/** 动画基础文本 */
private baseText: string = "";
protected onDestroy() {
this.stopTextAnimation();
}
public updateProgress(current: number, total: number) {
this.progressBar.progress = current / total;
this.progressBar.node.active = true;
this.label.string = `正在更新中 ${current}/${total}`;
}
public updateProgressText(text: string, enableAnimation: boolean = false) {
this.stopTextAnimation();
if (enableAnimation) {
this.startTextAnimation(text);
} else {
this.label.string = text;
}
this.progressBar.node.active = false;
}
/**
* 开始文本动画
* @param baseText 基础文本,如"加载中"
* @param interval 动画间隔时间默认500毫秒
*/
public startTextAnimation(baseText: string, interval: number = 500) {
this.stopTextAnimation();
this.baseText = baseText;
this.animationState = 0;
// 立即显示第一帧
this.updateAnimationText();
// 启动定时器
this.animationTimer = setInterval(() => {
this.animationState = (this.animationState + 1) % 4; // 0,1,2,3 循环
this.updateAnimationText();
}, interval);
}
/**
* 停止文本动画
*/
public stopTextAnimation() {
if (this.animationTimer !== null) {
clearInterval(this.animationTimer);
this.animationTimer = null;
}
}
/**
* 更新动画文本显示
*/
private updateAnimationText() {
const dots = ".".repeat(this.animationState);
this.label.string = this.baseText + dots;
}
}