Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active January 27, 2024 10:32
Show Gist options
  • Save mminer/1331330 to your computer and use it in GitHub Desktop.
Save mminer/1331330 to your computer and use it in GitHub Desktop.
Simple Unity script to create a scrolling marquee.
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 message's rect if we haven't already.
if (messageRect.width == 0) {
var dimensions = GUI.skin.label.CalcSize(new GUIContent(message));
// Start 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 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);
}
}
@xlan2020
Copy link

xlan2020 commented Sep 4, 2022

Thank you!

@13ruce1337
Copy link

I've recently run into this and with the new UIDocuments the above method didn't work well for me. This was my fix. Hope it helps someone.

    private IMGUIContainer container;
    private const float scroll_speed = 0.001f;

    private void OnEnable()
    {
        container = GetComponent<UIDocument>().rootVisualElement.Q<IMGUIContainer>("footer");
        StartCoroutine(AutoScroll());
    }
    private IEnumerator AutoScroll()
    {
        float position = 100f;
        while (position >= -100f)
        {
            container.style.left = Length.Percent(position);
            position = (position <= -9f) ? 100f : position -= 0.01f;
            yield return new WaitForSeconds(scroll_speed);
        }
    }

from there you can do stuff like container.Add(message_label); assuming Label message_label;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment