- Home /
View the distance an object has from the camera
Can I view the distance an object has from the main camera through the Unity editor UI somewhere?
I don't think that by default there is a feature in Unity for this, however, it should not be difficult to write an editor script that does this. I will try to come up with one quickly.
Answer by Harinezumi · May 22, 2018 at 09:16 AM
A different solution using EditorWindow (need to have the custom editor window open for it to work):
using UnityEngine;
using UnityEditor;
public class DistanceFromCamera : EditorWindow {
private GUIStyle style;
[MenuItem("Tools/Distance window")]
private static void Init () {
DistanceFromCamera window = GetWindow<DistanceFromCamera>();
SceneView.onSceneGUIDelegate += window.OnSceneGUI;
window.Setup();
}
private void Setup () {
style = new GUIStyle();
style.normal.textColor = Color.red;
style.fontSize = 16;
style.alignment = TextAnchor.MiddleCenter;
}
private void OnDestroy () {
SceneView.onSceneGUIDelegate -= OnSceneGUI;
}
private void OnSceneGUI (SceneView sceneView) {
string distanceString = "N/A";
Camera mainCamera = Camera.main;
if (mainCamera != null) {
GameObject[] selectedObjects = Selection.gameObjects;
if (selectedObjects.Length == 1) {
GameObject selectedObject = selectedObjects[0];
float distance = (selectedObject.transform.position - mainCamera.transform.position).magnitude;
distanceString = distance.ToString();
}
}
Handles.Label(Selection.gameObjects[0].transform.position, distanceString, style);
}
}
Answer by Hellium · May 22, 2018 at 08:14 AM
Here is a custom editor script drawing a line between the currently selected object and the camera. Place this script in an Editor folder.
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(GameObject))]
public class DistanceFromCameraEditor : Editor
{
[DrawGizmo(GizmoType.Selected)]
static void RenderCustomGizmo(Transform objectTransform, GizmoType gizmoType)
{
Camera camera = Camera.main ;
if( camera == null )
camera = FindObjectOfType<Camera>();
if( camera == null )
return ;
Vector3 midPoint = (objectTransform.position + camera.transform.position) * 0.5f ;
float distance = Vector3.Distance( objectTransform.position, camera.transform.position );
Gizmos.DrawLine(objectTransform.position, camera.transform.position);
Handles.Label(midPoint, distance.ToString());
}
}
For me this script changes how the inspector looks (can't upload files for some reason, so here is a link): https://ibb.co/kAN$$anonymous$$$$anonymous$$T .
Do you know why this happens? I tried this in Unity 2017.3.1f1.
You are right I hadn't noticed this. It also happens to me in Unity 2018.1.0b13. I have no idea why.
Did not notice that, indeed.
Because there already is a custom inspector of the GameObject type, this class overrides the custom inspector. And it seems, it's not possible to inherit from this class defined as internal....
You should go with @Harinezumi 's solution
Identical problem here: https://answers.unity.com/questions/343984/custom-editor-makes-inspector-view-act-weirdly.html
Your answer