🌍 Unity/유니티 프로그래밍
텍스트 타이핑 효과
맨텀
2018. 6. 8. 13:50
[ A : Update 이용한 타입 ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using TMPro; using UnityEngine; public class SpeechManager : MonoBehaviour { [Header("[ Reference ]")] public TextMeshProUGUI txt; [Header("[ data ]")] public string printString; private int index = 0; private const float WAIT_TIME = 0.05f; // 딜레이 private float waitTimer = 0.0f; private string wholeText = ""; private string typewriterText = ""; private int currentIndex = 0; // 하나의 텍스트 내의 인덱스 private bool IsWriting = false; private void OnEnable() { currentIndex = 0; index = 0; CallRandomText(); } void Update() { if (IsWriting == true) { waitTimer += Time.deltaTime; if (waitTimer > WAIT_TIME && currentIndex < wholeText.Length) { typewriterText += wholeText[currentIndex]; txt.text = typewriterText; waitTimer = 0.0f; currentIndex++; if (currentIndex >= wholeText.Length) IsWriting = false; } } } public void CallRandomText() { txt.text = ""; wholeText = ""; currentIndex = 0; typewriterText = ""; index = Random.Range(0, printString.Length); wholeText = printString; // 랜덤으로 호출. index ++ 방식으로 하면 순서대로 나옴. IsWriting = true; } } |
[ B : 코루틴 이용한 타입 ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | using System.Collections; using TMPro; using UnityEngine; public class TextBubbleManager : MonoBehaviour { [Header("Reference")] public TextMeshProUGUI txt = null; [Header("Setting")] public string printString = "Type Text Here"; public float delay = 0.05f; private string orignText = ""; private int count = 0; void Start() { if (txt != null) { orignText = printString; txt.text = ""; if (orignText.Length > 0) { txt.text += orignText[count++]; StartCoroutine("PrintCoroutine"); } } } IEnumerator PrintCoroutine() { yield return new WaitForSeconds(delay); if (orignText.Length - count > 0) { txt.text += orignText[count++]; StartCoroutine("PrintCoroutine"); } } } |
[ B_2 : 코루틴 이용한 타입 변형 - 켜질때마다 초기화되는 텍스트 ]
- 처음에 꺼져있는 텍스트일 경우 start() 부분을 OnEnable로 고칠것. OnEnable이 Start 보다 먼저 실행됨.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; public class TextWriter : MonoBehaviour { [Header("Reference")] public TextMeshProUGUI txt = null; [Header("Setting")] public string printString = "Type Text Here"; public float delay = 0.05f; private string orignText = ""; private int count = 0; private void Start() { orignText = printString; } private void OnEnable() { count = 0; if (txt != null) { txt.text = ""; if (orignText.Length > 0) { txt.text += orignText[count++]; StartCoroutine("PrintCoroutine"); } } } private void OnDisable() { StopCoroutine("PrintCoroutine"); } IEnumerator PrintCoroutine() { yield return new WaitForSeconds(delay); if (orignText.Length - count > 0) { txt.text += orignText[count++]; StartCoroutine("PrintCoroutine"); } } } |