{"title":"Unity C# 代码优化:实现对话逐字显示并点击控制", "description":"优化 Unity C# 代码,实现对话逐字显示的功能。当鼠标点击时,如果对话尚未完全显示,则立即显示完整文本;再次点击则跳转到下一段对话。", "keywords":"Unity, C#, 代码优化, 对话系统, 逐字显示, 鼠标点击, UI", "content":"using UnityEngine; \nusing UnityEngine.UI; \nusing QFramework; \nusing System.Collections; \nusing System.Collections.Generic; \n\nnamespace QFramework.Escape \n{ \n public class ChatPanelData : UIPanelData \n { \n // 对话列表 \n public List chatList; \n // 对话后触发的事件Code \n public string EventCode; \n } \n\n public partial class ChatPanel : UIPanel \n { \n // 对话完成的回调 \n public delegate void ChatCallback(); \n\n // 对话的当前索引 \n private int CurrentIndex = 0; \n\n // 对话列表 \n private List chatList; \n\n // 协程 \n private Coroutine mCoroutine; \n\n // 是否完全显示对话 \n private bool isShowingComplete = false; \n\n protected override void OnInit(IUIData uiData = null) \n { \n mData = uiData as ChatPanelData ?? new ChatPanelData(); \n chatList = (mData as ChatPanelData).chatList; \n CurrentIndex = 0; \n UpdateChat(); \n nextButton.onClick.AddListener(() => \n { \n Debug.Log("click"); \n if (isShowingComplete) \n { \n UpdateChat(); \n } \n else \n { \n CompleteCurrentText(); \n } \n }); \n } \n\n protected void UpdateChat() \n { \n if (CurrentIndex < chatList.Count) \n { \n dialogdata.text = ""; \n isShowingComplete = false; \n\n if (mCoroutine != null) \n { \n StopCoroutine(mCoroutine); \n mCoroutine = null; \n } \n\n mCoroutine = StartCoroutine(TypeText(chatList[CurrentIndex].Dialog)); \n\n if (chatList[CurrentIndex].Position == ChatPosition.Left) \n { \n ImageLeft.sprite = ChatSystem.Instance.imageDic[chatList[CurrentIndex].CharacterName]; \n LeftName.text = ChatSystem.Instance.GetCharacterName(chatList[CurrentIndex].CharacterName); \n ImageLeft.gameObject.SetActive(true); \n ImageRight.gameObject.SetActive(false); \n LeftName.gameObject.SetActive(true); \n RightName.gameObject.SetActive(false); \n } \n else \n { \n ImageRight.sprite = ChatSystem.Instance.imageDic[chatList[CurrentIndex].CharacterName]; \n RightName.text = ChatSystem.Instance.GetCharacterName(chatList[CurrentIndex].CharacterName); \n ImageLeft.gameObject.SetActive(false); \n ImageRight.gameObject.SetActive(true); \n LeftName.gameObject.SetActive(false); \n RightName.gameObject.SetActive(true); \n } \n CurrentIndex++; \n } \n else \n { \n CloseSelf(); \n } \n } \n\n private IEnumerator TypeText(string text) \n { \n int length = text.Length; \n for (int i = 0; i < length; i++) \n { \n dialogdata.text += text[i]; \n yield return new WaitForSeconds(0.07f); \n } \n \n isShowingComplete = true; \n } \n \n private void CompleteCurrentText() \n { \n StopCoroutine(mCoroutine); \n dialogdata.text = chatList[CurrentIndex - 1].Dialog; \n isShowingComplete = true; \n } \n\n protected override void OnOpen(IUIData uiData = null) \n { \n Player.Instance.Blocking = true; \n } \n\n protected override void OnShow() \n { \n } \n\n protected override void OnHide() \n { \n } \n\n protected override void OnClose() \n { \n Player.Instance.Blocking = false; \n if (mData.EventCode != "" && mData.EventCode != null) \n { \n GameEventHandle.Instance.MakeEventInfluence(mData.EventCode); \n } \n \n mCoroutine = null; \n } \n } \n}


原文地址: http://www.cveoy.top/t/topic/phHa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录