{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/uwa-answer-optimize-frequent-set-active-ui/",
  "atlas": "https://www.pystone.net/?node=uwa-answer-optimize-frequent-set-active-ui#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/uwa-answer-optimize-frequent-set-active-ui.md",
  "context": "https://www.pystone.net/notes/uwa-answer-optimize-frequent-set-active-ui.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:uwa-answer-optimize-frequent-set-active-ui",
  "slug": "uwa-answer-optimize-frequent-set-active-ui",
  "title": "如何优化UI中大量使用SetActive(true)/(false)",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "石心.Yong"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "如何优化UI中大量使用SetActive(true)/(false)",
  "contentRole": "source",
  "sourceType": "source",
  "sourceMetadata": {
    "author": "石心.Yong"
  },
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "external_source",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "9b33feffa44d3b25132fedcebd02dd62dac41ff48bf4eba0e9f82538757b3594",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "如何优化UI中大量使用SetActive(true)/(false)",
      "anchor": "如何优化ui中大量使用setactivetruefalse",
      "citation": "https://www.pystone.net/notes/uwa-answer-optimize-frequent-set-active-ui/#%E5%A6%82%E4%BD%95%E4%BC%98%E5%8C%96ui%E4%B8%AD%E5%A4%A7%E9%87%8F%E4%BD%BF%E7%94%A8setactivetruefalse"
    },
    {
      "depth": 2,
      "text": "问题",
      "anchor": "问题",
      "citation": "https://www.pystone.net/notes/uwa-answer-optimize-frequent-set-active-ui/#%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 2,
      "text": "我的回答",
      "anchor": "我的回答",
      "citation": "https://www.pystone.net/notes/uwa-answer-optimize-frequent-set-active-ui/#%E6%88%91%E7%9A%84%E5%9B%9E%E7%AD%94"
    },
    {
      "depth": 2,
      "text": "来源与关系",
      "anchor": "来源与关系",
      "citation": "https://www.pystone.net/notes/uwa-answer-optimize-frequent-set-active-ui/#%E6%9D%A5%E6%BA%90%E4%B8%8E%E5%85%B3%E7%B3%BB"
    }
  ],
  "claims": [],
  "outgoing": [
    {
      "id": "note:my-uwa-answers",
      "title": "我的 UWA 问答",
      "url": "https://www.pystone.net/notes/my-uwa-answers/",
      "atlas": "https://www.pystone.net/?node=my-uwa-answers#knowledge-atlas",
      "label": "我的 UWA 问答",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "上位集合：我的 UWA 问答",
      "citation": "https://www.pystone.net/notes/uwa-answer-optimize-frequent-set-active-ui/#%E6%9D%A5%E6%BA%90%E4%B8%8E%E5%85%B3%E7%B3%BB"
    }
  ],
  "incoming": [
    {
      "id": "note:my-uwa-answers",
      "title": "我的 UWA 问答",
      "url": "https://www.pystone.net/notes/my-uwa-answers/",
      "atlas": "https://www.pystone.net/?node=my-uwa-answers#knowledge-atlas",
      "label": "我的 UWA 问答",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "2021-03-26：如何优化UI中大量使用SetActive(true)/(false)（UI、资源管理、UGUI、终端平台）",
      "citation": "https://www.pystone.net/notes/my-uwa-answers/#2021"
    },
    {
      "id": "note:game-graphics-and-runtime",
      "title": "游戏图形与运行时",
      "url": "https://www.pystone.net/notes/game-graphics-and-runtime/",
      "atlas": "https://www.pystone.net/?node=game-graphics-and-runtime#knowledge-atlas",
      "label": "游戏图形与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "我的UWA问答中的“2021-03-26 - 如何优化UI中大量使用SetActive(true)-(false)”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#%E6%88%91%E7%9A%84uwa%E9%97%AE%E7%AD%94"
    }
  ],
  "contentMarkdown": "# 如何优化UI中大量使用SetActive(true)/(false)\n\n> [!note] 我的 UWA 问答\n> 本文收录我在 UWA 问答发表的公开回答。问题内容归原提问者；回答保留当时语境，技术结论可能受 Unity 版本、平台和项目条件限制。\n\n## 问题\n\n在UI的日常开发中为了显示隐藏某些GO使用了大量的SetActive(true)/(false)代码,由于SetActive的开销过大是否有其他解决方案\n\n- 提问者：zyy\n- 提问时间：2021-03-26 00:32:32\n\n## 我的回答\n\n这种情况下Active/Deactive的开销主要有两个方面：\n\n- C#层到Native层的穿梭调用速度比C#层内的速度慢。\n- UI元素的变化导致所在的Canvas变化，触发函数Canvas.SendWillRenderCanvases()与Canvas.BuildBatch()造成高耗时。\n\n因此，优化也可以从以下几点考虑：\n\n1. 在C#层设置变量来标识相应的GO处于Active还是非Active状态，避免对Active的对象进行SetActive(true)，避免对非Active的对象进行SetActive(false)。\n2. 将要频繁变化的UI元素与不频繁变化的UI元素放在不同的Canvas中，减少UI元素变化时的耗时。\n3. 通过将UI元素的坐标移动到Canvas的范围之外的方法来显示与隐藏，避免SetActive的耗时以及SendWillRenderCanvases的耗时\n4. 另外，经测试，对Component进行 enabled = false 的操作比对GO进行SetActive(false)的操作耗时低。\n\n--------------------\n\n2021.3.27 更新：\n\n经题主提示，导致开销的还有一个方面：UI元素的网格顶点数组改变会造成堆内存分配，触发GC，导致耗时。不过对UI元素进行位置移动不会造成堆内存分配。\n\n优化方法增加一条：通过添加CanvasGroup组件设置透明度的方式来进行显示与隐藏。\n\n- 回答时间：2021-03-26 02:13:30\n- 最后更新：2021-03-29 02:52:36\n- 采纳状态：未采纳\n\n## 来源与关系\n\n- 上位集合：[[10-计算机、信息技术与工程/05-游戏图形与运行时/我的UWA问答/我的UWA问答|我的 UWA 问答]]\n- UWA 标签：UI、资源管理、UGUI、终端平台\n- 原始问题：[如何优化UI中大量使用SetActive(true)/(false)](https://answer.uwa4d.com/question/605c4aa0cfa35d5b53669c57)\n- 源页最后更新：2021-03-29 03:06:51\n"
}
