{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/uwa-answer-implement-mesh-renderer-effect/",
  "atlas": "https://www.pystone.net/?node=uwa-answer-implement-mesh-renderer-effect#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/uwa-answer-implement-mesh-renderer-effect.md",
  "context": "https://www.pystone.net/notes/uwa-answer-implement-mesh-renderer-effect.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:uwa-answer-implement-mesh-renderer-effect",
  "slug": "uwa-answer-implement-mesh-renderer-effect",
  "title": "自己如何实现MeshRender 的效果",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "石心.Yong"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "自己如何实现MeshRender 的效果",
  "contentRole": "source",
  "sourceType": "source",
  "sourceMetadata": {
    "author": "石心.Yong"
  },
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "external_source",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "1e5fafa4dd6a698ba34f1fce6d8bb70912481e8a145fd38a90186fffc20f88fe",
  "assets": [
    {
      "reference": "assets/6096bc126bb31032f979157b/01.png",
      "url": "/media/60d7add76896576fd8f0.png",
      "mediaType": "image/png",
      "contentHash": "60d7add76896576fd8f00c59f07813d45c8af9b2cd5e668c415bcef79ff28ba1",
      "byteLength": 54005,
      "width": 813,
      "height": 321
    },
    {
      "reference": "assets/6096bc126bb31032f979157b/02.png",
      "url": "/media/8681d448b8167adf31f8.png",
      "mediaType": "image/png",
      "contentHash": "8681d448b8167adf31f8a1926813207a0473cd35344f9ab1f554e52f106d9d16",
      "byteLength": 149060,
      "width": 939,
      "height": 549
    },
    {
      "reference": "assets/6096bc126bb31032f979157b/03.png",
      "url": "/media/fe5a93558545645ff27d.png",
      "mediaType": "image/png",
      "contentHash": "fe5a93558545645ff27d5dc93c0960fe7e4868628d4329f8294a5bd972f27bd2",
      "byteLength": 74671,
      "width": 1116,
      "height": 389
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "自己如何实现MeshRender 的效果",
      "anchor": "自己如何实现meshrender-的效果",
      "citation": "https://www.pystone.net/notes/uwa-answer-implement-mesh-renderer-effect/#%E8%87%AA%E5%B7%B1%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0meshrender-%E7%9A%84%E6%95%88%E6%9E%9C"
    },
    {
      "depth": 2,
      "text": "问题",
      "anchor": "问题",
      "citation": "https://www.pystone.net/notes/uwa-answer-implement-mesh-renderer-effect/#%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 2,
      "text": "我的回答",
      "anchor": "我的回答",
      "citation": "https://www.pystone.net/notes/uwa-answer-implement-mesh-renderer-effect/#%E6%88%91%E7%9A%84%E5%9B%9E%E7%AD%94"
    },
    {
      "depth": 2,
      "text": "来源与关系",
      "anchor": "来源与关系",
      "citation": "https://www.pystone.net/notes/uwa-answer-implement-mesh-renderer-effect/#%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-implement-mesh-renderer-effect/#%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-05-09：自己如何实现MeshRender 的效果（引擎技术、资源管理、动画模块、渲染模块）",
      "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-05-09 - 自己如何实现MeshRender 的效果”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#%E6%88%91%E7%9A%84uwa%E9%97%AE%E7%AD%94"
    }
  ],
  "contentMarkdown": "# 自己如何实现MeshRender 的效果\n\n> [!note] 我的 UWA 问答\n> 本文收录我在 UWA 问答发表的公开回答。问题内容归原提问者；回答保留当时语境，技术结论可能受 Unity 版本、平台和项目条件限制。\n\n## 问题\n\n我有个需求就是 关卡里的对象 是通过脚本 间接创建的，而不是直接将prefab 拖到场景中。\n\n因此我需要能象 MeshRender 那样直接在场景中显示 并方便我选中与编辑。\n\n代码类似这样：\n\n```\npublic class SpawnPrefab : MonoBehaviour\n\n    {\n\n        public GameObject prefabEntity;\n\n         void Start()\n\n         {\n\n              Instantiate(prefabEntity);\n\n        }\n\n    }\n```\n\nMeshRender 有下面两个特性：\n\n1. 在Editor 模式下 SceneView 与 GameView 下面同时显示\n2. 在运行模式下绘制出的对象可以在 SceneView 下选中\n\n目前我是在OnDrawGizmos 函数中调用 Graphics.DrawMesh 但Scene View中却无法选中，\n\n在Update中绘制 却在 Editor 模式下无法 显示。 我感觉好像尝试的方向错了。\n\n- 提问者：silekey\n- 提问时间：2021-05-09 08:28:02\n\n## 我的回答\n\nOnDrawGizmos里面绘制显然是不行的，这个函数在打包出来Runtime都不会调用。\n\n要在SceneView中选中编辑，一定得是场景中的某个GameObject。\n\n（用GameObject挂MeshRenderer肯定能满足 “SceneView编辑” 和 “运行时脚本创建” 两个需求的。）\n\n如果题主的需求是，一定要用Graphics的立即渲染接口来画Mesh的话。如果要在Editor下显示，给类加一个ExecuteAlways属性就可以了，只是还不能在SceneView中选中编辑。\n\n![image.png](assets/6096bc126bb31032f979157b/01.png)\n\n如果还想实现Scene窗口下编辑的功能，只能给要绘制的Mesh绑定一个场景中的挂了MeshRenderer的GameObject了。Graphics.DrawMesh绘制的物体的transform与这个GameObject保持一致就行了。如下图，Game窗口为OnPostRender函数中用Graphics.DrawMeshNow函数绘制的效果，Scene窗口中为绑定的用来编辑调整的物体，两者的transform是一致的：\n\n![image.png](assets/6096bc126bb31032f979157b/02.png)\n\n关键代码如下：\n\n![image.png](assets/6096bc126bb31032f979157b/03.png)\n\n- 回答时间：2021-05-09 10:46:29\n- 最后更新：2021-05-09 11:18:06\n- 采纳状态：已采纳\n\n## 来源与关系\n\n- 上位集合：[[10-计算机、信息技术与工程/05-游戏图形与运行时/我的UWA问答/我的UWA问答|我的 UWA 问答]]\n- UWA 标签：引擎技术、资源管理、动画模块、渲染模块\n- 原始问题：[自己如何实现MeshRender 的效果](https://answer.uwa4d.com/question/6096bc126bb31032f979157b)\n- 源页最后更新：2021-05-11 02:12:23\n"
}
