Unity官方介绍文档:
https://docs.unity3d.com/ScriptReference/IMGUI.Controls.SearchField.html
Properties
1、autoSetFocusOnFindCommand:当用户按下“Ctrl/Cmd + F”时,聚焦该控件,默认值为true
相关代码:
private bool m_AutoSetFocusOnFindCommand = true;
public bool autoSetFocusOnFindCommand
{
get
{
return this.m_AutoSetFocusOnFindCommand;
}
set
{
this.m_AutoSetFocusOnFindCommand = value;
}
}
private void CommandEventHandling()
{
Event current = Event.current;
if (current.type == EventType.ExecuteCommand || current.type == EventType.ValidateCommand)
{
//UnityEngine内部应该建立了commandName与按键的对应关系
//https://docs.unity3d.com/ScriptReference/Event-commandName.html
if (this.m_AutoSetFocusOnFindCommand && current.commandName == "Find")
{
if (current.type == EventType.ExecuteCommand)
{
this.SetFocus();
}
current.Use();
}
}
}
2、searchFieldControlID
控件的ControllID,就不多介绍了,构造函数里会为它赋值。
公有函数:
构造函数:
public SearchField()
{
this.m_ControlID = GUIUtility.GetPermanentControlID();
}
HasFocus:
public bool HasFocus()
{
return GUIUtility.keyboardControl == this.m_ControlID;
}
SetFocus:
public void SetFocus()
{
this.m_WantsFocus = true;
}
OnGUI
提供了三个重载:
public string OnGUI(string text, params GUILayoutOption[] options);
public string OnGUI(Rect rect, string text);
public string OnGUI(Rect rect, string text, GUIStyle style, GUIStyle cancelButtonStyle, GUIStyle emptyCancelButtonStyle);
public string OnGUI(Rect rect, string text, GUIStyle style, GUIStyle cancelButtonStyle, GUIStyle emptyCancelButtonStyle)
{
this.CommandEventHandling();
this.FocusAndKeyHandling();
float fixedWidth = cancelButtonStyle.fixedWidth;
Rect position = rect;
position.width -= fixedWidth;
text = EditorGUI.TextFieldInternal(this.m_ControlID, position, text, style);
Rect position2 = rect;
position2.x += rect.width - fixedWidth;
position2.width = fixedWidth;
//搜索框的 X 按钮
if (GUI.Button(position2, GUIContent.none, (!(text != "")) ? emptyCancelButtonStyle : cancelButtonStyle) && text != "")
{
//搜索文字 置为 Empty,并取消其聚焦
text = "";
GUIUtility.keyboardControl = 0;
}
return text;
}
public string OnGUI(Rect rect, string text)
{
//EditorStyles.searchField = 搜索框样式
//EditorStyles.searchFieldCancelButton= 搜索框取消按钮样式
//EditorStyles.searchFieldCancelButtonEmpty = 搜索框 搜索关键字为空时的 样式
return this.OnGUI(rect, text, EditorStyles.searchField, EditorStyles.searchFieldCancelButton, EditorStyles.searchFieldCancelButtonEmpty);
}
public string OnGUI(string text, params GUILayoutOption[] options)
{
Rect rect = GUILayoutUtility.GetRect(36f, 1E+07f, 16f, 16f, EditorStyles.searchField, options);
return this.OnGUI(rect, text);
}
私有函数:
FocusAndKeyHandling:
private void FocusAndKeyHandling()
{
Event current = Event.current;
//SetFocus公有接口将私有变量m_WantsFocus置为true,将controlID赋值给GUIUtility.keyboardControl以此实现Focus
if (this.m_WantsFocus && current.type == EventType.Repaint)
{
GUIUtility.keyboardControl = this.m_ControlID;
EditorGUIUtility.editingTextField = true;
this.m_WantsFocus = false;
}
if (current.type == EventType.KeyDown && (current.keyCode == KeyCode.DownArrow || current.keyCode == KeyCode.UpArrow) && GUIUtility.keyboardControl == this.m_ControlID && GUIUtility.hotControl == 0)
{
if (this.downOrUpArrowKeyPressed != null)
{
this.downOrUpArrowKeyPressed();
current.Use();
}
}
}
Event:
downOrUpArrowKeyPressed:
搜索字段时检测到向上或者向下键被按下时,可以用来改变键盘焦点到另一个控件,例如TreeView,如下图
public event SearchField.SearchFieldCallback downOrUpArrowKeyPressed
{
add
{
SearchField.SearchFieldCallback searchFieldCallback = this.downOrUpArrowKeyPressed;
SearchField.SearchFieldCallback searchFieldCallback2;
do
{
searchFieldCallback2 = searchFieldCallback;
searchFieldCallback = Interlocked.CompareExchange<SearchField.SearchFieldCallback>(ref this.downOrUpArrowKeyPressed, (SearchField.SearchFieldCallback)Delegate.Combine(searchFieldCallback2, value), searchFieldCallback);
}
while (searchFieldCallback != searchFieldCallback2);
}
remove
{
SearchField.SearchFieldCallback searchFieldCallback = this.downOrUpArrowKeyPressed;
SearchField.SearchFieldCallback searchFieldCallback2;
do
{
searchFieldCallback2 = searchFieldCallback;
searchFieldCallback = Interlocked.CompareExchange<SearchField.SearchFieldCallback>(ref this.downOrUpArrowKeyPressed, (SearchField.SearchFieldCallback)Delegate.Remove(searchFieldCallback2, value), searchFieldCallback);
}
while (searchFieldCallback != searchFieldCallback2);
}
}
以上代码为使用ILSpy工具反编译得来,仅供学习使用。
以上知识分享,如有错误,欢迎指出,共同学习,共同进步。