[UNITY]2DRPG開発日誌 #70 長いテキストをマスク無しでスクロールする

transform.positionをいじってでスクロールしようとするとかなり面倒なので、「1文字目を削除して文字列の最後に移す」という動きを実装することにしました。

書き方はこうです。簡単ですね。

    TextMeshProUGUI tmp;
    bool isScroll = true;
    float space = 5;
    float speed = 0.2f;
    float counter;

    void Start()
    {
        if (!isScroll)
            return;

        GetComponent();

        AddSpace();
    }

    void GetComponent()
    {
        tmp = this.gameObject.GetComponent<TextMeshProUGUI>();
    }

    void AddSpace()
    {
        for (int i = 0; i < space; i++)
            tmp.text += " ";
    }

    void Update()
    {
        if (!isScroll)
            return;

        if (Tool.IsNull(tmp)
            || tmp.text.Length <= 0)
            return;

        counter += Time.deltaTime;

        if (counter < speed)
            return;

        counter = 0;
        tmp.enableWordWrapping = false;

        string deleteLetter = tmp.text.Substring(0, 1);
        string newString  = tmp.text.Substring(1, tmp.text.Length - 1);

        tmp.text = newString + deleteLetter;
    }

 

UNITY
スポンサーリンク
Share
Follow Us
KITTYPOOL