Marquee/scrolling text with Unity 4.6 UI
Hey there,
I'd like to create a marquee/scrolling effect just like in html with the UI.Text-Component. The text should scroll to the boundaries of the rect transform and then repeat scrolling from the other side. I really don't know where to start. There are a few existing solutions for scrolling text, but they use GUI or they scroll to the end of the screen - which is not the solution I want.
Any help is appreciated! Thank you :)
Answer by Sardonic · Feb 06, 2017 at 01:44 AM
Alright @TimNick151297, so, this isn't award winning™ scripting by anybody's standard, but I ran into this situation recently, and this is what I did:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HorizontalScroll : MonoBehaviour
{
public ScrollRect myScrollRect;
public float speed;
public float refreshdelay;
void Update()
{
if (myScrollRect.horizontalNormalizedPosition < 1)
{
myScrollRect.horizontalNormalizedPosition = myScrollRect.horizontalNormalizedPosition + speed;
}
if (myScrollRect.horizontalNormalizedPosition > 1)
{
StartCoroutine(refresh());
}
}
IEnumerator refresh()
{
yield return new WaitForSeconds(refreshdelay);
myScrollRect.horizontalNormalizedPosition = 0.0f;
StopAllCoroutines();
}
}
Variables are: Speed - something around 0.005 Refreshdelay - in seconds, I used 2
Other requirements: Scroll Rect, throw away scrollbars. Put a 'Text' on the content, and make sure it's showing up properly with the viewport set to be the proper viewing area in the scrollview, and the content expanding to the right.
Put a Content Size Fitter on the text, set to preferred size on the horizontal axis.
Hopefully this can help anyone else in this situation as well, and maybe somebody can come up with something more bulletproof.
Answer by shawonRokerz · Jul 04, 2021 at 08:34 AM
Hello Anyone who is struggling with marquee text issue can follow the following code
public string message;
public float scrollSpeed = 10;
public RectTransform panelItem;
public Text textItem;
float panelItemWidth;
void Start()
{
panelItemWidth = panelItem.rect.width;
textItem.text = message;
}
void OnGUI()
{
textItem.transform.position -= new Vector3((Time.deltaTime * scrollSpeed), 0, 0);
if (textItem.gameObject.GetComponent<RectTransform>().transform.localPosition.x <= -(textItem.gameObject.GetComponent<Text>().preferredWidth) )
{
textItem.transform.localPosition = new Vector3(panelItemWidth, 0, 0);
}
}
Here I have used a parent UI Panel which is holding the text component (UI Text) for marquee. I also use a Rect Mask 2D in UI Panel and for the UI Text I set Horizontal Overflow to Overflow as to make it workable. Hope this will help other. Thanks
Yeah, no. This doesn't work. Been at this for about 20 or so hours and can't get it working what so ever. It either doesn't stop scrolling OR it changes the XYZ coords to be thousands of numbers away.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I can't change Text color 3 Answers
Show a text in the position of a GameObject 0 Answers