- Home /
How to achieve proper inspector functionality (blue highlight of last tweaked) with elements from the GUI class (not from EditorGUI)
So, I'm working on a custom inspector that uses GUI.HorizontalSlider
rather than EditorGUI.Slider
because I need more control with the exact positioning of the horizontal slider itself and the look of the slider using custom GUIStyles.
Everything is working great except for one thing. When tweaking any slider or EditorGUI
property, it is highlighted and somehow tagged as active (with a blue highlight/outline). This functionality does not work with GUI elements from the GUI
class in the inspector. This causes problems when tweaking my slider. Any previously tweaked element stays highlighted, even after tweaking my slider. I've got an EditorGUI.FloatField
setup to edit the same value as the slider, and when clicking on the float field to adjust the value, and then moving the slider, the numbers in the float field don't change properly, and they stay highlighted as if still editing them.
I've tried wrapping my GUI.HorizontalSlider
with EditorGUI.BeginProperty()
and EditorGUI.EndProperty()
but this doesn't solve this problem.
Is there anything in the API that would allow me to take control of this mysterious blue highlighting of the last tweaked GUI element in the inspector? If I could somehow force it to happen, this would solve my problems. I would like to not have to resort to using EditorGUI.Slider
because I need to know the exact Rect
that corresponds to the horizontal slider itself (due to some skinning magic), and using EditorGUI.Slider
adds a float field and a label.
Any help would be greatly appreciated.
Answer by sonicether · Feb 11, 2015 at 08:46 AM
Alright, I found a solution. I figured I'd post it here in case anyone googling the problem lands on this page.
Setting focus (blue highlighted control is "focused") of a control is done with GUI.FocusControl()
. If you look at the method signature in the docs, you'll see it uses a control name to determine which control to focus. So, you need to name your GUI control with GUI.SetNextControlName()
before it's created.
I simply check to see if the slider was adjusted with EditorGUI.BeginChangeCheck()
and EditorGUI.EndChangeCheck()
, and if it was, I focus the control with GUI.FocusControl
being sure to provide the control's name.
Your answer
Follow this Question
Related Questions
Custom inspector variable resets automatically 1 Answer
Custom inspector: changing foldout's text color 3 Answers
How to force close Popup Window? 1 Answer
Accessing All Selected Objects in OnSceneGUI 1 Answer
EditorGUI.Foldout consumes click so GUI.Button doesnt work when inside of foldout region. 1 Answer