【cocos creator】【进度条封装】进度条控制脚本,支持节点进度条,图片进度条,进度条组件,和进度文字展示

进度条控制脚本,支持节点进度条,图片进度条,进度条组件,和进度文字展示
在这里插入图片描述
在这里插入图片描述


const { ccclass, property, menu } = cc._decorator;

let text_type = cc.Enum({
    "20%": 0,
    "1/5": 1,
    "差值": 2,
    "自定义": 3,
});
let bar_type = cc.Enum({
    "Sprite": 0,
    "ProgressBar": 1,
    "Node": 2,
});

@ccclass
@menu("自定义组件/进度条")
export default class barNode extends cc.Component {

    @property({
        type: cc.Sprite,
        visible() { return this.barType == bar_type.Sprite }
    })
    spriteBar: cc.Sprite = null;
    @property({
        type: cc.ProgressBar,
        visible() { return this.barType == bar_type.ProgressBar }
    })
    progressBar: cc.ProgressBar = null;
    @property({
        type: cc.Node,
        visible() { return this.barType == bar_type.Node }
    })
    nodeBar: cc.Node = null;
    @property({
        type: cc.Float,
        visible() { return this.barType == bar_type.Node }
    })
    max: number = 1;
    @property({
        type: cc.Float,
        visible() { return this.barType == bar_type.Node }
    })
    min: number = 0;
    @property(cc.RichText)
    barDesc: cc.RichText = null;
    @property(cc.Label)
    barDesc2: cc.Label = null;

    @property({
        type: cc.Enum(bar_type),
        displayName: "进度条类型"
    })
    barType = bar_type.Sprite;

    @property({
        type: cc.Enum(text_type),
        displayName: "进度文字类型"
    })
    textType = text_type["1/5"];

    _width = 0;

    protected onLoad(): void {
    }

    init(value, limit, bar_desc?) {
        let desc: string = bar_desc || "[0]";
        let percent = value / limit;
        percent = percent > 1 ? 1 : percent;
        if (this.spriteBar && this.barType == bar_type.Sprite) this.spriteBar.fillRange = percent;
        if (this.progressBar && this.barType == bar_type.ProgressBar) this.progressBar.progress = (percent < 0.1 && percent != 0) ? 0.1 : percent;
        if (this.nodeBar && this.barType == bar_type.Node) {
            this.nodeBar.width = (percent && percent * this.max < this.min) ? this.min : percent * this.max;
        }
        let str = "";
        switch (this.textType) {
            case 0: str = `${this.fixForce(percent * 100, 2)}%`
                desc = desc.replace("[0]", str);
                break;
            case 1: str = `${value}/${limit}`;
                desc = desc.replace("[0]", str);
                break;
            case 2: str = `${limit - value < 0 ? 0 : limit - value}`
                desc = desc.replace("[0]", str);
                break;
            case 3:
                desc = desc.replace("[0]", value);
                desc = desc.replace("[1]", limit);
                break;
        }
        if (this.barDesc) this.barDesc.string = desc;
        if (this.barDesc2) this.barDesc2.string = desc;
    }
    fixForce(count, fixTo): string {
        let a = (count + "").split(".");
        let b = a[0];
        if (a.length > 1) b = a[0] + "." + a[1].slice(0, fixTo);
        if (b == "0.00" && count != 0) {
            if (a.length > 1) b = a[0] + "." + a[1].slice(0, 4);
        }
        return b;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值