Search Unity

How to highlight labels in SettingsProvider search?

Discussion in 'Immediate Mode GUI (IMGUI)' started by LazloBonin, Apr 2, 2019.

  1. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    812
    I'm trying to upgrade Bolt to use the latest SettingsProvider API including search.

    I got it mostly working, except the "highlight" effect that comes with search in default panels.

    What API can I use to achieve this?

    upload_2019-4-1_20-28-7.png
     
  2. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    812
    I got it everything working except the blue background, which I've replaced with a bold.
    Is there a new tag for rich text labels that would allow this highlight effect?
     
  3. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Hi,

    How do you create your SettingsProvider? It works well by default in my project.
    This is my code:

    Code (CSharp):
    1. [SettingsProvider]
    2. private static SettingsProvider CreateSettingsProvider()
    3. {
    4.     return new SettingsProvider("Preferences/Console", SettingsScope.User, new string[] {"Preview logs", "Reload Style", "Padding", "Font size", "Smooth auto-scroll", "Show Console On Fatal Error", "Console"})
    5.     {
    6.         guiHandler =  (searchContext => ConsoleWindow.PreferencesGUI())
    7.     };
    8. }
    9.  
    10. public static void PreferencesGUI()
    11. {
    12.     ...
    13. }
     
  4. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    812
    The core of it would be in the content of your PreferencesGUI method.
    I'm guessing you're using Unity's default EditorGUILayout.PropertyField methods?
    If so that would explain why it's automatic. Unfortunately I can't use them.
     
  5. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Nope I don't use PropertyField. I've just found than when you are searching, it highlights only the prefix label so you have to use something like this:
    Code (CSharp):
    1. EditorGUILayout.PrefixLabel("Highlight me please");
     

    Attached Files:

  6. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    812
    That's nice, but I'm not using PrefixLabel either, Bolt has its own very custom inspector framework. I wonder if there is any way to get that blue background otherwise?
     
  7. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Oh sorry, I missunderstood what you want. You want to have the highlight in a custom editor/window, right ?
    So here the solution with a bit of reflection:):

    Code (CSharp):
    1. private static ConstructorInfo s_labelHighlightScopeConstructor;
    2. public static IDisposable LabelHighlightScope(string labelHighlightContext, Color selectionColor, Color textColor)
    3. {
    4.     if (s_labelHighlightScopeConstructor == null)
    5.     {
    6.         Type labelHighlightScopeType = System.Type.GetType("UnityEditor.EditorGUI+LabelHighlightScope,UnityEditor.dll");
    7.         s_labelHighlightScopeConstructor = labelHighlightScopeType.GetConstructor(new Type[] {typeof(string), typeof(Color), typeof(Color) });
    8.     }
    9.     return (IDisposable)s_labelHighlightScopeConstructor.Invoke(null, new object[] {labelHighlightContext, selectionColor, textColor});
    10. }
    And you can call it like this:
    Code (CSharp):
    1. using(LabelHighlightScope("YouSelectedLabel", Color.blue, Color.white))
    2. {
    3.     //your code here
    4. }
     
    LazloBonin likes this.
  8. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    812
    Perfect, thanks!
     
    PsyKaw likes this.