Is there a way to manually set a controlID to one of Unity’s built-in controls (i.e. Buttons, text fields, etc) ?
I am writing a custom control that behave like a normal label when not focused and switch to a text field when I detect a mouse click. Problem is, if I use GUI/EditorGUI.TextField(…), they internally set GUIUtility.hotControl and I lose my custom control’s focus.
My code looks like this for now :
public static string EditableLabel(Rect position, string label)
{
int controlId = GUIUtility.GetControlID(GUIHashes.EditableLabel, FocusType.Keyboard, position);
EditorGUIUtility.AddCursorRect(position, MouseCursor.Text, controlId);
GUI.SetNextControlName("EditableLabel");
switch(Event.current.GetTypeForControl(controlId))
{
case EventType.Repaint:
if(GUIUtility.hotControl == controlId)
label = GUI.TextArea(position, label);
else
GUI.Label(position, label, Styles.LabelBoldItalicStyle);
break;
case EventType.MouseDown :
if(Event.current.button == 0 && position.Contains(Event.current.mousePosition))
{
GUIUtility.hotControl = controlId;
EditorGUI.FocusTextInControl("EditableLabel");
}
break;
case EventType.MouseUp:
if(Event.current.button == 0 && GUIUtility.hotControl == controlId)
{
GUIUtility.hotControl = 0;
GUIUtility.keyboardControl = 0;
}
break;
}
return label;
}
I’m making tools right now and the lack of IMGUI solid documention is seriously hindering…