Unity中DrawWireDisc绘制粒子圆与碰撞盒无显示问题求助
Unity绘制粒子圆与碰撞盒线条解决方案
需要的GameObject
- 只需要一个空GameObject(Empty GameObject)即可,用来挂载绘制脚本。空对象不会引入多余的渲染或碰撞组件,完全符合你的需求,不需要带碰撞器的GameObject。
脚本放置位置
- 将脚本挂载到刚才创建的空GameObject上,确保该对象处于激活状态(Inspector面板的复选框勾选)。
- 绘制逻辑要写在
OnDrawGizmos或OnDrawGizmosSelected函数里:OnDrawGizmos:无论是否选中对象,都会持续绘制线条。OnDrawGizmosSelected:只有选中挂载脚本的对象时才会绘制,适合调试时临时查看。
DrawCircle的正确实现&绘制流程
Unity的Gizmos工具没有内置DrawCircle函数,需要自己实现。结合DrawWireCube(做碰撞盒线框),以下是完整可运行的脚本:
using UnityEngine; public class CircleAndBoxDrawer : MonoBehaviour { // 可在Inspector调整的参数 public float circleRadius = 2f; public int circleSegments = 32; // 段数越多,圆越平滑 public Vector2 boxSize = new Vector2(1f, 1f); public Color drawColor = Color.cyan; // 场景/游戏视图通用绘制函数 void OnDrawGizmos() { Gizmos.color = drawColor; // 绘制碰撞盒线框 Gizmos.DrawWireCube(transform.position, new Vector3(boxSize.x, boxSize.y, 0)); // 调用自定义圆形绘制函数 DrawCustomCircle(transform.position, circleRadius, circleSegments); } // 自定义圆形绘制逻辑 void DrawCustomCircle(Vector3 center, float radius, int segments) { float anglePerSegment = 360f / segments; Vector3 lastPoint = center + Quaternion.Euler(0, 0, 0) * Vector3.right * radius; for (int i = 1; i <= segments; i++) { float currentAngle = anglePerSegment * i; Vector3 currentPoint = center + Quaternion.Euler(0, 0, currentAngle) * Vector3.right * radius; Gizmos.DrawLine(lastPoint, currentPoint); lastPoint = currentPoint; } } }
看不到线条的排查要点
- 检查Scene/Game窗口的Gizmos开关是否开启(窗口右上角的Gizmos按钮)。
- 确认挂载脚本的GameObject处于激活状态。
- 检查Inspector面板里的参数:确保
circleRadius、boxSize不为0,circleSegments至少设为8以上保证圆形可见。 - 如果用
OnDrawGizmosSelected,需要选中挂载脚本的对象才能看到线条。
内容的提问来源于stack exchange,提问作者Robin
相关产品推荐
相关产品推荐

