Search Unity

Use placeholder text in CustomEditor

Discussion in 'Immediate Mode GUI (IMGUI)' started by LazyDog_Rich, Aug 9, 2016.

  1. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    There's some way to put a placeholder text(you know, italic in gray) in the string box using custom editor or property drawer?

    Here a example of string box photoshoped to look like I want:
     
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    There's not a nice way to do this out of the box, but you could draw a GUI.Label with a grey/italicized style over the text field when it is empty.

    I've made note of this feature request.
     
    Billy4184 and LazyDog_Rich like this.
  3. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    haha I think about that yesterday before get sleep "maybe I could draw a text over the thex field and use a conditional to show it only when the text field is empty or not selected" xD

    Now I only have one dobt left: There's a way to check when the field is selected or clicked?

    Thanks in forward!
     
  4. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    Not without reflection. You'll need to call the internal TextField passing an ID you get yourself from GetControlID. Then use that ID to check EditorGUI.activeEditor.IsEditingControl(id).
     
  5. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    70
    Ok, going to try my best on this workaround, thanks for answer!
     
  6. benbenmushi

    benbenmushi

    Joined:
    Jan 29, 2013
    Posts:
    34
    Here's how I managed to implement this feature

    Code (CSharp):
    1. string PlaceHolderTextField(Rect rect, string input, string controlID, string placeHolder = "Enter Value...")
    2. {
    3.     if (Event.current.type != EventType.Layout)
    4.     {
    5.         GUI.SetNextControlName(controlID);
    6.         if (!string.IsNullOrEmpty(input) || GUI.GetNameOfFocusedControl() == controlID)
    7.             return GUI.TextField(rect, input);
    8.         else
    9.             GUI.TextField(rect, placeHolder, greyTextField);
    10.     }
    11.     return input;
    12. }
    Simply make sure the controlID is unique

    Note: downside is GUI.TextField doesn't allow to paste values... and EditorGUI.TextField doesn't work GUI.SetNextControlName and GUI.GetNameOfFocusedControl functions afak
     
    Last edited: Jan 17, 2018
    codestage likes this.
  7. davidosullivan

    davidosullivan

    Joined:
    Jun 9, 2015
    Posts:
    387
    I think you could do it like this:
    Code (CSharp):
    1. //make an italic style textarea for the placeholder
    2.                 var placeholderTextStyle = new GUIStyle(EditorStyles.textArea);
    3.                 placeholderTextStyle.fontStyle = FontStyle.Italic;
    4.                 //Output the real text field
    5.                 myText = EditorGUI.TextArea(myRect, myText);
    6.                 //if thats empty output a disabled dummy one
    7.                 if (string.IsNullOrEmpty(myText))
    8.                 {
    9.                     EditorGUI.BeginDisabledGroup(true);
    10.                     EditorGUI.TextArea(myRect, "Placeholder Text", placeholderTextStyle);
    11.                     EditorGUI.EndDisabledGroup();
    12.                 }
     
    codestage and sxnorthrop like this.