首页 > 其他分享 >【工具类】Cocos 分帧执行函数

【工具类】Cocos 分帧执行函数

时间:2022-09-01 12:12:54浏览次数:72  
标签:count Cocos 函数 cc param 分帧 执行 data

版本:2.4.10

参考:卡顿优化之卡顿原理全解析与如何快速定位到卡顿问题

 

一 为啥要分帧

一个游戏帧率是60时,每帧分配的执行时间是1秒/60 = 0.016666秒 = 16毫秒。

当这一帧的计算渲染消耗时间超过16毫秒时,就会进行扩帧,例如计算耗时需要50毫秒,那么需要至少4帧进行计算,导致4帧停留在同一个画面上,视觉上游戏就卡顿了。

 

 

为了防止卡顿,所以把大量计算分帧执行,例如总耗时需要50毫秒的计算,分到10帧去执行,那么每帧只占用5毫秒的时间,那么就不会导致卡顿。

 

二 分帧函数

首先获取当前时间,然后执行函数,再次获取当前时间,如果两次时间超过了分配的执行时间,则将函数放到下一帧执行。

FrameExecute.ts:

/**
 * 分帧执行
 * @author chenkai 2022.9.1
 */
export class FrameExecute extends cc.Component {

    //单例
    private static _instance: FrameExecute;
    public static get ins() {
        if (this._instance == null) {
            this._instance = new FrameExecute();
        }
        return this._instance;
    }

    /**
     * 分帧执行
     * @param fun     执行函数
     * @param target  执行函数对象
     * @param start   起始次数     (从0开始,0表示执行第1次)
     * @param max     最大执行次数 (例如5则表示函数总共执行5次)
     * @param executeTime   分配的执行时间
     * @param data    传递数据
     * @returns
     */
    public execute(fun: Function, target: any, start: number, max: number, executeTime: number, data: any = null) {
        //获取开始时间
        var startTime = new Date().getTime();
        //执行计数
        var count = start;
        //开始执行函数,如果超过分配的执行时间,则延迟到下一帧执行
        for (var i = count; i < max; i++) {
            //执行函数
            fun.call(target, count, data);
            //超过最大执行次数,则退出
            count++;
            if (count >= max) {
                return;
            }
            //获取消耗时间
            var costTime = new Date().getTime() - startTime;
            console.log("执行耗时:", costTime);
            //消耗时间 > 分配的时间,则延迟到下一帧执行
            if (costTime > executeTime) {
                console.log("超时,进入下一轮加载")
                this.scheduleOnce(() => { this.execute(fun, target, count, max, executeTime, data) });
                return;
            }
        }
    }
}

  

在场景中使用,分帧加载一个物品并显示到场景中

MainScene.ts:

const { ccclass, property } = cc._decorator;

/**
 * 分帧测试
 * @author chenkai 2022.9.1
 */
@ccclass
export default class FrameLoad extends cc.Component {

    @property({ type: cc.Node, tooltip: "item容器" })
    content: cc.Node = null;

    @property({ type: cc.Node, tooltip: "item" })
    item: cc.Node = null


    onl oad() {
        this.content.removeAllChildren();
        FrameExecute.ins.execute(this.loadAsset, this, 0, 50, 1, { name: "数据" });
    }

    /**
     * 加载函数
     * @param count 执行次数
     * @param data  传递数据
     */
    public loadAsset(count, data) {
        console.log("执行第n次:", count, data);
        var item = cc.instantiate(this.item);
        item.parent = this.content;
    }
}

 

显示效果

 

 

 

 

 

 

 

 

 

搜索

复制

标签:count,Cocos,函数,cc,param,分帧,执行,data
From: https://www.cnblogs.com/gamedaybyday/p/16646063.html

相关文章

  • 并发多线程10 future其他成员函数、shared_future、atomic
    第十节future其他成员函数、shared_future、atomic一、std::future的成员函数1、std::future_statusstatus=result.wait_for(std::chrono::seconds(几秒));卡住当前......
  • JavaScript 中的构造函数和新的运算符
    JavaScript中的构造函数和新的运算符ConstructorFunctionsandthenewOperator你好,我是Gibson,在这篇博客中,我们将学习构造函数和新的运算符。我们可以使用构造函......
  • pg的函数使用记录
    string_agg函数能将结果集某个字段的所有行连接成字符串,并以delimiter(分隔符)分隔SELECTc1,string_agg(c2,',')FROMtablearray_agg函数和string_agg 函数类似,最......
  • Python入门系列(六)一篇学会python函数
    函数函数是只在调用时运行的代码块。defmy_function():print("Hellofromafunction")my_function()信息可以作为参数传递到函数中。defmy_function(fname):......
  • Python自学笔记11-函数的定义和调用
    函数是组织代码的非常有效的方式,有了函数,我们就可以编写大规模的项目。可以说,函数是组织代码的最小单元。Python函数的定义函数是代码封装的一种手段,函数中包含一段可以......
  • 一元函数积分学的概念与计算
    一元函数积分学的概念与计算目录概念定积分概念定积分存在定理不定积分原函数和不定积分不定积分存在性变限积分概念性质反常积分计算基本积分公式凑微分换元分部积分有理......
  • 【C标准库】详解fopen函数 一篇让你搞懂fopen函数
    创作不易,感谢支持!fopen函数头文件:stdio.h功能是打开一个文件,其声明格式是:FILE*fopen(constchar*filename,constchar*mode);文件指针名=fopen(文件名,使用文......
  • python 根据dic的value去区key的值,直接用函数
    defget_keys(d,value):return[kfork,vind.items()ifv==value]get_keys({'a':'001','b':'002'},'001')#=>['a'] 字符串里面有参数用ftinydic......
  • Spdlog日志库的使用,支持文件名/行号/函数名的log打印输出
    一、简介spdlog是基于C++11的日志组件,它非常轻量,使用时你仅仅需要引入头文件就可以了。https://github.com/gabime/spdloghttps://github.com/gabime/spdlog/wiki/3.......
  • Cocos 打字机效果
    版本:2.4.4参考:cocos论坛-富文本打字机效果 一功能需求游戏中有剧情对话时会用到打字机效果 功能需求1对话字符串需要逐字显示,那么就得用计时器来获取字符进行......