using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class AutoScrollText : MonoBehaviour
{
public Text nameText;
private Vector3 initialsPos;
bool ison = false;
int spacing = 0;
void Start()
{
//SetTextWidth(nameText.text);
}
private void Update()
{
if (ison)
{
if (initialsPos.x - nameText.GetComponent<RectTransform>().localPosition.x < spacing * 31 + 31)
{
nameText.GetComponent<RectTransform>().localPosition += new Vector3(-1f, 0, 0);
}
else
{
nameText.GetComponent<RectTransform>().localPosition = initialsPos;
}
}
}
public void SetTextWidth(string str)
{
nameText.alignment = TextAnchor.MiddleCenter;
nameText.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
nameText.GetComponent<RectTransform>().localPosition = Vector3.zero;
nameText.text = str;
if (str.Length > 8)
{
nameText.alignment = TextAnchor.MiddleLeft;
nameText.GetComponent<RectTransform>().pivot = new Vector2(0, 0.5f);
nameText.GetComponent<RectTransform>().localPosition += new Vector3(-143, 0, 0);
spacing = nameText.text.Length - 8;
ison = true;
}
else
{
nameText.alignment = TextAnchor.MiddleCenter;
nameText.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
ison = false;
}
initialsPos = nameText.GetComponent<RectTransform>().localPosition;
nameText.GetComponent<RectTransform>().sizeDelta = new Vector2(str.Length * 33, nameText.GetComponent<RectTransform>().sizeDelta.y);
}
}