{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference/",
  "atlas": "https://www.pystone.net/?node=unity6-performance-gc-camera-main-reference#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference.md",
  "context": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:unity6-performance-gc-camera-main-reference",
  "slug": "unity6-performance-gc-camera-main-reference",
  "title": "Unity 6 性能测量、垃圾回收与 Camera.main 官方资料摘录",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "Unity 6 性能测量、垃圾回收与 Camera.main 官方资料摘录",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "99-资料来源、摘录与归档",
  "tags": [],
  "mocs": [],
  "contentHash": "5c5221d1fa5428ad74bd5b686694c8f427ed6a6a24ae0f700fb16df2a351e53a",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "Unity 6 性能测量、垃圾回收与 Camera.main 官方资料摘录",
      "anchor": "unity-6-性能测量垃圾回收与-cameramain-官方资料摘录",
      "citation": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference/#unity-6-%E6%80%A7%E8%83%BD%E6%B5%8B%E9%87%8F%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E4%B8%8E-cameramain-%E5%AE%98%E6%96%B9%E8%B5%84%E6%96%99%E6%91%98%E5%BD%95"
    },
    {
      "depth": 2,
      "text": "在目标平台采集真实数据",
      "anchor": "在目标平台采集真实数据",
      "citation": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference/#%E5%9C%A8%E7%9B%AE%E6%A0%87%E5%B9%B3%E5%8F%B0%E9%87%87%E9%9B%86%E7%9C%9F%E5%AE%9E%E6%95%B0%E6%8D%AE"
    },
    {
      "depth": 2,
      "text": "垃圾回收器与临时分配",
      "anchor": "垃圾回收器与临时分配",
      "citation": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference/#%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E5%99%A8%E4%B8%8E%E4%B8%B4%E6%97%B6%E5%88%86%E9%85%8D"
    },
    {
      "depth": 2,
      "text": "增量、非增量与手动 GC",
      "anchor": "增量非增量与手动-gc",
      "citation": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference/#%E5%A2%9E%E9%87%8F%E9%9D%9E%E5%A2%9E%E9%87%8F%E4%B8%8E%E6%89%8B%E5%8A%A8-gc"
    },
    {
      "depth": 2,
      "text": "Camera.main 的当前实现边界",
      "anchor": "cameramain-的当前实现边界",
      "citation": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference/#cameramain-%E7%9A%84%E5%BD%93%E5%89%8D%E5%AE%9E%E7%8E%B0%E8%BE%B9%E7%95%8C"
    },
    {
      "depth": 2,
      "text": "资料共同支持的诊断顺序",
      "anchor": "资料共同支持的诊断顺序",
      "citation": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference/#%E8%B5%84%E6%96%99%E5%85%B1%E5%90%8C%E6%94%AF%E6%8C%81%E7%9A%84%E8%AF%8A%E6%96%AD%E9%A1%BA%E5%BA%8F"
    }
  ],
  "claims": [],
  "outgoing": [
    {
      "id": "note:performance-optimization-cost-frequency-scope",
      "title": "性能优化的本质是控制成本发生的频率、时机与范围",
      "url": "https://www.pystone.net/notes/performance-optimization-cost-frequency-scope/",
      "atlas": "https://www.pystone.net/?node=performance-optimization-cost-frequency-scope#knowledge-atlas",
      "label": "性能优化的本质是控制成本发生的频率、时机与范围",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "核对版本为 Unity 6.0（6000.0），核对日期为 2026-07-23。API 和平台行为可能继续变化；综合应用见性能优化的本质是控制成本发生的频率、时机与范围。",
      "citation": "https://www.pystone.net/notes/unity6-performance-gc-camera-main-reference/#unity-6-%E6%80%A7%E8%83%BD%E6%B5%8B%E9%87%8F%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E4%B8%8E-cameramain-%E5%AE%98%E6%96%B9%E8%B5%84%E6%96%99%E6%91%98%E5%BD%95"
    }
  ],
  "incoming": [
    {
      "id": "note:performance-optimization-cost-frequency-scope",
      "title": "性能优化的本质是控制成本发生的频率、时机与范围",
      "url": "https://www.pystone.net/notes/performance-optimization-cost-frequency-scope/",
      "atlas": "https://www.pystone.net/?node=performance-optimization-cost-frequency-scope#knowledge-atlas",
      "label": "性能优化的本质是控制成本发生的频率、时机与范围",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "本次官方来源中的“Unity 6 性能测量、垃圾回收与 Camera.main 官方资料摘录”导航项",
      "citation": "https://www.pystone.net/notes/performance-optimization-cost-frequency-scope/#%E6%9C%AC%E6%AC%A1%E5%AE%98%E6%96%B9%E6%9D%A5%E6%BA%90"
    },
    {
      "id": "note:sources-excerpts-archive",
      "title": "资料来源、摘录与归档",
      "url": "https://www.pystone.net/notes/sources-excerpts-archive/",
      "atlas": "https://www.pystone.net/?node=sources-excerpts-archive#knowledge-atlas",
      "label": "资料来源、摘录与归档",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "01-外部文章与转载摘录中的“Unity 6 性能测量、垃圾回收与 Camera.main 官方资料摘录”导航项",
      "citation": "https://www.pystone.net/notes/sources-excerpts-archive/#01-%E5%A4%96%E9%83%A8%E6%96%87%E7%AB%A0%E4%B8%8E%E8%BD%AC%E8%BD%BD%E6%91%98%E5%BD%95"
    }
  ],
  "contentMarkdown": "# Unity 6 性能测量、垃圾回收与 Camera.main 官方资料摘录\n\n> [!source] 官方资料摘录\n> 核对版本为 Unity 6.0（6000.0），核对日期为 2026-07-23。API 和平台行为可能继续变化；综合应用见[[10-计算机、信息技术与工程/05-游戏图形与运行时/游戏性能优化/性能优化的本质是控制成本发生的频率、时机与范围|性能优化的本质是控制成本发生的频率、时机与范围]]。\n\n## 在目标平台采集真实数据\n\nUnity 的[目标平台性能采集文档](https://docs.unity3d.com/6000.0/Documentation/Manual/profiling-target-device.html)要求在目标发布平台的 Development Build 上连接 Profiler，可通过局域网、线缆或 IP 连接。它支持一个基础边界：编辑器或开发机上的热点只是诊断线索，发布设备、构建后端与真实场景才决定可外推的性能结论。\n\n文档说明了连接步骤，但没有保证 Development Build 与最终 Release 完全等价；Deep Profiling 本身也可能带来额外开销。因此应先用低侵入采样定位，再用接近发布配置的复测确认。\n\n## 垃圾回收器与临时分配\n\nUnity 6 的[垃圾回收概览](https://docs.unity3d.com/6000.0/Documentation/Manual/performance-garbage-collector.html)说明：托管堆没有足够连续空间满足分配时会触发回收；逐帧临时对象会按频率持续累积待处理工作。文档以每帧 1 KB、60 FPS 为例说明小分配如何在一分钟内累积到 3.6 MB，但这个数字只是算术示例，不是所有项目的性能阈值。\n\n可保留的机制是：分配大小、发生频率、对象存活期和堆状态共同决定回收压力；“0 B/帧”是实时路径的优化方向，不是脱离场景的强制验收线。\n\n## 增量、非增量与手动 GC\n\n[垃圾回收模式文档](https://docs.unity3d.com/6000.0/Documentation/Manual/performance-incremental-garbage-collection.html)说明：\n\n- 增量 GC 默认启用，把标记工作分散到多帧，主要降低单次尖峰，并不会让总回收工作凭空消失；\n- 引用变化过多会增加 write barrier 与重复扫描成本，极端时增量标记可能无法完成并退回完整回收；\n- 非增量模式暂停主线程处理整个托管堆，实时应用可能出现明显 GC spike；\n- 手动禁用 GC 会阻止回收并让堆持续增长，只适合可严格控制分配的短关键区间。\n\n因此，“始终开启增量 GC”与“彻底禁用 GC”都不是普适答案。应观察帧时间分布、引用变化、堆增长和目标平台行为后选择。\n\n## `Camera.main` 的当前实现边界\n\nUnity 6 的[`Camera.main` API](https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Camera-main.html)返回第一个启用且带 `MainCamera` 标签的相机；没有符合条件的组件时返回 `null`。当前实现会缓存带该标签的 GameObject，单次访问仍有与 `GetComponent` 相近的小 CPU 开销，性能敏感处可以自行缓存结果。\n\n这限制了历史经验中“每次调用都全场景搜索”的说法。当前仍不能据文档断言缓存永远值得做：调用频率、场景切换、相机启停和缓存失效风险都需要进入测量。\n\n## 资料共同支持的诊断顺序\n\n1. 在目标平台和代表性场景采样；\n2. 先确认成本是否位于用户可见关键路径及高分位帧；\n3. 对 GC 同时检查分配频率、生命周期、堆增长和回收模式；\n4. 对单个 API 先核对当前版本实现，再决定是否缓存或替换；\n5. 改动后在相同设备与工作负载复测，不沿用旧版本经验数字。\n"
}
