当前位置:网站首页>Unity typewriter to automatically roll text to the bottom of the text box

Unity typewriter to automatically roll text to the bottom of the text box

2022-06-10 13:05:00 Lin fengyiyi

The effect is shown in the figure :

Steps are as follows :

One 、 stay Canvas——Image Next , Create a ScrollView, Be careful , Here I want not to let white Image Block my background , Right here. ScrollView Upper Image The color of has been modified , Make it transparent Alpha Change it to 1( Be careful not to change to 0, Otherwise you won't see the text )

Two 、 Yes ScrollView Make the following changes :

3、 ... and 、 Yes Content Make the following changes :

Four 、 Yes Text Make the following changes :

  5、 ... and 、 The typewriter script is as follows :

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

namespace MyController
{
    public class DaZiJiController : MonoBehaviour
    {
        public Text m_tweenText;
        [Range(1, 30)]
        public float m_speed = 1.0f;
        public AudioClip m_audio;
        private bool m_canTween = false;
        private string m_totalStr;
        private float m_timeChange = 0.0f;
        public Scrollbar verticalScrollbar;

        void Start()
        {
            BeginTextShow();
        }

        void Update()
        {
            // If you can start animation 
            if (m_canTween)
            {
                // Determine whether the text should end the animation 
                if (m_tweenText.text.Equals(m_totalStr))
                {
                    // end 
                    TextTweenFinsh();
                }
                else
                {
                    // not finished , Text animation 
                    m_tweenText.text = m_totalStr.Substring(0, (int)(m_speed * m_timeChange));
                    m_timeChange += Time.deltaTime;
                    StartCoroutine(ScrollBarBottom());
                }
            }
        }

        void LateUpdate()
        {
            if(Input.GetKeyDown(KeyCode.A))
            {
                //TextTweenFinsh();
                m_tweenText.text = " Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n" +
                    " Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n" +
                    " Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n" +
                    " Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n Thousands of birds fly away , The trail of man is gone , In a lonely boat, there are many rainbows , Fishing for snow in the cold river alone .\n";
                BeginTextShow();
            }
        }

        void BeginTextShow()
        {
            // Save the text beforehand 
            m_totalStr = m_tweenText.text;
            // Empty Unity Text Text content 
            m_tweenText.text = "";
            // Change time to zero 
            m_timeChange = 0f;
            // Add audio components , And set the audio to be played 
            m_tweenText.gameObject.GetComponent<AudioSource>().clip = m_audio;
            // Set the audio loop 
            m_tweenText.GetComponent<AudioSource>().loop = true;
            m_tweenText.GetComponent<AudioSource>().volume = 0.4f;
            // Play the audio 
            m_tweenText.GetComponent<AudioSource>().Play();
            // You can start animation 
            m_canTween = true;
        }
        void TextTweenFinsh()
        {
            m_canTween = false;
            m_tweenText.text = m_totalStr;
            m_timeChange = 0.0f;
            m_tweenText.GetComponent<AudioSource>().Stop();
            //DestroyObject(m_tweenText.GetComponent<AudioSource>());
            Debug.Log(" Trigger “ Typewriter end !” event ");
        }

        IEnumerator ScrollBarBottom()
        {
            yield return null;
            verticalScrollbar.value = 0;
        }
    }
}

Demo Download link :

link :https://pan.baidu.com/s/15MijVu99gNqLvfCRIBSevQ
Extraction code :sk7m

原网站

版权声明
本文为[Lin fengyiyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101133395993.html

随机推荐