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?

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
foreach (WordStylePair pair in words){
    //if no more room to print the next word

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.


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.