I’m making a console for my game and I recently faced one problem. I added lot’s of information to the string and then displayed that information inside UI text. The problem is that the string became too long for TextMeshGenerator. So I came up with some solution. To set maximum characters for each string and when the string reaches that maximum amount create new UI text. The issues is that how do I split / cut the string? For example I need to add 1000 characters to the string so I, add for example 800 of characters until the first string is full (reached max amount of characters) and I add the rest 200 of the characters to the new created string.
Can someone give me some tips on how to achieve this? Thank you!
Also how much characters UI text / string can contain, until it starts to give errors?
You could use string.Substring(int, index, int length).
string string1 = stringToSplit.Substring(0,800);
//will get the first 800 characters
string string2 = stringToSplit.Substring(800,200);
//will get 200 characters starting from the 800th character
You could even make a method that splits your string and returns an array/list of strings.
I don’t know what’s the cap of UI text in characters.
I came up with a way to procedurally split up strings easily. Hope this solution will work for you!
// The entire string which you want to use
public string info;
// The maximum length of the string
public int maxStringLength = 800;
// A list to hold all of the split strings
public List<string> splitStrings;
private void Start()
{
// Iterate throught the entire strings length.
for (int i = 0; i < info.Length; i += maxStringLength)
{
// Split the string into Substrings
string split = (info.Substring(i, Mathf.Min(maxStringLength, info.Length - i)));
// Add the string to the 'splitStrings' list.
splitStrings.Add(split);
}
}