How to draw Handles transparent occluded by geometry
I've build a custom Editor for Setting Up specific Systems in our game world. I want my Handles to be transparent when occluded by any objects (especially my colliders to see if they intersect with the terrain).
Right now they're just drawn on top of erverything else, whereas Unity's BoxColliderEditor for example switches to draw transparent lines when the collider is stuck in or hidden by any geometry.
My OnSceneGUI is registered to SceneView.onSceneGUIDelegate and within that i call DrawCollider():
private void DrawCollider(BoxCollider col, bool selected) {
Color clr = Handles.color;
if (selected) {
Handles.color = Color.magenta;
}
Vector3 center = col.center;
Vector3 size = col.size;
Vector3 minPos = center - size * 0.5f;
Vector3 maxPos = center + size * 0.5f;
Matrix4x4 matrix = Handles.matrix;
Handles.matrix = Matrix4x4.TRS(col.transform.position, col.transform.rotation, Vector3.one);
//Draw Box
Handles.DrawWireCube(center, size);
if (selected) {
Vector3 topRight = this.DrawMovementHandler(new Vector3(maxPos.x, center.y + size.y * 0.5f, maxPos.z));
Vector3 botLeft = this.DrawMovementHandler(new Vector3(minPos.x, center.y - size.y * 0.5f, minPos.z));
if (topRight.z > botLeft.z && topRight.x > botLeft.x && topRight.y > botLeft.y) {
col.center = (topRight + botLeft) * 0.5f;
col.size = topRight - botLeft;
}
}
Handles.matrix = matrix;
Handles.color = clr;
}
I just don't understand how Unity's BoxColliders are drawn differently. As far as i figured out Unity is also just calling Handles.DrawWireCube internally in it's OnSceneGUI Method (from BoxEditor.cs):
Color color = Handles.color;
Handles.color = boxColor;
Vector3 minPos = center - size * 0.5f;
Vector3 maxPos = center + size * 0.5f;
Matrix4x4 matrix = Handles.matrix;
Handles.matrix = transform;
int hotControl = GUIUtility.hotControl;
if (!handlesOnly)
Handles.DrawWireCube(center, size);
Vector3 point = transform.inverse.MultiplyPoint(Camera.current.transform.position);
bool isCameraInsideBox = new Bounds(center, size).Contains(point);
Handles.color = midPointHandleColor;
this.MidpointHandles(ref minPos, ref maxPos, Handles.matrix, isCameraInsideBox);
if (hotControl != GUIUtility.hotControl && GUIUtility.hotControl != 0)
this.m_HandleControlID = GUIUtility.hotControl;
bool changed = GUI.changed;
if (changed)
{
center = (maxPos + minPos) * 0.5f;
size = maxPos - minPos;
}
Handles.color = color;
Handles.matrix = matrix;
So i was just wondering where the magic happens and how the depth value is conscidered for drawing these handles.
Unity Box: 
My Box: 
Your answer
Follow this Question
Related Questions
Progrid with custom handle 1 Answer
How to fix Emission maps that aren't cooperating with other maps? 0 Answers
Do Unity Editor GUI Utilities (Handles.DrawLine & EditorGUI.DrawRect) have limitations? 2 Answers
UnityEvent referens to custom editor from inside array of class and editor losing settings 1 Answer
How can I color a PrefixLabel? 0 Answers