How do I go about ensuring, say, Gizmos.DrawWireCube is a fixed size relative to the viewport camera?
I’m not sure what you want to do; gizmos are drawn in world space, so their size is going to change relative to how far away they are. If you want a gizmo to appear the same size on screen regardless of where it is in world space, you’ll need to how far away the camera is and scale the gizmo accordingly.
I know this is REALLY old, but it’s still the first thing that comes up in Google when searching for screen size gizmos.
HandleUtility.GetHandleSize(worldPos) does exactly what you want, but unfortunately it’s only available in the editor…
But then I realized I could just use ILSpy to check out what GetHandleSize does, and rewrote it to use Gizmos matrix, and now it works great. So here it is:
public static float GetGizmoSize(Vector3 position)
{
Camera current = Camera.current;
position = Gizmos.matrix.MultiplyPoint(position);
if (current)
{
Transform transform = current.transform;
Vector3 position2 = transform.position;
float z = Vector3.Dot(position - position2, transform.TransformDirection(new Vector3(0f, 0f, 1f)));
Vector3 a = current.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(0f, 0f, z)));
Vector3 b = current.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(1f, 0f, z)));
float magnitude = (a - b).magnitude;
return 80f / Mathf.Max(magnitude, 0.0001f);
}
return 20f;
}
I have made a tutorial article on how to scale gizmo objects depending on the camera distance in the editor:
Thanks.