Unity编辑器搜索风格输入框(放大镜可点击)

参考 Unity编辑器搜索风格输入框

效果如下

没输入的时候

有输入的时候

 private GUIStyle TextFieldRoundEdge;
        private GUIStyle TextFieldRoundEdgeCancelButton;
        private GUIStyle TextFieldRoundEdgeCancelButtonEmpty;
        private GUIStyle TransparentTextField;
        private string m_InputSearchText;
        /// <summary>
        /// 绘制输入框,放在OnGUI函数里
        /// </summary>
        private void DrawInputTextField()
        {
            if (TextFieldRoundEdge == null)
            {
                TextFieldRoundEdge = new GUIStyle("SearchTextField");
                TextFieldRoundEdgeCancelButton = new GUIStyle("SearchCancelButton");
                TextFieldRoundEdgeCancelButtonEmpty = new GUIStyle("SearchCancelButtonEmpty");
                TransparentTextField = new GUIStyle(EditorStyles.whiteLabel);
                TransparentTextField.normal.textColor = EditorStyles.textField.normal.textColor;
            }

            //获取当前输入框的Rect(位置大小)
            Rect position = EditorGUILayout.GetControlRect();
            //设置圆角style的GUIStyle
            GUIStyle textFieldRoundEdge = TextFieldRoundEdge;
            //设置输入框的GUIStyle为透明,所以看到的“输入框”是TextFieldRoundEdge的风格
            GUIStyle transparentTextField = TransparentTextField;
            //选择取消按钮(x)的GUIStyle
            GUIStyle gUIStyle = (m_InputSearchText != "") ? TextFieldRoundEdgeCancelButton : TextFieldRoundEdgeCancelButtonEmpty;

            //输入框的水平位置向左移动取消按钮宽度的距离
            position.width -= gUIStyle.fixedWidth;
            //放大镜的大小
            float magnifierWidth = textFieldRoundEdge.CalcSize(new GUIContent("")).x;
            Rect rect = position;
            rect.width = magnifierWidth;
            if ((Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition)))
            {
                //当放大镜被点击
                Debug.LogError("放大镜被点击");
            }

            //如果面板重绘
            if (Event.current.type == EventType.Repaint)
            {
                //根据是否是专业版来选取颜色
                GUI.contentColor = (EditorGUIUtility.isProSkin ? new Color(0.706f, 0.706f, 0.706f, 0.5f) : new Color(0f, 0f, 0f, 0.5f));
                //当没有输入的时候提示“请输入”
                if (string.IsNullOrEmpty(m_InputSearchText))
                {
                    textFieldRoundEdge.Draw(position, new GUIContent("请输入"), 0);
                }
                else
                {
                    textFieldRoundEdge.Draw(position, new GUIContent(""), 0);
                }
                //因为是“全局变量”,用完要重置回来
                GUI.contentColor = Color.white;
            }
            rect = position;
            //为了空出左边那个放大镜的位置
            float num = textFieldRoundEdge.CalcSize(new GUIContent("")).x - 2f;
            rect.width -= num;
            rect.x += num;
            rect.y += 1f;//为了和后面的style对其

            m_InputSearchText = EditorGUI.TextField(rect, m_InputSearchText, transparentTextField);
            //绘制取消按钮,位置要在输入框右边
            position.x += position.width;
            position.width = gUIStyle.fixedWidth;
            position.height = gUIStyle.fixedHeight;
            if (GUI.Button(position, GUIContent.none, gUIStyle) && m_InputSearchText != "")
            {
                m_InputSearchText = "";
                //用户是否做了输入
                GUI.changed = true;
                //把焦点移开输入框
                GUIUtility.keyboardControl = 0;
            }
        }

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值