<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><title>4ui.cc</title><link>https://www.4ui.cc/</link><description>4ui.cc 个人博客：分享游戏制作中的技术经验、架构思考与踩坑记录。</description><language>zh-CN</language><lastBuildDate>Fri, 04 Dec 2026 05:16:15 GMT</lastBuildDate><item><title>微信小游戏包体瘦身清单</title><link>https://www.4ui.cc/posts/minigame-package/</link><guid isPermaLink="true">https://www.4ui.cc/posts/minigame-package/</guid><pubDate>Sun, 22 Nov 2026 13:00:00 GMT</pubDate><category>小游戏</category><category>性能优化</category><category>工程化</category><description>&lt;p&gt;做微信小游戏，第一道坎往往不是玩法，而是&lt;strong&gt;包体&lt;/strong&gt;：主包有 4MB 的硬上限，超了连真机预览都过不去。下面是一份按收益排序的清单，可以逐项打勾。&lt;/p&gt;
&lt;div class=&quot;callout callout-info&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;先看数据&lt;/div&gt;&lt;p&gt;动手之前先把构建产物按大小排个序，找到真正的大头。很多时候一张没压缩的背景图就占了 1MB。&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;bash&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;bash · 2 lines&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;Bash&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;c&quot;&gt;# 列出构建目录里最大的 20 个文件&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;b&quot;&gt;find&lt;/span&gt; .&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;build&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;wechatgame &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;type f &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;exec du &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;k {} &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; sort &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;rn &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; head &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;20&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;一资源通常占大头&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#一资源通常占大头&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;一、资源（通常占大头）&lt;/h2&gt;
&lt;ul&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled checked&gt; 图片统一走压缩：PNG 用有损压缩工具，能接受的地方换成 JPG / WebP&lt;/li&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled checked&gt; 合图（图集）减少文件数量，同时减少请求次数&lt;/li&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled checked&gt; 检查纹理尺寸，UI 背景图不需要 2048×2048&lt;/li&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled checked&gt; 音频转成单声道 + 合适码率，BGM 和大音效放远程&lt;/li&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; 字体子集化：只保留游戏里实际用到的字符&lt;/li&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; 清理没被引用的资源（引擎通常有依赖分析工具）&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;二代码&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#二代码&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;二、代码&lt;/h2&gt;
&lt;ul&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled checked&gt; 开启压缩混淆，去掉 &lt;code&gt;console.log&lt;/code&gt; 和调试代码&lt;/li&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled checked&gt; 检查第三方库：为了一个工具函数引入整个库？&lt;/li&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; 按功能拆&lt;strong&gt;分包&lt;/strong&gt;：首屏只需要登录和主界面，战斗、活动、关卡按需加载&lt;/li&gt;
&lt;li class=&quot;task&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; 引擎模块裁剪：没用到的物理、3D、粒子模块在项目设置里关掉&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;三加载策略&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#三加载策略&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;三、加载策略&lt;/h2&gt;
&lt;p&gt;主包只放“启动必须的东西”，其余全部延后：&lt;/p&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;text&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;text · 4 lines&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;TEXT&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;主包 (≤ 4MB)          首屏 UI + 启动逻辑 + 加载界面&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;├── 分包：大厅          进入大厅时加载&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;├── 分包：战斗          第一次进入战斗时加载&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;└── 远程资源（CDN）     BGM、大图、配置表，运行时下载并缓存&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;callout callout-warning&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;远程资源要做好兜底&lt;/div&gt;&lt;p&gt;网络失败时要能重试，关键资源下载失败要有降级方案（比如 BGM 加载失败就静音继续，而不是卡在加载界面）。&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;四守住成果&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#四守住成果&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;四、守住成果&lt;/h2&gt;
&lt;p&gt;包体优化最怕“越改越胖”。在构建脚本里加一道检查，超过阈值直接报错：&lt;/p&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;bash&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;check-size.sh&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;Bash&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;c&quot;&gt;#!/usr/bin/env bash&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;LIMIT_KB&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;3800&lt;/span&gt;   &lt;span class=&quot;c&quot;&gt;# 给 4MB 留一点余量&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;SIZE_KB&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;f&quot;&gt;$&lt;/span&gt;(du &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;sk .&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;build&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;wechatgame&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;main &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; cut &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;f1)&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; [ &lt;span class=&quot;s&quot;&gt;&amp;quot;$SIZE_KB&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;gt &lt;span class=&quot;s&quot;&gt;&amp;quot;$LIMIT_KB&amp;quot;&lt;/span&gt; ]; &lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;b&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;❌ 主包 ${SIZE_KB}KB 超过 ${LIMIT_KB}KB&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;b&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;✅ 主包 ${SIZE_KB}KB&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;具体的大小限制和分包规则，以微信官方文档的最新说明为准。清单之外，最有效的习惯其实只有一个：&lt;strong&gt;每次加资源前，先想想它是不是非放主包不可&lt;/strong&gt;。&lt;/p&gt;
</description></item><item><title>游戏循环：固定时间步长与渲染插值</title><link>https://www.4ui.cc/posts/fixed-timestep/</link><guid isPermaLink="true">https://www.4ui.cc/posts/fixed-timestep/</guid><pubDate>Sun, 15 Nov 2026 14:10:00 GMT</pubDate><category>游戏循环</category><category>物理</category><category>TypeScript</category><description>&lt;p&gt;几乎每个游戏项目早期都会遇到这样的 bug：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;在 120Hz 的屏幕上，角色移动速度是 60Hz 的两倍；&lt;/li&gt;
&lt;li&gt;掉帧的时候，子弹直接穿过了墙。&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;问题出在&lt;strong&gt;游戏循环&lt;/strong&gt;怎么处理时间。&lt;/p&gt;
&lt;h2 id=&quot;方案一每帧更新一次最朴素&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#方案一每帧更新一次最朴素&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;方案一：每帧更新一次（最朴素）&lt;/h2&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;typescript · 5 lines&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;TypeScript&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;frame&lt;/span&gt;() {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;f&quot;&gt;update&lt;/span&gt;();          &lt;span class=&quot;c&quot;&gt;// 每帧移动固定距离&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;f&quot;&gt;render&lt;/span&gt;();&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;b&quot;&gt;requestAnimationFrame&lt;/span&gt;(frame);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;速度和帧率绑死了。刷新率越高，游戏越快。&lt;/p&gt;
&lt;h2 id=&quot;方案二可变步长&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#方案二可变步长&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;方案二：可变步长&lt;/h2&gt;
&lt;p&gt;把上一帧的耗时 &lt;code&gt;dt&lt;/code&gt; 传进去：&lt;/p&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;typescript · 8 lines&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;TypeScript&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; last &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; performance.&lt;span class=&quot;f&quot;&gt;now&lt;/span&gt;();&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;frame&lt;/span&gt;(now&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;number&lt;/span&gt;) {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; dt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; (now &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; last) &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1000&lt;/span&gt;;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  last &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; now;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;f&quot;&gt;update&lt;/span&gt;(dt);        &lt;span class=&quot;c&quot;&gt;// 位置 += 速度 * dt&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;f&quot;&gt;render&lt;/span&gt;();&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;b&quot;&gt;requestAnimationFrame&lt;/span&gt;(frame);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;移动速度对了，但&lt;strong&gt;物理和碰撞对 dt 很敏感&lt;/strong&gt;：dt 一大，一步就跨过了薄墙；数值积分误差也随 dt 变化，同样的操作每次结果不同 —— 对需要回放、帧同步的游戏来说是致命的。&lt;/p&gt;
&lt;h2 id=&quot;方案三固定步长-累加器&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#方案三固定步长-累加器&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;方案三：固定步长 + 累加器&lt;/h2&gt;
&lt;p&gt;逻辑永远以固定的步长（比如 1/60 秒）推进，渲染想跑多快跑多快：&lt;/p&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;loop.ts&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;TypeScript&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; STEP &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;60&lt;/span&gt;;          &lt;span class=&quot;c&quot;&gt;// 逻辑帧：固定 60Hz&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; MAX_FRAME &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0.25&lt;/span&gt;;       &lt;span class=&quot;c&quot;&gt;// 防止卡顿后“死亡螺旋”&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; acc &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt;;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; last &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; performance.&lt;span class=&quot;f&quot;&gt;now&lt;/span&gt;();&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;frame&lt;/span&gt;(now&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;number&lt;/span&gt;) {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; dt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; (now &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; last) &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1000&lt;/span&gt;;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  last &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; now;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; (dt &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; MAX_FRAME) dt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; MAX_FRAME;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  acc &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; dt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; (acc &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; STEP) {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;f&quot;&gt;saveState&lt;/span&gt;();              &lt;span class=&quot;c&quot;&gt;// 记住上一个逻辑状态，供插值用&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;f&quot;&gt;update&lt;/span&gt;(STEP);             &lt;span class=&quot;c&quot;&gt;// 永远是同一个 dt，结果可复现&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    acc &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; STEP;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  }&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; alpha &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; acc &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; STEP;   &lt;span class=&quot;c&quot;&gt;// 当前时刻落在两个逻辑帧之间的比例&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;f&quot;&gt;render&lt;/span&gt;(alpha);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;b&quot;&gt;requestAnimationFrame&lt;/span&gt;(frame);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;callout callout-note&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;死亡螺旋&lt;/div&gt;&lt;p&gt;如果某一帧特别慢，累加器里会攒下很多步，追帧又导致下一帧更慢……所以要给单帧 &lt;code&gt;dt&lt;/code&gt; 设上限，宁可让游戏“慢动作”一下，也不要卡死。&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;渲染插值消除抖动&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#渲染插值消除抖动&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;渲染插值：消除抖动&lt;/h2&gt;
&lt;p&gt;逻辑 60Hz、屏幕 144Hz 时，如果直接画最新的逻辑状态，画面会一顿一顿的。解决办法是&lt;strong&gt;在上一个状态和当前状态之间插值&lt;/strong&gt;：&lt;/p&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;typescript · 7 lines&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;TypeScript&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;render&lt;/span&gt;(alpha&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;number&lt;/span&gt;) {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; e &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt; entities) {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; x &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; e.prevX &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; (e.x &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; e.prevX) &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; alpha;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; y &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; e.prevY &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; (e.y &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; e.prevY) &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; alpha;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;f&quot;&gt;drawSprite&lt;/span&gt;(e.sprite, x, y);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  }&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;代价是画面比逻辑&lt;strong&gt;晚最多一个逻辑帧&lt;/strong&gt;（约 16ms），绝大多数游戏完全感觉不到。&lt;/p&gt;
&lt;h2 id=&quot;小结&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#小结&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;小结&lt;/h2&gt;
&lt;div class=&quot;table-wrap&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align:left&quot;&gt;方案&lt;/th&gt;&lt;th style=&quot;text-align:center&quot;&gt;速度与帧率无关&lt;/th&gt;&lt;th style=&quot;text-align:center&quot;&gt;物理稳定&lt;/th&gt;&lt;th style=&quot;text-align:center&quot;&gt;可复现&lt;/th&gt;&lt;th style=&quot;text-align:center&quot;&gt;画面平滑&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;每帧更新&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;❌&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;❌&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;❌&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;可变步长&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;❌&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;❌&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;固定步长&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;❌&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;固定步长 + 插值&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;td style=&quot;text-align:center&quot;&gt;✅&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;Unity 的 &lt;code&gt;FixedUpdate&lt;/code&gt; 加刚体的 Interpolate 选项，本质上就是这一套。理解了原理，遇到“为什么我的角色在 FixedUpdate 里移动会抖”这类问题，就知道该往哪儿查了。&lt;/p&gt;
</description></item><item><title>对象池：让 GC 卡顿闭嘴的第一步</title><link>https://www.4ui.cc/posts/object-pool/</link><guid isPermaLink="true">https://www.4ui.cc/posts/object-pool/</guid><pubDate>Sun, 08 Nov 2026 13:30:00 GMT</pubDate><category>性能优化</category><category>Unity</category><category>TypeScript</category><description>&lt;p&gt;战斗场景里每秒几十发子弹、上百个伤害飘字，如果每次都 &lt;code&gt;new&lt;/code&gt; 一个再丢掉，垃圾回收迟早会找上门 —— 表现就是&lt;strong&gt;每隔几秒卡一下&lt;/strong&gt;，帧率曲线上出现一根根尖刺。&lt;/p&gt;
&lt;p&gt;对象池的思路很朴素：&lt;strong&gt;用完不扔，回收复用&lt;/strong&gt;。&lt;/p&gt;
&lt;h2 id=&quot;原理&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#原理&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;原理&lt;/h2&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;text&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;text · 7 lines&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;TEXT&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;Get()  ──► 池里有空闲对象？ ──是──► 取出、重置、返回&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;                 │&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;                 否&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;                 ▼&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;            新建一个对象返回&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;Release(obj) ──► 隐藏 / 清理状态 ──► 放回池中&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;关键在于：对象的&lt;strong&gt;创建成本&lt;/strong&gt;只付一次，之后全是 O(1) 的取用和归还。&lt;/p&gt;
&lt;h2 id=&quot;c-实现unity&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#c-实现unity&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;C# 实现（Unity）&lt;/h2&gt;
&lt;p&gt;Unity 2021 起自带 &lt;code&gt;UnityEngine.Pool.ObjectPool&amp;lt;T&amp;gt;&lt;/code&gt;，能用就直接用。自己写一个也不复杂：&lt;/p&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;csharp&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;SimplePool.cs&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;C#&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;System&lt;/span&gt;;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;System&lt;/span&gt;.&lt;span class=&quot;t&quot;&gt;Collections&lt;/span&gt;.&lt;span class=&quot;t&quot;&gt;Generic&lt;/span&gt;;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;SimplePool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;where&lt;/span&gt; T &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;Stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; _free &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;Stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;();&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; _create;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; _onGet, _onRelease;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;int&lt;/span&gt; _maxSize;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;SimplePool&lt;/span&gt;(&lt;span class=&quot;t&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; create, &lt;span class=&quot;t&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; onGet &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l&quot;&gt;null&lt;/span&gt;, &lt;span class=&quot;t&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; onRelease &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l&quot;&gt;null&lt;/span&gt;, &lt;span class=&quot;t&quot;&gt;int&lt;/span&gt; maxSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;256&lt;/span&gt;)&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;        _create &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; create; _onGet &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; onGet; _onRelease &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; onRelease; _maxSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; maxSize;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; T &lt;span class=&quot;f&quot;&gt;Get&lt;/span&gt;()&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;        T item &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; _free.&lt;span class=&quot;t&quot;&gt;Count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; _free.&lt;span class=&quot;f&quot;&gt;Pop&lt;/span&gt;() &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;_create&lt;/span&gt;();&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;        _onGet&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;Invoke&lt;/span&gt;(item);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; item;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;Release&lt;/span&gt;(T item)&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;        _onRelease&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;Invoke&lt;/span&gt;(item);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; (_free.&lt;span class=&quot;t&quot;&gt;Count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; _maxSize) _free.&lt;span class=&quot;f&quot;&gt;Push&lt;/span&gt;(item);   &lt;span class=&quot;c&quot;&gt;// 超出上限就交给 GC，防止池子无限膨胀&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;Prewarm&lt;/span&gt;(&lt;span class=&quot;t&quot;&gt;int&lt;/span&gt; count)&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;t&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt;; i &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; count; i&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;) _free.&lt;span class=&quot;f&quot;&gt;Push&lt;/span&gt;(&lt;span class=&quot;f&quot;&gt;_create&lt;/span&gt;());&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;typescript-实现cocos-小游戏&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#typescript-实现cocos-小游戏&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;TypeScript 实现（Cocos / 小游戏）&lt;/h2&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;typescript&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;Pool.ts&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;TypeScript&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;Pool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; free&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; T[] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; [];&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;f&quot;&gt;constructor&lt;/span&gt;(&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; create&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; () &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; T,&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; reset&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; (o&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; T) &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; () &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; {},&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; max &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;256&lt;/span&gt;,&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  ) {}&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;()&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; T {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;.free.&lt;span class=&quot;f&quot;&gt;pop&lt;/span&gt;() &lt;span class=&quot;o&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;create&lt;/span&gt;();&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  }&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;f&quot;&gt;release&lt;/span&gt;(o&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; T)&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;t&quot;&gt;void&lt;/span&gt; {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;reset&lt;/span&gt;(o);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;.free.length &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;.max) &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;.free.&lt;span class=&quot;f&quot;&gt;push&lt;/span&gt;(o);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  }&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;c&quot;&gt;// 用法：伤害飘字&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; damageTexts &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;Pool&lt;/span&gt;(() &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;createDamageLabel&lt;/span&gt;(), (n) &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; { n.active &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;l&quot;&gt;false&lt;/span&gt;; });&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;几个常见的坑&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#几个常见的坑&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;几个常见的坑&lt;/h2&gt;
&lt;div class=&quot;callout callout-warning&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;状态没重置干净&lt;/div&gt;&lt;p&gt;回收时一定要重置&lt;strong&gt;所有&lt;/strong&gt;运行时状态：位置、缩放、透明度、计时器、事件监听、Tween。最隐蔽的是没停掉的 Tween —— 对象被复用后，旧动画还在改它的属性。&lt;/p&gt;
&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;重复回收&lt;/strong&gt;：同一个对象被 &lt;code&gt;Release&lt;/code&gt; 两次，之后会被两个人同时拿走。调试版可以加一个 &lt;code&gt;HashSet&lt;/code&gt; 检查。&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;回收后还在用&lt;/strong&gt;：回收之后，原来的引用要及时置空。&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;池子无限增长&lt;/strong&gt;：峰值过后池里堆着几千个对象，等于内存泄漏。设上限，或在切场景时清空。&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;预热时机&lt;/strong&gt;：在 Loading 阶段预先创建一批（&lt;code&gt;Prewarm&lt;/code&gt;），避免战斗开始的第一波卡顿。&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;什么时候不需要对象池&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#什么时候不需要对象池&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;什么时候不需要对象池&lt;/h2&gt;
&lt;div class=&quot;table-wrap&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align:left&quot;&gt;场景&lt;/th&gt;&lt;th style=&quot;text-align:left&quot;&gt;建议&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;每秒创建几十次以上的小对象&lt;/td&gt;&lt;td style=&quot;text-align:left&quot;&gt;✅ 用对象池&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;生命周期很长的对象（角色、UI 面板）&lt;/td&gt;&lt;td style=&quot;text-align:left&quot;&gt;❌ 没必要&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;创建成本极低的纯数据结构&lt;/td&gt;&lt;td style=&quot;text-align:left&quot;&gt;🤔 先用 Profiler 确认是瓶颈再说&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;优化的第一原则永远是：&lt;strong&gt;先测量，再优化&lt;/strong&gt;。&lt;/p&gt;
</description></item><item><title>Hello World：4ui.cc 开张了</title><link>https://www.4ui.cc/posts/hello-world/</link><guid isPermaLink="true">https://www.4ui.cc/posts/hello-world/</guid><pubDate>Sun, 01 Nov 2026 12:00:00 GMT</pubDate><category>随笔</category><category>建站</category><description>&lt;p&gt;&lt;code&gt;printf(&amp;quot;Hello, World!\n&amp;quot;);&lt;/code&gt; —— 每个程序员的第一行代码，也是这个博客的第一篇文章。&lt;/p&gt;
&lt;h2 id=&quot;为什么要写博客&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#为什么要写博客&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;为什么要写博客&lt;/h2&gt;
&lt;p&gt;做游戏这些年，踩过的坑不少，很多问题第二次遇到时才发现：上次的解法已经忘得差不多了。与其散落在聊天记录和临时笔记里，不如认真整理成文章，&lt;strong&gt;写给未来的自己，也顺便分享给同行&lt;/strong&gt;。&lt;/p&gt;
&lt;p&gt;这里主要会写：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;客户端&lt;/strong&gt;：玩法框架、战斗系统、UI 管线、性能优化&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;小游戏&lt;/strong&gt;：微信 / 抖音小游戏的包体、加载与适配&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;图形&lt;/strong&gt;：Shader、特效、后处理&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;工程化&lt;/strong&gt;：资源管线、打包发布、自动化工具&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;callout callout-tip&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;写作原则&lt;/div&gt;&lt;p&gt;只写自己真正做过、验证过的东西；能用一张表说清楚的，就不写三段话。&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;这个站本身&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#这个站本身&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;这个站本身&lt;/h2&gt;
&lt;p&gt;整个站点是纯静态的：Markdown 写文章，一个几百行的 Python 脚本生成 HTML，没有数据库、没有后台、没有第三方依赖。&lt;/p&gt;
&lt;p&gt;首页那个会变形的粒子视口，是用 Canvas 2D 手写的 3D 投影，大概是这样把一个三维点投到屏幕上的：&lt;/p&gt;
&lt;div class=&quot;codeblock&quot; data-lang=&quot;javascript&quot;&gt;&lt;div class=&quot;cb-head&quot;&gt;&lt;span class=&quot;cb-dots&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class=&quot;cb-title&quot;&gt;project.js&lt;/span&gt;&lt;span class=&quot;cb-lang&quot;&gt;JavaScript&lt;/span&gt;&lt;button type=&quot;button&quot; class=&quot;cb-copy&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;c&quot;&gt;// 先绕 Y 轴（yaw）再绕 X 轴（pitch）旋转，最后做透视除法&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;f&quot;&gt;project&lt;/span&gt;(x, y, z, cam) {&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; x1 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;b&quot;&gt;Math&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;cos&lt;/span&gt;(cam.yaw) &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; z &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;b&quot;&gt;Math&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;sin&lt;/span&gt;(cam.yaw);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; z1 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;b&quot;&gt;Math&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;sin&lt;/span&gt;(cam.yaw) &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; z &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;b&quot;&gt;Math&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;cos&lt;/span&gt;(cam.yaw);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; y2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; y &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;b&quot;&gt;Math&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;cos&lt;/span&gt;(cam.pitch) &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; z1 &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;b&quot;&gt;Math&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;sin&lt;/span&gt;(cam.pitch);&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; z2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; y &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;b&quot;&gt;Math&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;sin&lt;/span&gt;(cam.pitch) &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; z1 &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;b&quot;&gt;Math&lt;/span&gt;.&lt;span class=&quot;f&quot;&gt;cos&lt;/span&gt;(cam.pitch) &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; cam.dist;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; s &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; cam.focal &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; z2;           &lt;span class=&quot;c&quot;&gt;// 越远越小&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; { sx&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; cam.cx &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; x1 &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; s, sy&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; cam.cy &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; y2 &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; s, depth&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; z2 };&lt;/span&gt;&lt;span class=&quot;ln&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;几个小彩蛋：&lt;/p&gt;
&lt;div class=&quot;table-wrap&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align:left&quot;&gt;操作&lt;/th&gt;&lt;th style=&quot;text-align:left&quot;&gt;效果&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;按 &lt;kbd&gt;&amp;#96;&lt;/kbd&gt;&lt;/td&gt;&lt;td style=&quot;text-align:left&quot;&gt;打开下拉式终端，输入 &lt;code&gt;help&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;按 &lt;kbd&gt;Ctrl&lt;/kbd&gt; + &lt;kbd&gt;K&lt;/kbd&gt; 或 &lt;kbd&gt;/&lt;/kbd&gt;&lt;/td&gt;&lt;td style=&quot;text-align:left&quot;&gt;全站搜索&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;首页拖拽 / 点击 / 双击视口&lt;/td&gt;&lt;td style=&quot;text-align:left&quot;&gt;旋转镜头 / 粒子爆破 / 切换形状&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;点击右下角 &lt;code&gt;theme&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:left&quot;&gt;切换主题色&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:left&quot;&gt;↑ ↑ ↓ ↓ ← → ← → B A&lt;/td&gt;&lt;td style=&quot;text-align:left&quot;&gt;你懂的&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;h2 id=&quot;接下来&quot;&gt;&lt;a class=&quot;h-anchor&quot; href=&quot;#接下来&quot; aria-hidden=&quot;true&quot;&gt;##&lt;/a&gt;接下来&lt;/h2&gt;
&lt;p&gt;先把手头积累的几篇笔记整理出来。更新频率不定，但每一篇都会尽量写扎实。&lt;/p&gt;
&lt;p&gt;欢迎常来。&lt;/p&gt;
</description></item></channel></rss>
