Created
August 12, 2024 14:17
-
-
Save SolarianZ/9cc9401a5e67cdc57f8c28a750b29ee5 to your computer and use it in GitHub Desktop.
{"category": "Unity Engine/Editor/Sample/Handles", "keywords": "Unity, Editor, Handles, Scene, Pickable, Clickable"} An example of drawing clickable lines (Handles) in the SceneView.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEditor; | |
| using UnityEngine; | |
| public class PickableHandleSample : MonoBehaviour | |
| { | |
| public Vector3 startPoint = Vector3.zero; | |
| public Vector3 endPoint = Vector3.up * 3; | |
| } | |
| [CustomEditor(typeof(PickableHandleSample))] | |
| public class PickableHandleSampleEditor : Editor | |
| { | |
| private void OnSceneGUI() | |
| { | |
| PickableHandleSample pickableHandleSample = (PickableHandleSample)target; | |
| Vector3 startPoint = pickableHandleSample.startPoint; | |
| Vector3 endPoint = pickableHandleSample.endPoint; | |
| Handles.color = Color.yellow; | |
| // Check mouse hover | |
| float mouseToLineDist = HandleUtility.DistanceToLine(startPoint, endPoint); | |
| bool isHovering = mouseToLineDist < 5; | |
| if (isHovering) | |
| { | |
| Handles.DrawAAPolyLine(5, startPoint, endPoint); | |
| } | |
| else | |
| { | |
| Handles.DrawLine(startPoint, endPoint); | |
| } | |
| // Check mouse click | |
| Event evt = Event.current; | |
| if (isHovering && evt.type == EventType.MouseUp && evt.button == 0) | |
| { | |
| Debug.Log($"Line clicked: {startPoint}<->{endPoint}"); | |
| evt.Use(); | |
| } | |
| // Reselect the currently processed control to avoid clicking on other objects in the Scene | |
| HandleUtility.AddControl(GUIUtility.hotControl, mouseToLineDist); | |
| // Repaint the SceneView to avoid the delay in updating the thickness of the lines above | |
| SceneView.RepaintAll(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment