Unity Scene编辑器中如何始终显示相机视锥体线条(无需选中相机)
Unity 6.2 持续显示相机视锥体的解决方案
问题描述
我正在开发一个Unity项目,需要持续监控主相机的视角。默认情况下,相机视锥体的白色线条仅在Hierarchy面板中选中相机GameObject时,才会在Scene窗口中显示。是否有办法让这些线条始终可见,即使我在场景中操作其他对象?我已尝试检查Gizmos菜单,但似乎没有强制特定对象Gizmo始终开启的选项。目标是无需点击相机对象就能随时查看相机的取景范围。
解决方案
通过自定义Gizmo脚本,可实现相机视锥体在Scene窗口中始终显示,不受对象选中状态影响:
using UnityEngine; #if UNITY_EDITOR using UnityEditor; public class ShowCamFrustum : MonoBehaviour { private Camera _camera; void OnDrawGizmos() { if (!_camera) _camera = GetComponent<Camera>(); if (!_camera) return; Gizmos.color = Color.white; Matrix4x4 temp = Gizmos.matrix; Gizmos.matrix = _camera.transform.localToWorldMatrix; if (_camera.orthographic) { float spread = _camera.farClipPlane - _camera.nearClipPlane; float center = (_camera.farClipPlane + _camera.nearClipPlane) * 0.5f; Gizmos.DrawWireCube(new Vector3(0, 0, center), new Vector3(_camera.orthographicSize * 2 * _camera.aspect, _camera.orthographicSize * 2, spread)); } else { Gizmos.DrawFrustum(Vector3.zero, _camera.fieldOfView, _camera.farClipPlane, _camera.nearClipPlane, _camera.aspect); } Gizmos.matrix = temp; } } #endif
使用步骤
- 在Unity编辑器中新建C#脚本,命名为
ShowCamFrustum - 将上述代码粘贴到脚本文件中
- 将脚本挂载到需要持续显示视锥体的相机GameObject上
- 完成后,无论是否选中相机,Scene窗口都会始终显示该相机的视锥体线条
内容的提问来源于stack exchange,提问作者serhat aktas
相关产品推荐
相关产品推荐

