负反馈性能调节系统

44 篇文章 4 订阅
12 篇文章 0 订阅

        最早看到这个概念是在《腾讯游戏开发精粹II》里,看了之后感觉非常实用,于是将其列为开放世界的一个必备组件。其运行逻辑简单总结就是:

        主动监测当前游戏性能,如果发现指标异常,主动对游戏进行干预以提高帧率、内存等。

        例如监测到帧率下降时,则进行减少一些特效、音效,对不重要的 AI 降频等操作,使帧率提升。这样使得游戏整体帧率保持平稳,且能最大限度保证玩家的游戏体验。

1、工作流程

        与传统的在设置面板设置高中低配不同,负反馈性能调节系统贯穿游戏始终,且一直生效,使得游戏性能保持稳定。我们在玩很多 3A 大作的时候,也会发现这个系统的踪迹:当游戏性能吃紧时,即便我们没有调整画质,也会看到部分模型精度降低、部分 NPC 停止工作等现象。

        负反馈调节系统的示意简图如下:

        这里举一个例子,下面链接为游戏《漫威蜘蛛侠2》的实机演示,视频开头有一段玩家操纵蜘蛛侠飞跃城市的过程。在这个过程中,如果发生了掉帧的情况,则可以将远处的人物、汽车等停止动画甚至直接静止。远端的景物,可以切换成低模,以减少性能压力。

【漫威蜘蛛侠2】4K 最高画质 电影式 全任务 全剧情流程通关攻略 开放世界动作冒险游戏 - Marvel's Spider Man 2【完结】_剧情【漫威蜘蛛侠2】4K 最高画质 电影式 全任务 全剧情流程通关攻略 开放世界动作冒险游戏 - Marvel's Spider Man 2【完结】共计31条视频,包括:第一期:表面张力、第二期:一次解决一件事、第三期:像过去一样等,UP主更多精彩视频,请关注UP账号。icon-default.png?t=N7T8https://www.bilibili.com/video/BV1Xp4y1T7ZX/

2、当前性能情况统计

        获取当前内存的使用情况,可以参考下面的代码,更多 API 可以看 Untiy 的官方文档。

//返回托管内存的预留空间大小。
long heapSize = UnityEngine.Profiling.Profiler.GetMonoHeapSizeLong();
//活动对象和非收集对象的已分配托管内存。
long usedHeapSize = UnityEngine.Profiling.Profiler.GetMonoUsedSizeLong();
//收集 Unity 对象使用的本机内存。
long runtimeMemorySize = UnityEngine.Profiling.Profiler.GetRuntimeMemorySizeLong();

Profiling.Profiler - Unity 脚本手册 Scripting API - 泰课在线 - 国内专业的Unity在线学习平台|Unity3d培训|Unity教程|Unreal|虚幻|AR/VR通过脚本控制 Profiler。icon-default.png?t=N7T8https://www.taikr.com/docs/UnityDocumentation_2019.1/ScriptReference/Profiling.Profiler.html        帧率可以自己算(例如使用 Time.deltaTime),但这个只能得到帧的总时间,不能区分 CPU 耗时和 GPU 耗时。可以使用 Unity 提供的新工具 FrameTimingManage 来监测帧耗时,示例代码如下:

        #region FPS 统计

        private int m_fpsCount;
        public int FPS => m_fpsCount;

        private FrameTiming[] mFrameTimingArray = new FrameTiming[5];
        private ProfilerRecorder mainThreadTimeRecorder;

        private double cpuFrameTime;
        private double cpuMainThreadFrameTime;
        private double cpuRenderThreadFrameTime;
        private double gpuFrameTime;

        private void UpdateFps()
        {
            int length = mFrameTimingArray.Length;
            FrameTimingManager.GetLatestTimings((uint)length, mFrameTimingArray);
            cpuFrameTime = 0;
            cpuMainThreadFrameTime = 0;
            gpuFrameTime = 0;
            cpuRenderThreadFrameTime = 0;
            foreach (var timing in mFrameTimingArray)
            {
                //是总 CPU 帧时间,是主线程某帧的开始到下一帧开始的时间间隔。
                cpuFrameTime += timing.cpuFrameTime;
                //主线程的工作时间,或者说是从帧开始到所有主线程任务完成的总耗时。
                cpuMainThreadFrameTime += timing.cpuMainThreadFrameTime;
                //渲染线程的工作时间,或者说是从渲染线程受到的第一个任务请求到 Present() 函数被调用的总时间间隔。
                cpuRenderThreadFrameTime += timing.cpuRenderThreadFrameTime;
                // GPU 的工作时间,或者说是从 GPU 收到任务到 GPU 发出完成信号的总时间间隔。
                gpuFrameTime += timing.gpuFrameTime;
            }
            cpuFrameTime /= length;//这里算出来比 Time.deltaTime 少很多
            cpuMainThreadFrameTime /= length;
            cpuRenderThreadFrameTime /= length;
            gpuFrameTime /= length;
            m_fpsCount = (int)(1000 / cpuFrameTime);
        }

        #endregion

        要注意的是 Frame Timing Manager 在 DevelopBuild 中是始终启用的。如果在 Release 中使用该功能,就必须激活它。勾选 Edit/Project Player/Other Settings/Rendering/Frame Timing Status 选项:

        更多信息可参考以下链接:

用Unity Frame Timing Manager检测性能瓶颈要想做出一款能流畅运行在各色设备和平台上的优秀游戏可能并没有那么简单。这也是为什么我们持续完善工具、帮助开发者优化整个创作流程,这其中也包括了最近改进过的Frame Timing Manager。Read on to discover how the Unity 2022.1 update provides enhanced platform support for this feature, enabling you to collect more data than previously possible.icon-default.png?t=N7T8https://unity.com/cn/blog/engine-platform/detecting-performance-bottlenecks-with-unity-frame-timing-manager

用Unity Frame Timing Manager检测性能瓶颈 | 电子创新网 Imgtec 社区icon-default.png?t=N7T8https://imgtec.eetrend.com/blog/2022/100563328.htmlFrameTimingManager - Unity 手册The FrameTimingManager is an API that captures detailed timing data about performance during individual frames in an application. You can use this data to assess those frames to understand why your application doesn’t meet performance targets.icon-default.png?t=N7T8https://docs.unity.cn/cn/2022.3/Manual/frame-timing-manager.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值