- Home /
Scrolling Text
How do I make a line of GUI Text scroll, repeating continuously, to the left of the screen? I prefer to use Javascript, and I tried a < marquee> tag. This is HTML, but when I format it to fit Javascript, It does not work correctly. Any help will be much appreciated!
Unity doesn't use Javascript, it uses Unityscript, which is more program$$anonymous$$g-language and less webpage-speak. They call it Javascript to make it sound more familiar - it has some similarities, but you can't just take a .js webpage and stick it in Unity. :(
I'm pretty sure a marquee GUI element is a non-trivial concept; you'd probably have to alter the displayed string itself to get it to scroll, or use some trickery to write an image over the top of the text as you scroll the element itself.
Answer by robertbu · Feb 04, 2013 at 07:25 PM
Here is a script that scrolls a GUI label. It does as @Loius suggested by altering the display string (i.e. it scrolls by characters not pixels). Attach it to an empty game object and set the string, time, and rect. Note that the rectangle needs to be larger than is needed for the string to display on the screen if you want to avoid word clipping. I think you can also turn off the clipping by using a GUI style and setting the clipping property.
#pragma strict
var charactersPerSecond : float = 8.0;
var text : String = "Here is some text to scroll";
var rect : Rect;
private var textUsing : String;
private var scrollBasis : String;
private var scrollText : String;
private var currChar : int = 0;
private var timer : float = 0.0;
function Start () {
NewText();
}
function Update () {
if (textUsing != text)
NewText();
var secondsPerCharacter : float = 1.0 / charactersPerSecond;
if (timer > secondsPerCharacter) {
var iT : int = Mathf.FloorToInt(timer / secondsPerCharacter);
currChar = (currChar + iT) % textUsing.Length;
timer -= iT * secondsPerCharacter;
scrollText = scrollBasis.Substring(currChar, textUsing.Length);
}
timer += Time.deltaTime;
}
function OnGUI() {
GUI.Label(rect, scrollText);
}
function NewText() {
textUsing = text;
scrollBasis = textUsing+textUsing;
currChar = 0;
scrollText = scrollBasis.Substring(currChar, textUsing.Length);
timer = 0.0;
}
Can you translate into c#??? Only know basic Javascript...