x


GUILayout ScrollView not working

I've got a chat with a scrollbar for the messages sent, but that scrollbar does not work with certain labels :S. I managed to convert FancyLabel to JS and made a label using that, but if I put it in my scrollview it does not show the scrollbar when there is too much text. Whenever I put in the original String object without the FancyLabel coloring and layout, it does work. Is there a way to get a GUILayout.Label that was built in the FancyLabel function to work with a scrollview? I'd love to give some examples of my script but I don't really feel like posting my complete script, but this is the original FancyLabel script in c#:

 using UnityEngine;    
 using System.Collections.Generic;
 using System.Text;
 /// <summary>
 /// Create a series of labels to accomodate the various formatting commands contained
 /// within the text.  Commands are enclosed within brackets: [].  The following
 /// case-insensitive commands are available:
 ///   * Background color (BC, BackColor): <RRGGBBAA> or '?' to reset to default    
 ///   * Color (C, Color): <RRGGBBAA> or '?' to reset to default
 ///   * Font name (F, Font): <font name>
 ///   * Font attribute (FA, FontAttribute): U (underline on), -U (underline off), S (strikethrough on) or -S (strikethrough off)
 ///   * Font size (FS, FontSize): <size>
 ///   * Hyperlink (H, HyperLink): H (hyperlink start) <hyperlink tag>, -H (hyperlink end)
 ///   * Horizontal alignment (HA, HAlign):  L (left), R (right), or C (center)
 ///   * Space (S, Space): <pixels>
 ///   * Vertical alignment (VA, Valign):  B (bottom) or '?' to reset to Unity default
 ///   Based on http://forum.unity3d.com/threads/9549-FancyLabel-Multicolor-and-Multifont-label
 ///   Hexadecimal color picker:  http://www.colorpicker.com/
 /// </summary>
 public class FormattedLabel : MonoBehaviour
 {    
     public GUISkin skin;
 
     // Use this for initialization
     void Start()
     {
     
     }
     
     // Update is called once per frame
     void Update()
     {
     
     }
  
     int HexStringToInt(string hexString)
     { 
         int value = 0; 
         int digitValue = 1; 
         hexString = hexString.ToUpper(); 
         char[] hexDigits = hexString.ToCharArray(0, hexString.Length); 
     
         for(int i = hexString.Length - 1; i >= 0; i--)
         { 
             int digit = 0; 
             if (hexDigits[i] >= '0' && hexDigits[i] <= '9')
             { 
                 digit = hexDigits[i] - '0'; 
             }
             else if(hexDigits[i] >= 'A' && hexDigits[i] <= 'F')
             { 
                 digit = hexDigits[i] - 'A' + 10;             
             }
             else
             { 
                 // Not a hex string 
                 return -1; 
             }
             
             value += digit * digitValue; 
             digitValue *= 16; 
         } 
     
         return value; 
     } 
  
  
  
     public void FancyLabel2(Rect rect, string text, Font normalFont, Font boldFont, Font italicFont, TextAlignment alignment)
     {
         //bool   leftSpace = false, rightSpace = false, topSpace = false, bottomSpace = false; 
      
         Color    textColor = GUI.skin.GetStyle("Label").normal.textColor; 
         Font    defaultFont = GUI.skin.font;
         Font   newFont = null; 
      
         //GUIStyle fontStyle = new GUIStyle(testStyle); 
         GUIStyle fontStyle = new GUIStyle();
         fontStyle.normal.textColor = textColor;
         
         // Start with normal font 
         if(normalFont != null)
         { 
             fontStyle.font = normalFont; 
         }
         else
         { 
             fontStyle.font = defaultFont;          
         }
      
         // NOTE: Lowering this padding reduces the line spacing 
         // May need to adjust per font 
         fontStyle.padding.bottom = -5; 
         
         GUILayout.BeginArea(rect); 
         GUILayout.BeginVertical(GUILayout.ExpandHeight(true), 
                                 GUILayout.Width(rect.height), 
                                 GUILayout.MinWidth(rect.height)); 
         GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true), 
                                     GUILayout.Width(rect.width), 
                                     GUILayout.MinWidth(rect.width));
                                     
         // Insert flexible space on the left if Center or Right aligned 
         if(alignment == TextAlignment.Right || alignment == TextAlignment.Center)
         { 
             GUILayout.FlexibleSpace(); 
         }
         int newline = 0;
         bool clearBuffer = false;
         double pixelsPerLine = 0;
         double avgPpC = 8.5;
         
         text = text.Replace("\n", "");
         text = text.Replace("\r", "");
         
         string[] toks = text.Split(' ');
         string output = "";
      
         for(int i=0; i<toks.Length; ++i)
         {
             //Add a leading space if you need one
             if(i != 0)
             {
                 output += " ";
                 pixelsPerLine += avgPpC;
             }
                     
             int index = 0;
             while(index < toks[i].Length)
             {
                 if(toks[i][index] == '\\')
                 {
                     //Must be an escape character
                     if(toks[i][index+1] == 'n')
                     {
                         ++newline;
                     }
                     else if(toks[i][index+1] == '#')
                     {
                         output += "#";
                     }
                     
                     index += 2;
                 }
                 else if(toks[i][index] == '#')
                 {
                     //Must be a style sequence
                     //The style will probably change, so might as well start a new label
                     clearBuffer = true;
                     
                     if(toks[i][index+1] == '!')
                     {
                         //Original Color
                         textColor = GUI.skin.GetStyle("Label").normal.textColor;
                     }
                     else if(toks[i][index+1] == 'n')
                     {
 
                         //Normal Font
 
                         if (normalFont != null)
 
                             newFont = normalFont;
 
                     }
 
                     else if(toks[i][index+1] == 'x')
 
                     {
 
                         //Bold Font
 
                         if (boldFont != null)
 
                             newFont = boldFont;
 
                     }
 
                    else if(toks[i][index+1] == 'i')
 
                     {
 
                         //Italic Font
 
                         if (italicFont != null)
 
                             newFont = italicFont;
 
                     }
 
                     else
 
                     {
 
                         //Must be a color change
 
                         string rText = toks[i].Substring(index + 1, 2);
 
                         string gText = toks[i].Substring(index + 3, 2);
 
                         string bText = toks[i].Substring(index + 5, 2);
 
                         string aText = toks[i].Substring(index + 7, 2);
 
      
 
                         float r = HexStringToInt(rText) / 255.0f; 
 
                         float g = HexStringToInt(gText) / 255.0f; 
 
                         float b = HexStringToInt(bText) / 255.0f; 
 
                         float a = HexStringToInt(aText) / 255.0f; 
 
                     
 
                         if(r < 0 || g < 0 || b < 0 || a < 0)
 
                         { 
 
                             Debug.Log("Invalid color sequence");
 
                             return;
 
                         } 
 
                     
 
                         textColor = new Color(r, g, b, a);
 
                         index += 7;
 
                     }
 
      
 
                     index += 2;
 
                 }
 
                 else
 
                 {
 
                     //Must just be a regular string
 
                     //Check to see if a new line is needed, then go ahead and add the text to the string
 
                     int index2, firstFormat, firstEscape = 0;
 
                     firstFormat = toks[i].IndexOf("#", index);
 
                     firstEscape = toks[i].IndexOf("\\", index);
 
                     //if(firstFormat != -1 && (firstFormat < firstEscape && firstEscape != -1))
 
                     if(firstFormat != -1 && (firstEscape!=-1?firstFormat<firstEscape:true))
 
                     {
 
                         index2 = firstFormat;
 
                     }
 
                     else if(firstEscape != -1)
 
                     {
 
                         index2 = firstEscape;
 
                     }
 
                     else
 
                     {
 
                         index2 = toks[i].Length;
 
                     }
 
                     
 
                     //Check to see if the words need to wrap
 
                     if((pixelsPerLine + (index2 - index)*avgPpC) >= rect.width)
 
                     {
 
                         if(newline == 0) newline = 1;
 
                     }
 
                     
 
                     //Check to see if you need to make a label
 
                     if(clearBuffer || newline > 0)
 
                     {
 
                         //Clear the buffer if the style changes or there is a newline
 
                         GUILayout.Label(output, fontStyle);
 
      
 
                         //Add in trailing spaces
 
                         int spaces = output.Length - output.TrimEnd(' ').Length;
 
                         //Might have to adjust this constant
 
                         GUILayout.Space(spaces * 5.0f);
 
                         //And also count that space in the pixel size of the buffer...
 
                         pixelsPerLine += spaces*avgPpC;
 
                         
 
                         //Clear the buffer and cleanup
 
                         output = "";
 
                         clearBuffer = false;
 
                         fontStyle.normal.textColor = textColor;
 
                         if(newFont != null)
 
                         { 
 
                             fontStyle.font = newFont; 
 
                             newFont = null; 
 
                         }
 
                     }
 
      
 
                     //You might have multiple newlines since the last label was created
 
                     //ie if you do multiple newlines in a row
 
                     while(newline > 0)
 
                     {
 
                         //Create a new line by ending the horizontal layout 
 
                         if(alignment == TextAlignment.Left || alignment == TextAlignment.Center)
 
                         { 
 
                             GUILayout.FlexibleSpace(); 
 
                         } 
 
                         GUILayout.EndHorizontal(); 
 
                         GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true), 
 
                                                     GUILayout.Width(rect.width), 
 
                                                     GUILayout.MinWidth(rect.width));          
 
                         if(alignment == TextAlignment.Right || alignment == TextAlignment.Center)
 
                         { 
 
                             GUILayout.FlexibleSpace(); 
 
                         }
 
                         
 
                         //You have to include this label, otherwise the newline will be
 
                         //at the same place as the last one.
 
                         if(newline > 1) GUILayout.Label(" ", fontStyle);
 
                         
 
                         --newline; 
 
                         pixelsPerLine = 0;
 
                     }
 
                     
 
                     //Write the new stuff to the buffer
 
                     output += toks[i].Substring(index, index2-index);
 
                     pixelsPerLine += (index2-index)*avgPpC;
 
                     index += index2-index;
 
                 }
 
             }
 
         }
 
         
 
         
 
         //Clear the buffer one last time
 
         GUILayout.Label(output, fontStyle);
 
         
 
         if(alignment == TextAlignment.Left || alignment == TextAlignment.Center)
 
         { 
 
             GUILayout.FlexibleSpace(); 
 
         } 
 
         GUILayout.EndHorizontal(); 
 
         GUILayout.FlexibleSpace(); 
 
         GUILayout.EndVertical(); 
 
         GUILayout.EndArea();   
 
     }
 
 }
 
 

I'm trying to use GUILayout.Label(output, fontStyle); (created twice in the script above) for the label in the scrollview. The label works perfectly but the scrollview doesn't.

If anything's unclear please let me know and I'll try to clarify it.

Thanks in advance!

more ▼

asked Jan 30, 2012 at 07:47 PM

thurone gravatar image

thurone
50 23 18 20

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x247
x118

asked: Jan 30, 2012 at 07:47 PM

Seen: 1533 times

Last Updated: Jan 30, 2012 at 07:47 PM