position | 立方体的坐标。 |
rotation | 立方体的旋转。 |
size | 立方体的大小。 |
float 从鼠标到立方体的距离(以像素为单位)。
返回鼠标指针到立方体的距离(以像素为单位)。
计算从鼠标指针到给定世界空间position
、给定rotation
和size
的立方体的屏幕空间距离。
当鼠标指针直接位于立方体上方时,返回零。
使用当前摄像机来确定距离。
using UnityEngine; using UnityEditor;
public class ExampleScript : MonoBehaviour { }
// Displays cube in scene view, and distance from mouse // to the cube. [CustomEditor(typeof(ExampleScript))] public class ExampleEditor : Editor { public void OnSceneGUI() { var t = target as ExampleScript; var tr = t.transform; var position = tr.position; var rotation = tr.rotation; var size = 1.0f; // draw a cube in scene Handles.color = Color.yellow; Handles.CubeHandleCap(0, position, rotation, size, Event.current.type); // calculate distance from mouse to cube, and display it var distance = HandleUtility.DistanceToCube(position, rotation, size); GUI.color = Color.black; Handles.Label(position, distance.ToString("F0")); // make scene view repaint on mouse move if (Event.current.type == EventType.MouseMove) Event.current.Use(); } }