JavaScript WordWrap for 3D text

Just attach to your 3D text and set the properties, works fine for most purposes. Doesn’t account for Non-Mono-spaced font issues.

Feel free to improve upon it, I just needed this for a project and it met my needs and performance metrics.

// Word Wrap v1.0 for Unity 3D Text Objects
// 
// Author: Michael Colin Voigt
// [url]http://3037.com[/url]
// Free for all purposes, if you use it, just email me at [email]info@3037.com[/email]

public var textObject:GameObject;
public var wrapPoint = 0;
public var currentText:String;
public var hyphen = true;

function Awake () 
{

	currentText = " " + currentText;
	
	var finalString:String ="";

	var currentWrap = wrapPoint;
	var count = 0;
	var startPoint = 0;
	var lastBlankSpace = 0;
	var currentLength = 0;
	var appendText:String = "";

	while( count <= currentText.Length  )
	{

		count++;
	
		if( count   >= currentText.Length)
		{
				var finalLength = currentText.Length - startPoint;
				finalString +=  currentText.Substring( startPoint+1 , finalLength-1 );
				break;
		}
	
		if( currentText[count] == " " )
		{
			lastBlankSpace = count;
		}
	
		if( currentText[count] == " "  count >= currentWrap )
		{
	
			currentLength = count - startPoint;
		
			appendText = currentText.Substring( startPoint+1 , currentLength ) + "\n" ;
			finalString += appendText;
		
			currentWrap = count + wrapPoint;
			startPoint = count;
	
		}
		
		if( currentText[count] != " "  count >= currentWrap )
		{
			
			for(var i = count; i > startPoint;i--)
			{
				
				
				if( currentText[i] == " " ) 
				{
					
					
				Debug.Log(currentText[i]);
					
					currentLength = i - startPoint;
		
					appendText = currentText.Substring( startPoint+1 , currentLength ) + "\n" ;
					finalString += appendText;
		
					currentWrap = i + wrapPoint;
					startPoint = i;
	
				}
				
			}
		}
		
		
		if(hyphen)
		{
			var worldLength = (count - lastBlankSpace);
	
			if(  worldLength  >    wrapPoint )
			{
				currentLength = count - startPoint;
		
				appendText = currentText.Substring( startPoint+1 , currentLength ) + "-\n" ;
				finalString += appendText;
		
				currentWrap = count + wrapPoint;
				startPoint = count;	
				lastBlankSpace = count;	
			}
		}
	}

textObject.GetComponent (TextMesh).text = finalString;

}

I hope no one minds if I bump this, but I was wondering if there was a way for the script to reset the wrapPoint when it detects a line break in the string. This script is working great so far, it’s just that every time I line break it still thinks that it’s in the same line as before.

DESIRED EFFECT:
This is the first sentence.
This is the second sentence.

CURRENT EFFECT:
This is the first sentence.
This is the
second sentence.

UPDATE: I’ve figured out a way around it! I’ve added an if statement in the script after the hyphen if statement and before the get component part:

if( message[count] == "`" ){
     currentLength = count - startPoint;
     appendText = message.Substring( startPoint+1 , currentLength-1 ) + "\n" ;      
     finalString += appendText;
     currentWrap = count + wrapPoint;
     startPoint = count;
}

Now whenever I put in the string, it will always line break and reset the count (wrapPoint) to 0. If you have to put in your sentence, you could replace it with any symbol or letter you don’t really use like ~ or *. Just make sure it’s ONE symbol, because if you put more than one it the if statement it won’t register.