- Home /
How can i split text file in unity and then spawn it as UI TEXT in game?
For example, I have a text file: small little girl lived in deep space. Her parents died, when she was three years old, since that time, she´s browsing space around her, finding new friends.
I need to spawn this text by words with one second delay...
it´s also can be done by creating new game object for each word, but i am sure, that it can be more easily..
it should works like this :
Small (1sec) little (1sec) girl (1sec)
lived(1sec) etc...
Thank you for answer
Answer by stepan-stulov · Aug 09, 2015 at 12:35 PM
[SerializeField] private TextAsset _myTextAsset;
List<string> words = _myTextAsset.text.split(" ").ToList();
And then run some sort of timer or coroutine over the words list storing current index.
Thank you... i tried to do it as you say, but i have problem.. i am new in unity, so sorry, if my question is stupid... but is it possible to instantiate new text object directly from script? for example to do something like this? i didn't make a coroutine yet, but i know, how to make it.. =]
List words = _myTextAsset.text.split(" ").ToList();
public float distanceX; // space between words
public float distanceY; // space between lines
public float endOfLine$$anonymous$$ultiplier; // multiplier for word.length
public float endOfLineIdentifier; // identifying end of line
private float defaultYtransform;
private float defaultXtransform;
private float previousXTransform;
private float previousYTransform;
foreach string word in words // going throw list
{
private text = new TextObject;
text = word;
if (endOfLineIdentifier >= (previousXTransform + distanceX + word.Length * endOfLine$$anonymous$$ultiplier)) // if end of line != 1 write word to this line
{
text.Transform = (previousXTransform + distanceX + word.Length * endOfLine$$anonymous$$ultiplier, previousYTransform , 0)
}
else // if end of line = 1 , return to start of second line
{
text.Transform = (defaultXtransform,defaultYtransform + distanceY,0)
defaultYtransform = (defaultYtransform + distanceY)
}
Instantiate text;
Answer by Quartsik · Aug 23, 2015 at 01:12 PM
Thank you.. I tried to make it as you say, but i have a problem. is it possible to make a new text object directly from script? i am sorry, if my questions are stupid, i am new in unity, and i am working on one project and i try to learn it by way. for example... is little bit possible to make a script like this? i know some syntax is bad, but i think, you will understand, what i am trying to do...
List<string> words = _myTextAsset.text.split(" ").ToList();
public float distanceX; // space between words
public float distanceY; // space between lines
public float endOfLineMultiplier; // multiplier for word.length
public float endOfLineIdentifier; // identifying end of line
private float defaultYtransform;
private float defaultXtransform;
private float previousXTransform;
private float previousYTransform;
foreach string word in words // going throw list
{
private text = new TextObject;
text = word;
if (endOfLineIdentifier >= (previousXTransform + distanceX + word.Length * endOfLineMultiplier)) // if end of line != 1 write word to this line
{
text.Transform = (previousXTransform + distanceX + word.Length * endOfLineMultiplier, previousYTransform , 0)
}
else // if end of line = 1 , return to start of second line
{
text.Transform = (defaultXtransform,defaultYtransform + distanceY,0)
defaultYtransform = (defaultYtransform + distanceY)
}
Instantiate text;
Answer by Varaughe · Apr 05, 2020 at 04:30 PM
Did you check https://assetstore.unity.com/packages/tools/gui/fully-flexible-ui-text-148050? You can have each character as a separate UI Text and then changing it's scale, position etc.