- Home /
how can i split a word to individual letters..??
hi all.
i need some insights on how to accomplish this...
i already have an array of words.. and it is shown in the scene as GUI.Label...
once its being played.. words from the array is shown...
now.. i need some codes/tips/ideas/etc to do this..
i want the words from the array appear in the scene as individual letters...
so it'll be like...
ARRAY : BALL, CHICKEN ....
in the GUI SCENE : B . A . L . L ... C . H . I . C . K . E . N
any kind of help is appreciated...
thanks for your honorable a attention..
Answer by flamy · May 24, 2012 at 07:04 AM
//incase it is string array. /*declaration*/
var length:int=10;
var names:String[]; // some values added to the array from editor, you can add it in code too ur wish
var x=10;
var y=100;
var width=30;
var height=30;
function Start()
{
}
function OnGUI()
{
if(Event.current.type==EventType.Repaint)
{
// to display the seperate letters.
for(var i=0;i<names.Length;i++)
{
for(var j=0;j<names[i].Length;j++)
GUI.Label(Rect(x+j*width,y+i*height,width,height/*x*//*y*//*width*//*height*/),""+names[i][j]);
}
}
}
Update: changed as per @Bunny83 's suggestion
what does
THIS :
for(var j=0;j x// y// width// height/),""+names[i].ToCharArray()[j]); }
...do???
Rect(x+j*width,y+i*height,width,height)
the rect doesnt 2 things, first it prints individual letters with certain gap in x and also moves the control to the new line when the new word starts, width is the gap in horizontal axis(including the space occupied by the letter) and height is the gap in vertical axis. x and y the starting positions.
names[i].ToCharArray()[j] separates individual letters.
for(var j=0;j
Just want to add that the string class has an indexer so you can easily access single characters from a string.
for(var i = 0; i < names.Length; i++)
{
for(var j = 0; j < names[i].Length; j++)
{
GUI.Label(Rect(x + j*width, y + i*height, width, height), "" + names[i][j]);
}
}
This would save you alot overhead since you convert the whole string into a char array for each character. That means since OnGUI is called 2 times per frame for a 10 character word you convert this word 40 times into an array...
Answer by lyzard · May 24, 2012 at 06:33 AM
given a string str, you can access each character like an array e.g. str[i]. Maybe you can start with something like:
string test = "chicken";
string output = "";
for(int i=0; i<test.Length; i++)
output+=test[i]+".";
Debug.Log(output);
a brief explanation about your snippet code, please... thanks..
this snippet simply builds a string called output by iterating over each character of the string called test. The loop starts at index 0 up to index given by the length of the test string. At each iteration, output+=test[i]+"." adds the character of string test at index i and "." to the output string. It is equivalent to output=output+test[i]+"." So as variable i gets incremented, it adds first test[0]+"." (first character of test string followed by "."), test[1]+"." (second character of test string followed by ".") and so on. At the end, it outputs the built string. Hope this helps ;)
Your answer
Follow this Question
Related Questions
Splitting String only on a "space". Using my method --- FIXED 2 Answers
convert string to list of lists 1 Answer
How to convert a string to int array in Unity C# 1 Answer
Change part of a string [Solved] 3 Answers
Strings, Arrays and Split in js 1 Answer