# Day 27 | Unity遊戲開發 - 對話介面管理器 ###### tags: `Unity` `AR手遊` > **目錄** > 創建中文字型 > 對話介面配置 > 對話管理器 ## 創建中文字型 ### Step1 找到要的字型 可以從原本電腦的字型中選取,或者是從網路上下載字型資源都可以。將想要的生成的字型拉近專案的資料夾中。 ### Step2 準備好要生成的字與會用到的Font Asset 在資料夾中,對剛剛拉進來的字型點選右鍵,創建TextMeshPro的Font Asset。複製好所有對話內容,包含中文字、符號等。 ### Step3 創建對話專屬的Font Assest 點選 **windows>TextMeshPro>Font Asset Creator** 進入產生器。 選取原本選定的字型,以及剛剛創建的Font Asset,再貼上複製的對話內容,再依照各自的需求對其他選項作調整,調整好後就可以點選Generate,之後再儲存字型就產生囉。  ## 對話介面配置 可依照自己的擺放要顯示的介面,但必須要有TextMeshPro來顯示對話以及Button來控制顯示下一句。  ## 對話管理器 ### Step1變數宣告 * TextMeshProUGUI textDisplay變數用來改變對話內容 * string[] sentences儲存對話句子 * int index對話陣列的索引 * float typingSpeed每個字的出現速度 * GameObject DialogPannel 用來控制對話介面Pannel * GameObject continueButton 控制顯示下一句話 ``` public TextMeshProUGUI textDisplay; public string[] sentences; public int index; public float typingSpeed; public GameObject DialogPannel; public GameObject continueButton; ``` 再宣告好string[]變數後,去Unity的Inspector面板中,填入對話的句子,之後會用到。 ### 對話顯示,打字效果 用foreach將句子中的每個letter分開顯示,WaitForSeconds出現延遲打字效果。 ``` IEnumerator Type() { foreach (char letter in sentences[index].ToCharArray()) { textDisplay.text += letter; yield return new WaitForSeconds(typingSpeed); } } ``` ### 跳到下一句函式 * 先讓continueButton不要顯示,等句子顯示玩再出現。 * 用index判斷對話結束沒,還沒結束就繼續顯示對話。 * 對話結束就清空TextProMesh的text並關閉對話介面 ``` public void NextSentence() { continueButton.SetActive(false); if (index < sentences.Length - 1){ index++; textDisplay.text = ""; typywords.Play(); StartCoroutine(Type()); } else { textDisplay.text = ""; continueButton.SetActive(false); DialogPannel.SetActive(false); } } ``` 記得在continueButton這個控制前往下一句話的Button,設置Onclick要觸發NextSentence()這個函式。 ### 整個對話生成器的主要功能串接 判斷句子的letter是不是都出現了,如果都出現了前往下一句話的Button就可以開啟,並處在選擇狀態。 ``` void Update() { if (textDisplay.text == sentences[index]) { continueButton.SetActive(true); typywords.Stop(); continueButton.GetComponent<Button>().Select(); } } ``` --- 以上對話的部分就完成囉!再依據不同的場景會有不同的對話系統去設定就可以了! 那我們明天見~
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up