public void Layout(string[] guiInfo) { int controlId = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive); if (Event.current.type == EventType.Layout) { HandleUtility.AddDefaultControl(controlId); } // examples about how to show ui info // note: Handles.BeginGUI and EndGUI are important, otherwise the default gizmos aren't drawn Handles.BeginGUI(); /* disabling this, never needed it so far * if (raycastHitValid) * { * ShowHandleInfo(raycastHit.point); * } */ PrefabPainterEditor.ShowGuiInfo(guiInfo); Handles.EndGUI(); }
// About the position hanlde see example https://docs.unity3d.com/ScriptReference/Handles.PositionHandle.html public void OnSceneGUI() { int controlId = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive); Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity)) { mousePos = hit.point; mousePosValid = true; /// /// process mouse & keyboard events /// //if (EditorWindow.focusedWindow) { switch (Event.current.type) { case EventType.KeyDown: { // toggle add mode if (Event.current.shift && Event.current.keyCode == KeyCode.A) { // toggle add mode SplineSettings.AttachMode selectedAttachMode = gizmo.splineSettings.attachMode; /* * SplineSettings.AttachMode selectedAttachMode = (SplineSettings.AttachMode)System.Enum.GetValues(typeof(SplineSettings.AttachMode)).GetValue(attachMode.enumValueIndex); * * int boundsIndex = ArrayUtility.IndexOf((SplineSettings.AttachMode[])System.Enum.GetValues(typeof(SplineSettings.AttachMode)), SplineSettings.AttachMode.Bounds); * int betweenIndex = ArrayUtility.IndexOf((SplineSettings.AttachMode[])System.Enum.GetValues(typeof(SplineSettings.AttachMode)), SplineSettings.AttachMode.Between); */ if (selectedAttachMode == SplineSettings.AttachMode.Bounds) { gizmo.splineSettings.attachMode = SplineSettings.AttachMode.Between; } else { gizmo.splineSettings.attachMode = SplineSettings.AttachMode.Bounds; } // trigger repaint, so that the enumpopup will be updated editor.Repaint(); } ; break; } } } // control key pressed if (Event.current.shift) { mousePos = hit.point; mousePosValid = true; // shift + ctrl = delete bool deleteMode = Event.current.control; int addControlPointIndex = FindClosestControlPoint(mousePos); //Handles.DrawSphere(controlId, mousePos, Quaternion.identity, 0.3f); Color handleColor; if (deleteMode) { handleColor = Color.red; // red = delete } // draw attachment line else { handleColor = Color.gray; // gray = add } // draw gray circle float radius = 0.1f; Handles.color = handleColor; Handles.DrawWireDisc(mousePos, hit.normal, radius); // draw line to closest point if (gizmo.splineSettings.controlPoints.Count > 0) { // draw indicator line to closest control point Vector3 lineStartPosition = gizmo.splineSettings.controlPoints.ElementAt(addControlPointIndex).position; Vector3 lineEndPosition = mousePos; Handles.DrawLine(lineStartPosition, lineEndPosition); // draw additional indicator line to the control point which is next in the list after the closest control point if (!deleteMode) { int neighbourIndex; if (addControlPointIndex > 0) { neighbourIndex = addControlPointIndex - 1; } else { neighbourIndex = addControlPointIndex + 1; } if (gizmo.splineSettings.attachMode == SplineSettings.AttachMode.Between && neighbourIndex >= 0 && neighbourIndex <= gizmo.splineSettings.controlPoints.Count - 1) { Vector3 neighbourLineStartPosition = gizmo.splineSettings.controlPoints.ElementAt(neighbourIndex).position; Vector3 neighbourLineEndPosition = mousePos; Handles.DrawLine(neighbourLineStartPosition, neighbourLineEndPosition); } } } if (Event.current.type == EventType.MouseDown) { // delete node if (deleteMode) { bool canDelete = gizmo.splineSettings.controlPoints.Count > 0; if (Event.current.button == 0 && canDelete) { Vector3 mousePosition = new Vector3(mousePos.x, mousePos.y, mousePos.z); int controlPointIndex = FindClosestControlPoint(mousePosition); // remove the closest point if (controlPointIndex != -1) { RemoveControlPoint(controlPointIndex); } Event.current.Use(); } } // add new node else { // left button = 0; right = 1; middle = 2 if (Event.current.button == 0) { Vector3 position = new Vector3(mousePos.x, mousePos.y, mousePos.z); AddControlPoint(position, addControlPointIndex); Event.current.Use(); } } } } } else { mousePosValid = false; } if (Event.current.type == EventType.Layout) { HandleUtility.AddDefaultControl(controlId); } DrawSplineGizmos(); // create gameobjects if (mousePosValid) { PerformEditorAction(); } // show info Handles.BeginGUI(); string[] info = new string[] { "Add Control Point: shift + click", "Remove control point: shift + ctrl + click", "Change Attach Mode: shift + A, Current: " + gizmo.splineSettings.attachMode }; PrefabPainterEditor.ShowGuiInfo(info); Handles.EndGUI(); }
public void OnSceneGUI() { bool mousePosValid = false; Vector3 mousePos = Vector3.zero; float radius = gizmo.brushSettings.brushSize / 2f; int controlId = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive); Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; // TODO: raycast hit against layer // see https://docs.unity3d.com/ScriptReference/Physics.Raycast.html if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity)) { mousePos = hit.point; mousePosValid = true; /// /// process mouse events /// // control key pressed if (Event.current.control) { // mouse wheel up/down changes the radius if (Event.current.type == EventType.ScrollWheel) { // ctrl + shift + scroll = brush rotation if (Event.current.shift) { int rotationStepSize = 10; int rotationMin = 0; // TODO: find out of to get that from Range int rotationMax = 360; // TODO: find out of to get that from Range // scroll up if (Event.current.delta.y > 0) { gizmo.brushSettings.brushRotation += rotationStepSize; if (gizmo.brushSettings.brushRotation > rotationMax) { gizmo.brushSettings.brushRotation = rotationMin + rotationStepSize; } Event.current.Use(); } // scroll down else if (Event.current.delta.y < 0) { gizmo.brushSettings.brushRotation -= rotationStepSize; if (gizmo.brushSettings.brushRotation < rotationMin) { gizmo.brushSettings.brushRotation = rotationMax - rotationStepSize; } Event.current.Use(); } } // ctrl + scroll = brush size else { // scroll up if (Event.current.delta.y > 0) { gizmo.brushSettings.brushSize++; Event.current.Use(); } // scroll down else if (Event.current.delta.y < 0) { gizmo.brushSettings.brushSize--; // TODO: slider if (gizmo.brushSettings.brushSize < 1) { gizmo.brushSettings.brushSize = 1; } Event.current.Use(); } } } } BrushMode brushMode = BrushMode.None; if (Event.current.shift) { brushMode = BrushMode.Add; if (Event.current.control) { brushMode = BrushMode.Remove; } } // draw brush gizmo DrawBrush(mousePos, hit.normal, radius, brushMode); // paint prefabs on mouse drag if (Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseDown) { // left button = 0; right = 1; middle = 2 if (Event.current.button == 0) { switch (brushMode) { case BrushMode.None: break; case BrushMode.Add: AddPrefabs(hit); break; case BrushMode.Remove: RemovePrefabs(hit.point); break; } Event.current.Use(); } } } else { mousePosValid = false; } if (Event.current.type == EventType.Layout) { HandleUtility.AddDefaultControl(controlId); } // examples about how to show ui info // note: Handles.BeginGUI and EndGUI are important, otherwise the default gizmos aren't drawn Handles.BeginGUI(); if (mousePosValid) { ShowHandleInfo(mousePos); } string[] info = new string[] { "Add prefabs: shift + drag mouse\nRemove prefabs: shift + ctrl + drag mouse\nBrush size: ctrl + mousewheel, Brush rotation: ctrl + shift + mousewheel", "Children: " + editor.getContainerChildren().Length }; PrefabPainterEditor.ShowGuiInfo(info); Handles.EndGUI(); }