# 對話框 #Method 1 (有點撿現成的意味~ (可是,因為是已經弄好的,是否相容於我們的遊戲,這很難確定~) 下載3DGameKit(免費)。 在Project的Assets的3DGameKit,打開Prefabs資料夾,再打開Interactables。 在Interactables,放置Crystal(作為觸發情況的地標)。 在Interactables,將InfoZone拖曳到Hierarchy的GameUtilities底下。 使用Scale Tool把InfoZone縮小,並把InfoZone設置在Cryatal的位置。 回到Prefabs資料夾,打開UIPrefabs資料夾,確認DialogueCanvas.prefab。 將DialogueCanvas.prefab拖曳到Hierarchy的UI底下。 回到InfoZone(點選),查看Inspector,找到Interact On Trigger(Script)。 在Interact On Trigger(Script),Layer選為Player。 (確認On Enter()和On Exit(),如果沒有選項,點選下方加號。) 從Hierarchy的UI底下,將DialogueCanvas拖曳為OnEnter()的Object。 從Hierarchy的UI底下,將DialogueCanvas拖曳為OnExit()的Object。 OnEnter()的Function選為DialogueCanvasController,並選擇ActivateCanvasWithText(string)。 然後,可以直接在下方的空格輸入內容。 OnExit()的Function選為DialogueCanvasController,並選擇DeactivateCanvasWithDelay(float)。 然後,可以直接在下方的空格輸入延緩(離開InfoZone後)消失的秒數。 (當然,只要在InfoZone內,對話不可能停止) #Method 2 (這個比較正規~可是,我看到的應用大多數是2D遊戲) (所以,如果要弄成3D,可能得下載3DGameKit參考~) (程式部分的縮排,編輯這邊的比較容易懂~) 右鍵在Hierarchy區塊,選擇UI的Panel,(然後可以修改名稱DialogueBox), 點一下Hierarchy的Canvas, 將Canvas Scaler中的UI Scale Mode轉為Scale With Screen Size, 修改Canvas Scaler中的Reference Resolution為 (X:3820, Y:2160)(4K)或(X:1920, Y:1080)(HD)。 點選DialogueBox,調整Rect Transform中的數據為合宜大小。 右鍵DialogueBox,點選UI的Text-TextMeshPro, 如果有視窗跳出來,點選Import TMP Essentials, 如果成功輸入,會在DialogueBox底下出現Text(TMP), 點選Text(TMP),在Main Settings中的Vertex Color選擇喜歡顏色, 在Main Settings中的Font Size選擇想要的字型大小。 然後,在Rect Transform中調整大小(不得超過DialogueBox)。 右鍵Project中的Assets的Create的C#Script, (修改新增C#Script的名字為Dialogue)。 (C#) using TMPro; public class Dialogue:MonoBehaviour { public TextMeshProUGUI textComponent; public string[] lines; public float textSpeed; private int index; void Start() { textComponent.text = string.Empty; StartDialogue(); } void StartDialogue() { index = 0; StartCoroutine(TypeLine()); } IEnumerator TypeLine() { foreach(char c in lines[index].ToCharArray()) { textComponent.text += c; yield return new WaitForSeconds(textSpeed); } } } (unity) 點選Hierarchy的DialogueBox, 在Dialogue(Script)中,可以直接輸入Text Speed(如,0.3)。 在Dialogue(Script)中,直接輸入Lines的Size。 (C#) 創建新Function void NextLine() { if (index < lines.Length-1) { index++; textComponent.text = string.Empty; StartCoroutine(TypeLine()); } else { gameObject.SetActive(false); } } 修改Update Function void Update() { if(Input.GetMouseButtonDown(0)) { if(textComponent.text == lines[index]) { NextLine(); } else { StopAllCoroutines(); textComponent.text = lines[index]; } } }
×
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