像gal一樣講故事~
阿新 • • 發佈:2018-05-20
list tostring IE component ret mon ++ code top
我希望在開頭的時候,能有一個講故事的Scene來講前置劇情,就寫了個類。用來控制文本逐字顯示的:
[RequireComponent(typeof(Text))] public class TextController : MonoBehaviour{ public bool m_isUseBtn = true; //是否使用按鈕 public Button m_btn; //按鈕 public float m_time = 0.05f; //字符顯示間隔時間 public string m_str = "this is test string."; //將要顯示的文本 public bool m_isUseAutoPlay = true; //是否使用自動播放 public float[] m_times; //自動播放間隔時間 private string s = ""; //單個字符 private Text m_text; //文本顯示的區域 private int textIndex; //文本標號 private float time = 0; void Start() { textIndex = 0; m_text= this.GetComponent<Text>(); if (m_isUseBtn && m_btn) { m_btn.GetComponent<Button>().onClick.AddListener(() => { OnTheClick(); }); } PlayText(); } private void Update() { time+= Time.deltaTime; if (ES3.KeyExists(textIndex.ToString(), "0_0")) { if (m_times != null && m_times.Length >= textIndex) { if (time >= m_times[textIndex - 1]) { PlayText(); } } else { if (time >= 5) { PlayText(); } } } } private void PlayText() { m_text.text = ""; m_str = ES3.Load<string>(textIndex.ToString(), "0_0"); textIndex++; time = 0; StartCoroutine(ShowText(m_str.Length)); } /// <summary> /// 協程顯示 /// </summary> /// <param name="strLength">欲顯示的長度</param> /// <returns></returns> IEnumerator ShowText(int strLength) { int i = 0; s = ""; while (i < strLength) { yield return new WaitForSeconds(m_time); s += m_str[i].ToString(); m_text.text = s; i += 1; } StopAllCoroutines(); } /// <summary> /// 響應按鈕 /// </summary> void OnTheClick() { StopAllCoroutines(); if (s != m_str) { m_text.text = m_str; } else { if (ES3.KeyExists(textIndex.ToString(), "0_0")) { PlayText(); } } }
原理很簡單,就一個協程而已。我寫了兩個模式,一個是自動翻頁,一個是按鍵翻頁。配上audio,還真有那麽點感覺。
可以看到我用的是EasySave3這個插件來做存儲的,最近對移動端存儲這塊有點疑惑,希望能有大神指導一些。Q543109599 好人一生平安~
像gal一樣講故事~