Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question What is so special about GUI.skin.textArea?

Discussion in 'Editor & General Support' started by steinbitglis, Oct 10, 2023.

  1. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    I have tried a lot of things to draw a text area with a custom skin, but I can't seem to use a custom style correctly.
    The following two calls demonstrate two ways to draw a text area that I thought would be equal, but that actually aren't. If I use the style that is equal to GUI.skin.textArea, I get a text area. If I use any other style (including 100% equals ones), I get something that looks more like a label.

    Is there a way to use a custom style for a textArea without actually replacing the GUI.skin.textArea.
    Or is it maybe documented somewhere that GUI.skin.textArea is special somehow?

    Code (CSharp):
    1. if (Event.current.type == EventType.Repaint) {
    2.     var r1 = new Rect(100,  50, 100, 100);
    3.     var r2 = new Rect(100, 100, 100, 100);
    4.  
    5.     GUI.skin.GetStyle("comment" ) .Draw(r1, new GUIContent("test"), -1);
    6.  
    7.     GUI.skin.textArea = new GUIStyle( GUI.skin.GetStyle("comment" ) );
    8.     GUI.skin.GetStyle("textArea" ).Draw(r2, new GUIContent("test"), -1);
    9. }
     
  2. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    Update:
    If I do the following, I will successfully be drawing a text area with two different GUIStyles.

    Code (CSharp):
    1. if (Event.current.type == EventType.Repaint) {
    2.     var s = System.Array.Find( GUI.skin.customStyles, s => s.name == "textarea" );
    3.     s                              .Draw(new Rect(100,  50, 100, 100), new GUIContent("test"), -1);
    4.  
    5.     var s2 = GUI.skin.textArea;
    6.     s2                             .Draw(new Rect(100, 100, 100, 100), new GUIContent("test"), -1);
    7. }
    So... I will now assume that there is no special reference anywhere, but the name of the GUIStyle is significant. And because I now will have to overload the name textarea (the casing needs to be exact), I can not use the string-to-guistyle operator or do GetStyle and be based on the name.
     
  3. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    After a lot of experimentation, I've found that if I populate both the background field and scaledBackground fields, the style (or rather, its background) seems to be applied as expected.

    The reason that my style was working, only when named textarea, seems to be that I was using the same texture as the fallback texture... and only 'textarea' styles would actually get a fallback.