Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding search feature - not an issue #18

Open
SmikeSix opened this issue Jul 8, 2023 · 0 comments
Open

Adding search feature - not an issue #18

SmikeSix opened this issue Jul 8, 2023 · 0 comments

Comments

@SmikeSix
Copy link

SmikeSix commented Jul 8, 2023

Hi i really love your plugin, just tried it out, im always having too many properties, so i hacked together a cheap search box on top of my inspectors.

It breaks the layout and just draws them by type. Its more like a search feature for properties.
I use it constantly, it safes a lot of time, but its not super performant.
If you like it integrate it into markup attributes or change it (just merged it with your editor, seems to work) please go ahead.
Anyone else feel free to use it, i use it since years with unity 2019/2020.

[CustomEditor(typeof(MonoBehaviour), true)]
[CanEditMultipleObjects]
public class CustomMonoBehaviourInspector : MarkedUpEditor
{


    internal static bool DoDrawDefaultInspector(SerializedObject obj, string search)
    {
        EditorGUI.BeginChangeCheck();
        obj.UpdateIfRequiredOrScript();

        // Loop through properties and create one field (including children) for each top level property.
        SerializedProperty property = obj.GetIterator();
        bool expanded = true;
        search = search.ToLower();
        while (property.NextVisible(true))
        {
            using (new EditorGUI.DisabledScope("m_Script" == property.propertyPath))
            {
                bool found = SearchForMatchesRecrusiv(search, property, true);
                if (found)
                {
                    if (property.depth >= 1)
                    {
                        String nName = property.propertyPath;
                        nName = nName.Replace("." + property.name, "." + property.displayName);
                        nName = nName.Replace(".", " » ");
                        EditorGUILayout.PropertyField(property, new GUIContent(nName), true);
                    }
                    else
                    {
                        EditorGUILayout.PropertyField(property, true);
                    }

                    //   D.Bla(property + " ok " + property.name + " " + property.displayName + " " + property.depth + " no value");

                }
            }
            expanded = false;
        }

        obj.ApplyModifiedProperties();
        return EditorGUI.EndChangeCheck();
    }

    private static bool SearchForMatchesRecrusiv(string search, SerializedProperty property, bool isFirstLayer = true)
    {
        bool found = false;

        if (property.name.ToLower().Contains(search))
        {
            found = true;
        }

        if (property.displayName.ToLower().Contains(search))
        {
            found = true;
        }

        if (property.propertyType == SerializedPropertyType.ObjectReference)
        {
            if (property.objectReferenceValue != null)
            {
                if (property.objectReferenceValue.name.ToLower().Contains(search))
                {
                    found = true;
                }


            }
        }
        else if (property.propertyType == SerializedPropertyType.String)
        {
            bool innerF = property.stringValue.ToLower().Contains(search);
            if (innerF)
            {
                found = true;
            }
        }
        else if (property.propertyType == SerializedPropertyType.Generic)
        {
            /*
            SerializedProperty prop = property.Copy();

            SerializedProperty endProperty = prop.GetEndProperty();
            while (prop.NextVisible(true) && !SerializedProperty.EqualContents(prop, endProperty))
            {
                bool foundInner = SearchForMatchesRecrusiv(search, prop, false);

                if (foundInner)
                {
                    //  found = true;
                }
            }
            */
        }

        return found;
    }


    //Unity's built-in editor
    /* Editor defaultEditor; //MarkedUp
     MarkedUpEditor markupEditor;
     Transform transform;
     *///  FoldoutAttributeHandler _foldout;

    protected void OnEnable()
    {

        currentSearch = EditorPrefs.GetString(GetTargetId(), "");
        base.OnEnable();
    }
    public string GetTargetId()
    {
        return "tx_search_" + target.GetInstanceID();
    }

    protected void OnDisable()
    {
        //When OnDisable is called, the default editor we created should be destroyed to avoid memory leakage.
        //Also, make sure to call any required methods like OnDisable
        //  _foldout?.OnDisable();
        /*
        Debug.Log("ON DISABLE");
        MethodInfo disableMethod = defaultEditor.GetType().GetMethod("OnDisable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        if (disableMethod != null)
            disableMethod.Invoke(defaultEditor, null);
        DestroyImmediate(defaultEditor);*/
        base.OnDisable();
    }

    public string currentSearch = "";
    public int wasFocused = 0;
    internal string searchBefore = "";
    public override void OnInspectorGUI()
    {

        Rect rs = EditorGUILayout.BeginHorizontal();
        GUI.SetNextControlName("MyTextField");
        Color defaultbackgroundColor = GUI.backgroundColor;
        Color defaultColor = GUI.color;

        if (!string.IsNullOrEmpty(currentSearch))
        {
            //  GUI.color = Color.;
        }
        currentSearch = EditorGUILayout.TextField(currentSearch, GUILayout.Width(EditorGUIUtility.currentViewWidth * 0.83f));
        rs.x += rs.width * 0.85f;
        rs.width *= 0.15f;

        if (wasFocused > 0)
        {
            EditorGUI.FocusTextInControl("MyTextField");
            wasFocused--;

        }
        if (!string.IsNullOrEmpty(currentSearch))
        {
            GUI.color = defaultColor;
            GUI.backgroundColor = Color.yellow;
        }
        if (GUI.Button(rs, "X"))
        {
            GUIUtility.keyboardControl = 0;
            currentSearch = "";
            wasFocused = 6;

            //  EditorGUI.FocusTextInControl("MyTextField");
        }
        if (!string.IsNullOrEmpty(currentSearch))
        {
            GUI.backgroundColor = defaultColor;
        }
        if (currentSearch != searchBefore)
            EditorPrefs.SetString(GetTargetId(), currentSearch);

        searchBefore = currentSearch;
        //  EditorGUILayout.LabelField("Local Space", EditorStyles.boldLabel);
        EditorGUILayout.EndHorizontal();
        if (!string.IsNullOrEmpty(currentSearch))
        {
            DoDrawDefaultInspector(serializedObject, currentSearch);

        }
        else
        {
            //defaultEditor.OnInspectorGUI();
            base.OnInspectorGUI();
            //  defaultEditor.OnInspectorGUI();
        }

        GUI.enabled = true;
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant