- Home /
How to implement Text Mesh text wrapping?
Hello,
I've created a keyboard in-game that the player can type with and I'm currently using a text mesh to represent the the text they type. I'm trying to limit the number of characters that can be entered on a single given "line" in the mesh. The idea will be that if a line runs too long, the script will find the next space after it goes over the limit and replace it with a line break. However, while making this I have ran into a few problems. The primary issue I've been running into is the "Cannot apply indexing with [] to an expression of type 'method group'" error as well as the "Cannot convert type 'char' to 'string'" error. Here's what I have so far:
using UnityEngine;
using System.Collections;
public class TextWrap : MonoBehaviour {
// Use this for initialization
public TextMesh text;
void Start () {
}
// Update is called once per frame
void Update () {
text.text = ApplyWrap(text.text, 14);
}
private string ResolveTextSize(string input, int lineLength){
int i=0;
while((string)input[i]!='\n'){
i++;
if(i>=lineLength){
if(input[i]==' '){
input[i]='\n';
}//end if
}//end if
}//end while
return input;
}//end ApplyWrap
}//end TextWrap
Thanks, Jesse
Answer by Bioinformatizer · Jun 17, 2015 at 01:24 AM
Hi Jesse, I took a look at your code, but, it is difficult to give you a complete answer without knowing how you are calling the ResolveTextSize and what ApplyWrap is. The code below can't really be tested until those pieces of information are evident, but, there are somethings that are altered that may help :
using UnityEngine;
using System.Collections;
public class TextWrap : MonoBehaviour {
// Use this for initialization
public TextMesh text;
void Start () {
}
// Update is called once per frame
void Update () {
text.text = ApplyWrap(text.text, 14); //What is applywrap?
}
//what calls ResolveTextSize?
private string ResolveTextSize(string input, int lineLength){
for( int i = 0; i < input.Length; i++){
if(i>=lineLength){
if(input[i]==' '){
input[i]="\r\n";
}//end if
}//end if
}//end for
return input.ToString;
}//end ApplyWrap
}//end TextWrap
For one, it is usually better to use a for loop if all of the information is already available and being passed into a function, the '.length' method will give you the int you need.
The '.toString' Method may help out your char not a string issue....maybe, again that is an issue of insufficient information. But, if it isn't that it may be how you are passing the input to the function.
Answer by Godnir · Jul 31, 2016 at 12:18 PM
if(input[i]==' '){
input[i]="\r\n";
}//end if
You're not allowed to assign a string into a char. input[i] = char "\r\n" = string
Your answer
Follow this Question
Related Questions
Finding a character unicode in C# 0 Answers
Error CS0029 fix 1 Answer
int to char conversion in JS ? 2 Answers
Replace chars in String with a Dictionary and avoid order-sensitive problems 0 Answers
How to store a tab character in an inspector field 0 Answers