using UnityEngine;
using System.Collections;

public class AutoScrollText : MonoBehaviour {

    private RectTransform rectTransform;
    private float parentSizeX;
    
    public float speed = 100.0f;
    
    void Awake(){
        rectTransform = GetComponent<RectTransform>();
        parentSizeX = transform.parent.GetComponent<RectTransform>().rect.width;
    }
    void OnEnable(){
        StartCoroutine(Scroll());
    }
    IEnumerator Scroll(){
        Vector2 p;
        while(true){
            p = rectTransform.anchoredPosition;
            p.x = parentSizeX;
            rectTransform.anchoredPosition = p;
            while(rectTransform.anchoredPosition.x > 0){
                yield return null;
                rectTransform.AddLocalPositionX(-speed * 3 * Time.deltaTime);
            }
            p = rectTransform.anchoredPosition;
            p.x = 0;
            rectTransform.anchoredPosition = p;
            yield return new WaitForSeconds(2.0f);
            
            while(rectTransform.anchoredPosition.x > -rectTransform.rect.width){
                yield return null;
                rectTransform.AddLocalPositionX(-speed * Time.deltaTime);
            }
            yield return new WaitForSeconds(0.5f);
        }
    }
}
