To use the text mesh wrapper snippet create a new 3D Text component in unity and then create a new C# script and name it as "WrapText" and replace the content with the below code snippet.
Then attach the code to the 3D text mesh and under the 'Text Mesh" field in Wrap text component drag the same 3D text mesh.
Under the Max Line Chars field you can set the length of a particular sentence.
That's it.
-------CODE SNIPPET-------------------COPY THE BELOW CONTENT-------------------------
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class WrapText : MonoBehaviour
{
public TextMesh textMesh;
public int maxLineChars = 35;
private int lastMaxLineChars = 35;
string wrappedText = "";
public bool disableWhilePlaying = true;
void Awake()
{
// bool shouldDisable = disableWhilePlaying !(Application.isEditor !Application.isPlaying);
// this.enabled = !shouldDisable;
}
void Update()
{
if (textMesh.text != wrappedText || maxLineChars != lastMaxLineChars)
{
int charCount = 0;
wrappedText = "";
string line = "";
char[] separators = new char[] { ' ', '\n', '\t' };
string[] words = textMesh.text.Split(separators);
for (int i = 0; i < words.Length; i++)
{
string word = words[i].Trim();
if (i == 0)
{
line = word;
charCount = word.Length;
}
else
{
if ((charCount + (charCount > 0 ? 1 : 0) + word.Length) <= maxLineChars)
{
if (charCount > 0)
{
line += ' ';
charCount += 1;
}
line += word;
charCount += word.Length;
}
else
{
if (wrappedText.Length > 0)
wrappedText += '\n';
wrappedText += line;
line = word;
charCount = word.Length;
}
}
}
if (charCount > 0)
{
if (wrappedText.Length > 0)
wrappedText += '\n';
wrappedText += line;
}
textMesh.text = wrappedText;
lastMaxLineChars = maxLineChars;
}
}
}
Then attach the code to the 3D text mesh and under the 'Text Mesh" field in Wrap text component drag the same 3D text mesh.
Under the Max Line Chars field you can set the length of a particular sentence.
That's it.
-------CODE SNIPPET-------------------COPY THE BELOW CONTENT-------------------------
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class WrapText : MonoBehaviour
{
public TextMesh textMesh;
public int maxLineChars = 35;
private int lastMaxLineChars = 35;
string wrappedText = "";
public bool disableWhilePlaying = true;
void Awake()
{
// bool shouldDisable = disableWhilePlaying !(Application.isEditor !Application.isPlaying);
// this.enabled = !shouldDisable;
}
void Update()
{
if (textMesh.text != wrappedText || maxLineChars != lastMaxLineChars)
{
int charCount = 0;
wrappedText = "";
string line = "";
char[] separators = new char[] { ' ', '\n', '\t' };
string[] words = textMesh.text.Split(separators);
for (int i = 0; i < words.Length; i++)
{
string word = words[i].Trim();
if (i == 0)
{
line = word;
charCount = word.Length;
}
else
{
if ((charCount + (charCount > 0 ? 1 : 0) + word.Length) <= maxLineChars)
{
if (charCount > 0)
{
line += ' ';
charCount += 1;
}
line += word;
charCount += word.Length;
}
else
{
if (wrappedText.Length > 0)
wrappedText += '\n';
wrappedText += line;
line = word;
charCount = word.Length;
}
}
}
if (charCount > 0)
{
if (wrappedText.Length > 0)
wrappedText += '\n';
wrappedText += line;
}
textMesh.text = wrappedText;
lastMaxLineChars = maxLineChars;
}
}
}
No comments:
Post a Comment