- Home /
Marquee.cs speeds up scrolling with keydown
Using the Marquee.cs from Unity Wiki speeds up the scrolling. Any ideas why? The script is really short and I've been through it a few times, but I'm not sure why it does this.
using UnityEngine;
public class Marquee : MonoBehaviour { public string message = "Where we're going, we don't need roads."; public float scrollSpeed = 50;
Rect messageRect;
void OnGUI ()
{
// Set up the message's rect if we haven't already
if (messageRect.width == 0) {
Vector2 dimensions = GUI.skin.label.CalcSize(new GUIContent(message));
// Start the message past the left side of the screen
messageRect.x = -dimensions.x;
messageRect.width = dimensions.x;
messageRect.height = dimensions.y;
}
messageRect.x += Time.deltaTime * scrollSpeed;
// If the message has moved past the right side, move it back to the left
if (messageRect.x > Screen.width) {
messageRect.x = -messageRect.width;
}
GUI.Label(messageRect, message);
}
}
Answer by Mike 3 · Sep 27, 2010 at 08:46 AM
It'll do the same when you move the mouse or click too - OnGUI is called multiple times a frame depending on the input
A potential fix would be to only move the rect's x value in the repaint event:
void OnGUI () { // Set up the message's rect if we haven't already if (messageRect.width == 0) { Vector2 dimensions = GUI.skin.label.CalcSize(new GUIContent(message));
// Start the message past the left side of the screen
messageRect.x = -dimensions.x;
messageRect.width = dimensions.x;
messageRect.height = dimensions.y;
}
if (Event.current.type == EventType.Repaint)
{
messageRect.x += Time.deltaTime * scrollSpeed;
// If the message has moved past the right side, move it back to the left
if (messageRect.x > Screen.width) {
messageRect.x = -messageRect.width;
}
}
GUI.Label(messageRect, message);
}
Heh, midway through the same answer. Would rather that be the case than for this place to be a ghost town! Upvoted.
Wow. I see that that would cause a problem. Thank you very much to both of you. That had been quite a peculiar incident.
Your answer
Follow this Question
Related Questions
Changing Speed in Gameplay Affects GUI 2 Answers
guiText.HitTest(Input.mousePosition) is not triggering 0 Answers
Keypress speeds up fade? 1 Answer
Make String Update for Value (GUI) 1 Answer
Having a speed counter 2 Answers