分享一下我的对话框打印机特效
上下文
uGUI 已经变成弃用了,我使用的最近 unity 6 的 UI Document。
小镇Town 场景有一个 TownUI 的 UIDocument, TownUI里面有一个类似 Prefab 的Dialog 的UIDocuement, Dialog 里面是一个VisualElement 作为背景, 里面是ScrollView, 然后里面是Label, 你会看到我的代码里也是这么查询的。
TownHandler 变成static 是方便在变得场景中随时调用
showStr 变成public 是为了支持可以在 inspector 那边随时更改
实现
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| using UnityEngine; using UnityEngine.UIElements; using System.Collections;
public class TownHandler : MonoBehaviour {
public static TownHandler instance { get; private set; }
public float displayTime = 4.0f; private VisualElement m_NonPlayerDialogue; private float m_TimerDisplay;
public string showStr; private Label label; private ScrollView scrollView;
private void Awake() { instance = this; }
void Start() {
UIDocument uiDocument = GetComponent<UIDocument>();
m_NonPlayerDialogue = uiDocument.rootVisualElement.Q<VisualElement>("Dialog"); m_NonPlayerDialogue.style.display = DisplayStyle.None; m_TimerDisplay = -1.0f;
scrollView = m_NonPlayerDialogue.Q<ScrollView>(); label = scrollView.Q<Label>();
Debug.LogWarning("label is " + label); }
void Update() {
}
public void DisplayDialogue() { Debug.Log("DisplayDialogue"); m_NonPlayerDialogue.style.display = DisplayStyle.Flex; m_TimerDisplay = displayTime;
StartCoroutine(PrintWord());
}
IEnumerator PrintWord() {
foreach (char letter in showStr) { label.text += letter; yield return new WaitForSeconds(0.1f);
scrollView.scrollOffset = new Vector2(0, label.resolvedStyle.height); }
}
}
|