UI GameObject可以在Scene View中显示,但是在Game View不能显示

出现UI GameObject可以在Scene View中显示,但是在Game View不能显示这种问题,可能有很多种原因,例如Layer设定、Camera Clipping设定、font assets等问题。 对于TextMeshPro,还有Material Set的问题,见这篇文章

而我这次遇到的,是因为我没有把UI GameObject放到UI Canvas上面。如下图所示,我的游戏的界面是在左下角很小的部分,而Canvas很大,但是没有完全覆盖住我的游戏界面。当我把UI GameObject放在了我的游戏界面,但是没有在Canvas里面的时候,UI就不会显示。
在这里插入图片描述
解决办法是,修改Canvas的Render mode: 从默认的Screen Space-Overlay (即上图展示的Canvas,无法修改位置和大小)修改至World Space。其他的部分(例如游戏界面和代码逻辑)均无须修改,就可以了。

在这里插入图片描述

关于Unity中的troubleshooting

在Unity中,当一个功能没有像预期的一样正常运行时,找到原因是很难的一件事情,因为有很多地方可能导致了没有正常运行。对于初学者而言,则更难,因为都不知道有哪些地方可能是原因。

除了多多积累之外,一种troubleshooting的思路是,“对比实验”。即,想办法找到正常工作的情况,然后将自己的异常情况和正常情况对比,看看哪里设置不一样。例如,在解决UI无法正常显示这个问题时,我就是试了很多改变,例如在新的Scene里面测试UI是否能正常显示、将某些GameObject Disable等等,然后找到了原因:原来是UI没有放进Canvas。

Canvas的三种Render Mode

(The following content is created by a LLM.)

Screen Space - Overlay: This is the default render mode for Canvas. In this mode, the UI elements are rendered on top of everything else in the scene, akin to an overlay. The Canvas is always the same size as the screen and it doesn’t move or rotate, so UI elements maintain the same size and position regardless of camera position or settings. This mode is best used for UI elements that need to always be visible and in the same place, such as health bars, score displays, or menus.

Screen Space - Camera: In this mode, the Canvas behaves similarly to Screen Space - Overlay, but the UI elements are rendered at a certain distance from a specified camera. They will appear to move, rotate, and scale with the camera, and they can be obscured by other objects in the scene based on their Z position. This mode is useful if you want to have UI elements that are attached to a specific camera, like a heads-up display (HUD) that moves with a player’s first-person perspective.

World Space: In this mode, the Canvas behaves like any other object in the scene. It can be moved, rotated, and scaled freely, and UI elements can interact with lights and be affected by perspective. This mode is useful when you want to integrate UI elements into the game world itself, like text bubbles over a character’s head or interactive buttons within the environment.

The appropriate mode to use depends on the needs of your game. If you have UI elements that need to be always visible and don’t interact with the game world, use Screen Space - Overlay. If you have UI elements that are attached to a specific camera view, use Screen Space - Camera. And if you have UI elements that are integrated into the game world, use World Space.

Unity的坐标系统、Tilemaps and Canvas

(The following content is created by a LLM.)

Unity’s Coordinate System: Unity uses a left-handed coordinate system. The center of the world is at (0, 0, 0). In a 2D game, positive X values go to the right, positive Y values go up, and positive Z values come out of the screen.

Tilemaps: Unity’s Tilemap system creates 2D tile-based environments. The Tilemap’s position refers to the cell at the origin (0, 0) of its grid.

Canvas and TextMeshPro: In Unity, all UI elements must be children of a Canvas. If you’re placing a TextMeshPro element in a 2D game world (and not as an overlay on the screen), you’ll want to use a Canvas in “World Space” mode. This makes the Canvas act like any other object in the world, so you can place it anywhere, and its children will follow its position, rotation, and scale.

### 可能的原因分析 在Unity开发过程中,如果Game视图出现空白或者无显示的情况,通常可能是由以下几个原因引起的: 1. **场景中缺少摄像机组件** 如果当前活动的Scene中没有设置任何Camera对象作为Main Camera,那么Game视图将无法正常渲染画面[^1]。 2. **摄像机配置错误** 即使存在Camera对象,但如果其`Target Display`属性被错误地设置为其他显示器编号(例如Display 2),而项目运行环境中并未连接额外的显示器,则可能导致Game视图为空白状态[^3]。 3. **材质或Shader问题** 场景中的物体可能使用了兼容的Material或自定义Shader,在某些情况下这可能会导致渲染失败。特别是当使用复杂效果如ASE工具、ShaderGraph时更容易出现问题[^4]。 4. **脚本逻辑异常** 若有脚本抛出了严重错误(比如NullReferenceException),也可能间接影响到整个游戏画布的表现形式。 5. **性能优化选项干扰** 对于较旧版本的Unity引擎来说(如引用提到的是2019.x系列),开启特定图形质量设定之后会出现类似“残影”的现象, 这实际上是因为帧缓冲区未及时清理所造成的结果[^2]. --- ### 解决方案建议 #### 方法一:检查并修复摄像机组件 确认是否有有效的Camera存在于Hierarchy面板下,并将其标记为主相机(Main Camera);同时调整好它的Transform参数以便能够覆盖住目标区域内的所有模型物件。 ```csharp // 示例代码用于动态创建默认摄像头 using UnityEngine; public class EnsureCamera : MonoBehaviour { void Start() { if(Camera.main == null){ GameObject camObj = new GameObject("Default Main Camera"); camObj.AddComponent<Camera>(); camObj.GetComponent<Camera>().orthographic = false; camObj.transform.position = Vector3.zero; camObj.transform.rotation = Quaternion.identity; } } } ``` #### 方法二:排查脚本执行期间产生的潜在崩溃点 通过阅读控制台日志定位具体哪个部分引发了上述提及的那种NullPointerException类型的报错信息,进而针对性修改源码消除隐患。 #### 方法三:验证使用的资源文件是否完好无损 重新导入那些怀疑有问题的贴图纹理或者是预制体资产,确保它们都能正确载进来而且没有任何损坏迹象。 #### 方法四:尝试切换同的Render Mode给UI Canvas试试看 有时候Screen Space - Overlay模式下的表现跟其它两种方式相比会有差异,可以考虑改成World Space看看是否会有所改善。 #### 方法五:更新至最新稳定版引擎程序 鉴于原始提问者正在使用的还是相对比较老的一个分支号(即v2019.4.26f1),强烈推荐升级成更接近当下时间线的新发行包来获取更好的技术支持和服务保障。 --- ### 总结说明 综上所述,针对Unity Game View呈现一片空白的现象可以从多个角度入手逐一排除可能性直到找到确切根源为止。以上列举了几种常见的诱因及其对应的处理措施供参考借鉴。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值