I hear it can’t be done: “GUIText does not have word wrap, skins, or anything else from OnGUI. GUIText and GUITextures are separate objects, from the Unity 1.x days, though they are still useful for some things since OnGUI can be problematic depending on what you’re doing.
–Eric”
However, that was a couple years ago – is it possible yet? I can’t use GUILayout because I need to be able to put the text underneath other object.
function FormatString ( text : String ) {
words = text.Split(" "[0]); //Split the string into seperate words
result = "";
for( var index = 0; index < words.length; index++)
{
var word = words[index].Trim();
if (index == 0) {
result = words[0];
block.text = result;
}
if (index > 0 ) {
result += " " + word;
block.text = result;
}
TextSize = block.GetScreenRect();
if (TextSize.width > lineLength)
{
//remover
result = result.Substring(0,result.Length-(word.Length));
result += "
Not sure if anyone still needs this but I used the code from sfbaystudios, and modified it in C# so that it also takes in the line width. Heres my code:
string wrapString(string msg, int width) {
string[] words = msg.Split (" " [0]);
string retVal = ""; //returning string
string NLstr = ""; //leftover string on new line
for (int index = 0 ; index < words.Length ; index++ ) {
string word = words[index].Trim();
//if word exceeds width
if (words[index].Length >= width+2) {
string[] temp = new string[5];
int i = 0;
while (words[index].Length > width) { //word exceeds width, cut it at widrh
temp *= words[index].Substring(0,width) +"
I tried using GUIText too but it doesnt have word wrapping feature. The simplest thing to do is use GUIStyle and customize the font in the inspector. Just make sure to check Word Wrap in the inspector. This is my code:
private var descriptionText : String;
var descriptionStyle : GUIStyle;
function Start()
{
descriptionText = "Multi-line text here";
}
function OnGUI()
{
GUI.Button (Rect (10,70, 100, 20), descriptionText, descriptionStyle);
}