Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Use placeholder text in CustomEditor

Discussion in 'Extensions & OnGUI' started by LazyDog_Rich, Aug 9, 2016.

  1. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    58
    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:
    550
    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.
     
    LazyDog_Rich likes this.
  3. LazyDog_Rich

    LazyDog_Rich

    Joined:
    Apr 27, 2014
    Posts:
    58
    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:
    550
    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:
    58
    Ok, going to try my best on this workaround, thanks for answer!
     
  6. benbenmushi

    benbenmushi

    Joined:
    Jan 29, 2013
    Posts:
    19
    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
  7. davidosullivan

    davidosullivan

    Joined:
    Jun 9, 2015
    Posts:
    379
    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.                 }