Ejemplo n.º 1
0
        /// <summary>
        /// 拖动鼠标以修改数值
        /// </summary>
        public static float DragValue(Rect rect, GUIContent content, float value, float step, GUIStyle style, bool horizontal = true)
        {
            int controlID = GUIUtility.GetControlID(FocusType.Passive);

            switch (Event.current.GetTypeForControl(controlID))
            {
            case EventType.Repaint:
            {
                GUI.Label(rect, content, style);
                break;
            }

            case EventType.MouseDown:
            {
                if (Event.current.button == 0 && rect.Contains(Event.current.mousePosition))
                {
                    GUIUtility.hotControl = controlID;
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == controlID)
                {
                    GUIUtility.hotControl = 0;
                }
                break;
            }
            }

            if (Event.current.isMouse && GUIUtility.hotControl == controlID)
            {
                if (Event.current.type == EventType.MouseDrag)
                {
                    if (horizontal)
                    {
                        value += Event.current.delta.x * step;
                    }
                    else
                    {
                        value -= Event.current.delta.y * step;
                    }
                    value = MathKit.RoundToSignificantDigitsFloat(value);

                    GUI.changed = true;
                }

                Event.current.Use();
            }

            return(value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 拖动鼠标以修改数值
        /// </summary>
        public static float DragValue(Rect rect, GUIContent content, float value, float step, GUIStyle style, bool horizontal = true)
        {
            int controlID = GUIUtility.GetControlID(FocusType.Passive);

            switch (Event.current.GetTypeForControl(controlID))
            {
            case EventType.Repaint:
            {
                GUI.Label(rect, content, style);
                break;
            }

            case EventType.MouseDown:
            {
                if (Event.current.button == 0 && rect.Contains(Event.current.mousePosition))
                {
                    GUIUtility.hotControl = controlID;
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == controlID)
                {
                    GUIUtility.hotControl = 0;
                    UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
                }
                break;
            }
            }

            if (GUIUtility.hotControl == controlID)
            {
                if (Event.current.isMouse)
                {
                    if (Event.current.type == EventType.MouseDrag)
                    {
                        if (horizontal)
                        {
                            value += Event.current.delta.x * step;
                        }
                        else
                        {
                            value -= Event.current.delta.y * step;
                        }
                        value = MathKit.RoundToSignificantDigitsFloat(value);

                        GUI.changed = true;
                    }

                    Event.current.Use();
                }

                rect.size   = new Vector2(1000, 1000);
                rect.center = Event.current.mousePosition;
            }

            EditorGUIUtility.AddCursorRect(rect, horizontal ? MouseCursor.ResizeHorizontal : MouseCursor.ResizeVertical);

            return(value);
        }