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
0
Question by Limnage · Sep 12, 2013 at 06:21 PM · guitextfontstyle

Best method for multiple styles within textbox?

I'm trying to make a textbox where words or letters within the textbox may have different styles (i.e. italic, size, font, etc). My idea now is to use GUILayout and put a label for each word, but that seems cumbersome. Also, would GUILayout.ExpandWidth(false) ensure that the words are arranged adjacently as if they were in the same label? Should I be using GUIText or UnityGUI without layout or UnityGUI with layout? Which will be the easiest to achieve the desired effect?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamora · Sep 12, 2013 at 07:05 PM

I have a strong feeling that you would get better results with a 3rd party GUI solution such as NGUI, namely because every Label you print with UnityGUI will cost you one draw call. One label can only have one style, so depending on how many different styles you have, you might end up with a whole bunch of draw calls just from your GUI.

If you insist on using UnityGUI, something like what follows might work. The code is pseudocode to demonstrate my flow of logic. I try to make it compilable in C#, but no guarantees.

Using a label for each word might not be as cumbersome (though pretty complicated to set up) as you fear, as you can store your word-style combinations in an array, and iterate over that. I would start by creating

 public class WordStylePair : ScriptableObject{
     public string word;
     public GUIStyle style;
 
     public WordStylePair(){
         word = "";
         style = "";
     }
 
     public WordStylePair(string newWord, GUIStyle newStyle){
         word = newWord;
         style = newStyle;        
     }
 }

Then, in my GUI class I would have

 public List< WordStylePair > words = new List< WordStylePair >();
 
 //inside OnGUI
 GUILayout.BeginVertical();
 GUILayout.BeginHorizontal();
 foreach (WordStylePair pair in words){
     GUILayout.Label(pair.word, pair.style);
     //if no more room to print the next word
     {
          GUILayout.EndHorizontal();
          GUILayout.BeginHorizontal();
     }
 }
 GUILayout.EndHorizontal();
 GUILayout.EndVertical();

This method requires you to create a custom inspector to create or modify WordStylePairs. A workaround could be to maintain two separate lists, one for strings and the other for their corresponding styles, so you can work with the default inspector.

Also worth mentioning is that this method will cause one draw call per word displayed. It will be worthwhile to try to batch subsequent words with the same style together and display the combined string in one label.

EDIT:

If you only need basic italics, bolds etc, you may be able to use Unity's Rich Text to incorporate multiple styles into one GUILabel. Especially if the stylistics are static and you know them beforehand, adding them won't be a big deal.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to get the size of text? 1 Answer

Why aren't asian fonts rendering dynamically? 0 Answers

Problem with GuiFont - Not appearing 0 Answers

Trying to make Fonts Fallbacks to real Bold/Italic font data 3 Answers

Odd question... can a GUIText object be displayed upside-down? 2 Answers


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