Cocos Creator 分帧加载(js - schedule)

效果在这里插入图片描述

loadingFrame.js

cc.Class({
    extends: cc.Component,

    properties: {
        hello: cc.Prefab,
        content: cc.Node,
        mask: cc.Node,
        // animation: cc.Animation,
    },

    onLoad() {
        this.mask.active = false;
        // this.animation.node.active = false;
    },

    click() {
        // for (let index = 0; index < 500; index++) {
        //     let temp = cc.instantiate(this.hello);
        //     temp.parent = this.content;
        //     temp.getComponent(cc.Label).string = index + 1;
        // }

        // 分帧加载
        let cb = (i) => {
            let temp = cc.instantiate(this.hello);
            temp.parent = this.content;
            temp.getComponent(cc.Label).string = i;
        };
        this.mask.active = true;
        // this.animation.node.active = true;
        // this.animation.play();
        this._loadPrefabFrame('test', 2, 500, cb);
    },

    /**
     *
     * @param {string} name 给计时器分配一个name
     * @param {number} limit 每帧至少加载几个prefab
     * @param {number} max 最大加载的prefab数量
     * @param {function} cb 加载代码
     */
    _loadPrefabFrame(name, limit, max, cb) {
        let count = 0;
        this[name] = () => {
            if (count < max) {
                for (let i = 0; i < limit; i++) {
                    count++;
                    cb && cb(count);
                    //关闭动画
                    if (count === max - 1) {
                        this.mask.active = false;
                        // this.animation.stop();
                        // this.animation.node.active = false;
                    }
                }
            }
        };
        this.schedule(this[name], 1 / 60, (max - 1) / limit, 0);
    },
});

使用

在这里插入图片描述

总结问题

Cocos Creator 采用 Java Script/Type Script语言开发,本质上就是JS。而JS是单线程的,如果使用不当,在同一帧加载或者销毁太多的资源,极有可能导致界面卡顿。

解决方法

将在一帧内要处理的资源,手动分为多次加载每一帧加载处理部分资源,这样我们就可以有计划的处理这些资源,也不会造成卡顿。本质上就是牺牲时间,换流畅。同时,我们也需要注意牺牲的时间要是太长,同样会造成用户不爽,因此我们要把握住这个度。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos Creator采用JavaScript/TypeScript语言开发,本质上是单线程的。如果在同一帧加载或销毁过多的资源,可能会导致界面卡顿。为了解决这个问题,可以采用分帧加载的方法。 一个常见的分帧加载的例子是在点击按钮时创建1000个预制体,并获取预制体上的脚本并赋值。可以使用for循环来逐个创建预制体,并在每个预制体创建完毕后进行下一帧的加载。具体代码如下: ```javascript this.mask.active = true; this.animation.node.active = true; this.animation.play(); let count = 0; let totalCount = 1000; let batchSize = 10; // 每帧加载的数量 function loadBatch() { for (let i = 0; i < batchSize; i++) { if (count >= totalCount) { break; } let temp = cc.instantiate(this.hello); temp.parent = this.content; temp.getComponent('test2').label.string = count; count++; } if (count < totalCount) { requestAnimationFrame(loadBatch); } else { this.mask.active = false; this.animation.stop(); this.animation.node.active = false; } } requestAnimationFrame(loadBatch); ``` 在这个例子中,我们使用了`requestAnimationFrame`函数来实现分帧加载。首先,我们将加载动画打开,并播放加载动画。然后,我们使用一个计数器`count`来记录已经加载的预制体数量。在每一帧中,我们使用一个循环来创建一批预制体,并将计数器递增。如果还有剩余的预制体需要加载,我们使用`requestAnimationFrame`函数来请求下一帧的加载。当所有预制体都加载完毕后,我们关闭加载动画。 通过这种分帧加载的方式,可以避免在同一帧中加载过多的资源,从而提高界面的流畅性。 #### 引用[.reference_title] - *1* [Cocos Creator 分帧加载(js - schedule)](https://blog.csdn.net/qq_14965517/article/details/107846189)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Cocos Creator 2.1.1 性能优化 (二)](https://blog.csdn.net/weixin_43995470/article/details/103443928)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值