Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Berenger · Jan 04, 2011 at 10:52 AM · guilayoutlabelguistylefontsizewordwrap

GUILayout.Label - WordWrap and FontSize issue.

Hi,

I'm trying to implement a popup code for my game (something like right clic an item, you get its stats). The idea is a script with a list of stuff to display, like a texte, an image etc.

The problem is with the text. I want to be able to choose the fontSize to have titles, and at the same time I need the words to wrap. I don't know why, but those things don't work together; When I use wordWrap = true, all entries are the same size ! I take the part of the code with the problem, so you can test it easily :

//PopUp.js //This class is a displayable element used by Popup class. It just display a text, with specific font size and color. class PopUpText { private var txt : String; private var police : int; private var color : Color; private var style : GUIStyle;

 function PopUpText(  s : String, p : int, c : Color, st : GUIStyle  ){ txt = s; police = p; color = c; style = st; }
 function Display()
 {   
     var save_police = style.fontSize;
     var save_color = style.normal.textColor;

     style.fontSize = police;
     style.normal.textColor = color;

     GUILayout.Label( txt, style );

     style.fontSize = save_police;
     style.normal.textColor = save_color;
 }

}

// This script contain severals elements that can be displayed one after the other. class PopUp extends MonoBehaviour { var w : int = 100; // The width of the area var h : int = 500; // The height of the area

 var useFlexibleSpace = false;   // Should I use a flexibleSpace at the end of the entries ?
 var wordWrap = false;               // Should I use wordWrap ?


 private var elements : PopUpText[];
 private var style : GUIStyle = new GUIStyle();  

 function OnGUI()
 {           
     style.wordWrap = wordWrap;

     var R : Rect = Rect( 0, 0, w, h );

     GUI.Box( R, "" ); // Background

     //Display all elements
     GUILayout.BeginArea( R );
         for( var E in elements )
             E.Display();        

         if( useFlexibleSpace)
             GUILayout.FlexibleSpace();
     GUILayout.EndArea();
 }

 // Set up, so you can test it quickly
 function Start()
 {
     elements = new PopUpText[4];
     elements[0] = new PopUpText( "First entry", 25, Color.yellow, style );
     elements[1] = new PopUpText( "Je crois que c'est vraiment le mode de vie V. Ca va tre trs intressant de voir les diffrentes dynamiques entre Lisa (Laura Vandervoort) et Erica (Elizabeth Mitchell), entre Anna et Lisa et entre Anna et sa mre. Cela va permettre de comparer diffrents exemples de maternit.", 15, Color.white, style );
     elements[2] = new PopUpText( "Second entry", 25, Color.yellow, style );
     elements[3] = new PopUpText( "Voici la recette de la Pte  crpe que j'ai slectionn pour vous. Elle est simple  raliser mme pour un dbutant en cuisine et prend peu de temps  prparer. Pour vous aider j'ai essay d'tre le plus clair possible, en vous guidant tape par tape pour la prparation de vos crpes", 15, Color.white, style );
 }

}

You might have accent problems, never mind the meaning of the text is not relevant (it's an interview of an actress of TV show "V", and a recipe ^^).

[ EDIT : Problem solved, the code below is fixed ]

So the problem was the shared GUIStyle. The proper way to do it seems to start from a root style for the font, aligment etc, then make a copy of it for each element so we can change the police individually. Here is the code, plus some parameters to play around with font size.

//PopUp.js //This class is a displayable element used by Popup class. It just display a text, with specific font size and color. class PopUpText { private var txt : String; private var police : int; private var color : Color; private var style : GUIStyle;

 function PopUpText(  s : String, p : int, c : Color, st : GUIStyle  )
 { 
         txt = s; 
         police = p; 
         color = c; 
         style = new GUIStyle( st ); //Copy, here is what solved the problem
 }
 function Display()
 {   
     style.normal.textColor = color;
     style.fontSize = police;
     GUILayout.Label( txt, style );
 }
 function SetPolice( p : int ) { police = p; }

}

// This script contain severals elements that can be displayed one after the other. class PopUp extends MonoBehaviour { var areaRect : Rect = Rect( 0, 0, 200, 500 );

 var font : Font;
 var textSize0 : int = 25;
 var textSize1 : int = 15;
 var textSize2 : int = 25;
 var textSize3 : int = 15;


 private var elements : PopUpText[];
 private var style : GUIStyle = new GUIStyle();  

 function OnGUI()
 {           
     GUI.Box( areaRect, "" ); // Background

     elements[0].SetPolice( textSize0 );
     elements[1].SetPolice( textSize1 );
     elements[2].SetPolice( textSize2 );
     elements[3].SetPolice( textSize3 );

     //Display all elements
     GUILayout.BeginArea( areaRect );
         for( var E in elements )
             E.Display();        

         GUILayout.FlexibleSpace();
     GUILayout.EndArea();
 }

 // Set up, so you can test it quickly
 function Start()
 {
     style.font = font;
     style.wordWrap = true;
     elements = new PopUpText[4];
     elements[0] = new PopUpText( "First entry", textSize0, Color.yellow, style );
     elements[1] = new PopUpText( "Je crois que c'est vraiment le mode de vie V. Ca va tre trs intressant de voir les diffrentes dynamiques entre Lisa (Laura Vandervoort) et Erica (Elizabeth Mitchell), entre Anna et Lisa et entre Anna et sa mre. Cela va permettre de comparer diffrents exemples de maternit.", textSize1, Color.white, style );
     elements[2] = new PopUpText( "Second entry", textSize2, Color.yellow, style );
     elements[3] = new PopUpText( "Voici la recette de la Pte   crpe que j'ai slectionn pour vous. Elle est simple   raliser mme pour un dbutant en cuisine et prend peu de temps   prparer. Pour vous aider j'ai essay d'tre le plus clair possible, en vous guidant tape par tape pour la prparation de vos crpes", textSize3, Color.white, style );
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jan 05, 2011 at 09:39 AM 0
Share

It seems that the problem of the fontSize came from the font. However, with an other (for instance, Time new roman), if the size of the font is ok, the size of the label displaying it don't follow, and the text is cropped (or over other label, if you use "overflow". Here is an image that show my point : http://img718.imageshack.us/i/labelissue.jpg/

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jason B · Jan 07, 2011 at 09:01 PM

Sorry if I'm misunderstanding, but you might want to consider splitting them up into 4 separate GUI panels, otherwise I'm pretty sure that putting varying text sizes in the same text box is bound to cause problems, like bigger text adhering to and getting cut off by the smallest sized text. To see what I mean, make your small text larger and I'm guessing you'll see that less of the large text gets cut off.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jan 07, 2011 at 11:03 PM 0
Share

Yep, you might be right. The part part where I switch between fontSize seems to screw things up. I continue to look that, if anyone have a suggestion on the good way to do it, I'll be glad to take it !

avatar image Berenger · Jan 07, 2011 at 11:18 PM 0
Share

Ok it works fine, thanks a lot !

avatar image
1

Answer by Waz · Oct 07, 2011 at 10:28 PM

Pixels answer here seems a better workaround for what is obviously a Unity bug. i.e. explicitly set the font size too.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

[SOLVED]GUIStyle Word Wrap Check 1 Answer

Using GUILayout to arrange a text field next to a label 0 Answers

GUILayout.Label alignment / spacing 1 Answer

Gui.Label chat system direction of label "stacking" 0 Answers

How can I enable word wrapping for GUI? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges