creator 分帧加载

const { ccclass, property } = cc._decorator;

@ccclass
export default class Case_FrameLoading extends cc.Component {

@property(cc.Node)
protected itemPrefab: cc.Node = null;

@property(cc.Node)
protected content: cc.Node = null;

@property(cc.Node)
protected normalBtn: cc.Node = null;

@property(cc.Node)
protected clearBtn: cc.Node = null;

@property(cc.Node)
protected frameBtn: cc.Node = null;

protected onLoad() {
    this.registerEvent();
}

protected onDestroy() {
    this.unregisterEvent();
}

/**
 * 订阅事件
 */
protected registerEvent() {
    this.normalBtn.on(cc.Node.EventType.TOUCH_END, this.onNormalBtnClick, this);
    this.clearBtn.on(cc.Node.EventType.TOUCH_END, this.onClearBtnClick, this);
    this.frameBtn.on(cc.Node.EventType.TOUCH_END, this.onFrameBtnClick, this);
}

/**
 * 取消事件订阅
 */
protected unregisterEvent() {
    this.normalBtn.off(cc.Node.EventType.TOUCH_END, this.onNormalBtnClick, this);
    this.clearBtn.off(cc.Node.EventType.TOUCH_END, this.onClearBtnClick, this);
    this.frameBtn.off(cc.Node.EventType.TOUCH_END, this.onFrameBtnClick, this);
}

/**
 * 普通加载按钮回调
 */
protected onNormalBtnClick() {
    this.clear();
    this.loadAtOnce();
}

/**
 * 清除按钮回调
 */
protected onClearBtnClick() {
    this.clear();
}

/**
 * 分帧加载按钮回调
 */
protected onFrameBtnClick() {
    this.clear();
    this.loadByFrame();
}

/**
 * 清除
 */
protected clear() {
    this.unscheduleAllCallbacks();
    this.content.destroyAllChildren();
}

/**
 * 添加 item
 * @param index 下标
 */
protected addItem(index: number) {
    const node = cc.instantiate(this.itemPrefab);
    node.setParent(this.content);
    node.getComponentInChildren(cc.Label).string = `${index + 1}`;
    node.active = true;
}

/**
 * 一次性加载
 */
protected loadAtOnce() {
    const total = 2000;
    for (let i = 0; i < total; i++) {
        this.addItem(i);
    }
}

/**
 * 分帧加载
 */
protected loadByFrame() {
    const total = 2000,
        countPerFrame = 30; // 每帧加载的数量
    let index = 0;  // 当前下标
    // 加载函数
    const load = () => {
        // 加载 item
        const count = Math.min(total - index, countPerFrame);
        for (let i = 0; i < count; i++) {
            this.addItem(index);
            index++;
        }
        // 是否还有
        if (index < total) {
            // 下一帧继续加载
            this.scheduleOnce(() => load());
        }
    }
    // 开始加载
    load();
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

web&Game

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

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

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

打赏作者

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

抵扣说明:

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

余额充值