- Home /
Apply style to specific string in text area?
Can this be done at all? I'm making a pseudo terminal and would like some highlighting on specific words if possible. I'm happy to apply a material to certain strings and add them to the text area rather than insert an entire string. Because of the complexities of doing this I only use one text area. I'd think multiple text areas would be out of the option.
Answer by brain56 · Nov 15, 2013 at 09:02 AM
No, that is not possible with the default text rendering facilities Unity 3D currently provides.
What you can do is use different text objects to render each part of the string with a different material. But I'd also recommend you take a look at third party Unity plug-ins for text. I would recommend *Toolkit 2D* and *NGUI*.
I'm looking for a free option preferably as I'll not be making revenue with this. Can I append text objects and strings into the same text area? If so ill likely be able to make that work.
Answer by MrVerdoux · Nov 15, 2013 at 09:30 AM
I can only think of doing it by extending GUI methods to add a "sequential" GUI option or something like that, but in my opinion that is not a very good solution, only that I can´t think of anything else.
using UnityEngine;
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
public static class GUIExtension{
public static void SequentialLabel(this GUI gui, Rect rect, List<StyledString> styledStrings){
float recorredWidth = 0;
for (int i=0; i<styledStrings.Count; i++){
Size stringSize = TextRenderer.MeasureText(styledStrings[i].text, styledStrings[i].style.Font.size);
GUI.Box(new Rect(rect.x + recorredWidth, rect.y, rect.width, rect.height),
styledStrings[i].text, styledStrings[i].style);
recorredWidth += stringSize.width;
}
}
}
public class StyledString{
public string text {get; private set;}
public GUIStyle style {get; private set;}
public StyledString(string text, GUIStyle style){
this.text = text;
this.style = style;
}
}
Note: I have a problem with autocompletion right now so probably the code needs fixing. Oh, and that is only for "one line" label, if you want more lines you will have to do a line end check.
Thanks I was unaware I could apply styles like that I'll see what I can come up with and post back. I'm not overly concerned about optimization or anything given the nature of the code.
The way to call it would be:
GUI.SequentialLabel(new Rect(left,top,width,height),myStringList);
But again, I´m not very sure this would work...
Are you able to confirm what package Size is in? I've added the Windows.Forms dll to my project but I'm still getting a namespace Error for Size. EDIT: It's in drawing....
It is in System.Drawing
http://msdn.microsoft.com/en-us/library/system.drawing.size%28v=vs.110%29.aspx
EDIT: I didn´t see you edit, hahaha
Your answer
