- Home /
how to make text that is writen automatically, letter by letter
I have a little problem.
How can i make a text that is written by himself. I mean that i have the text, but to make it as an animation like this. ModernCombat2 As you can see in the first seconds, the text is written by himself. how can i do that in my own game?
Answer by Bunny83 · Mar 02, 2011 at 12:09 AM
Well, that's quite simple. Just store your text in a script variable. A coroutine can copy it char by char.
edit: Added additionalLines variable
It should work... not tested!
var currentPosition : int = 0;
var Delay : float = 0.1; // 10 characters per sec.
var Text : String = "";
var additionalLines : String[];
function WriteText(aText : String) {
guiText.text = "";
currentPosition = 0;
Text = aText;
}
function Start(){
for ( var S : String in additionalLines )
Text += "\n" + S;
while (true){
if (currentPosition < Text.Length)
guiText.text += Text[currentPosition++];
yield WaitForSeconds (Delay);
}
}
@script RequireComponent(GUIText)
From within this script just use:
WriteText("This is the first line \n this is the second line \n maybe a third?");
To set the text from another script you have to get your script instance first. Note: in this example the script is named "AutoText".
GetComponent.<AutoText>().WriteText("first line \n second line");
To write a new text just call WriteText() with your text as parameter. This script uses a GUIText component.
But how can i write the second lline? or should i made a new gameobject?
You can make another one or if you assign the text via script add a "newline" character "\n" between the two lines. But that works only if assigned from a script, not in the inspector. I can add a string array so you can assign multiple lines in the inspector. I'll edit my answer. http://forum.unity3d.com/threads/1334-multiple-lines-in-a-GUIText-object
Just what ever you want: Open google, type "Unity " and just append what you want to know :D
You still don't understand the intention of this site. This site collects "common" questions related to unity. The question you can ask here have to be "of interest to at least one other Unity user somewhere"! Did you even read the FAQs? And even like on every other forum you should search for a similar question before you ask the same question over and over again. This page is like wikipedia. The higher ranked users can edit others questions and answers. As i said there are only a few rules here and they aren't complicated. Sure, everybody needs some time to get into it.
Answer by lampd1 · Jun 22, 2015 at 07:24 AM
Here's a C# example that works in Unity 5.1.1 using Unity's new UI system:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextTyper : MonoBehaviour {
public float letterPause = 0.2f;
public AudioClip typeSound1;
public AudioClip typeSound2;
string message;
Text textComp;
// Use this for initialization
void Start () {
textComp = GetComponent<Text>();
message = textComp.text;
textComp.text = "";
StartCoroutine(TypeText ());
}
IEnumerator TypeText () {
foreach (char letter in message.ToCharArray()) {
textComp.text += letter;
if (typeSound1 && typeSound2)
SoundManager.instance.RandomizeSfx(typeSound1, typeSound2);
yield return 0;
yield return new WaitForSeconds (letterPause);
}
}
}
SoundManager and Loader scripts (based on the examples found in Roguelike tutorial) found here: https://gist.github.com/reidblomquist/c22687bf22238d46447a
Thanks i really needed this for my next game. well done ;)
I can't get this code to work for me...I'm using Unity 5.1.1. I've already created an empty Gameobject and gave it a GUIText component, dragged this code/script as a new component on the empty GameObject. Did I miss something?
This is a revision of the previous answers that adds typing sound effects. You most likely missed the additional scripts from the OP's cough clickbait cough tutorial.
Lol, since when is a gist clickbait? Do you even internet? Would you rather me give a link to the official unity3d tutorial those assets are from? https://learn.unity.com/project/2d-roguelike-tutorial
Thank ya very much, you will probably be in credits of my game.
@lampd1 How are you handling rich text like colored texts or different fonts? if you write character by character, this is a problem.
Answer by AVividLight · Mar 01, 2011 at 11:45 PM
You could buy this. It will open Unity and the the asset store...
Answer by bnmindstorm · Jun 30, 2012 at 06:59 PM
If you want, the extension "TTF Text" that generates text meshes does it directly out of the box, with sound, and for any layout and font. Various other effects are also possible. Check this video : http://youtu.be/RzdjQStYkmU
Answer by LoungeKatt · Aug 25, 2014 at 02:08 AM
#pragma strict
public var output : String = "";
var Delay : float = 0.05; // 10 characters per sec.
private var Text : String[];
public var doubleSpace : boolean = false;
function WriteText(aText : String[]) {
output = "";
Text = aText;
// TypeText();
StartCoroutine("TypeText");
}
function TypeText () {
for (var word : String in Text) {
for (var letter in word.ToCharArray()) {
output += letter;
yield WaitForSeconds (Delay);
}
output += "\n";
if (doubleSpace)
output += "\n";
}
}
Attach this script to any GameObject that contains GUI elements to be "typed" into. If a button launches the display of text, make sure to place this.GetComponent().WriteText(ARRAY_OF_TEXT);
within the button execution. If no button is being used, this line may also be placed in the Start function.
ARRAY_OF_TEXT
should be a standard string array separated by line. The text parameter for the GUIText, GUILabel, or GUIButton to have text typed into would then be this.GetComponent().output
.
Enabling double space will separate each line by two line breaks.
Your answer
Follow this Question
Related Questions
Changing text on Canvas Text is very slow / unresponsive 1 Answer
Scroll intro text 5 Answers
Cutscene(Animation) problem 0 Answers