Unity 游戏对话系统:实现逐字显示并点击跳转下一段

在 Unity 游戏中实现对话系统时,除了基本的功能之外,还可以添加一些细节来提升玩家体验。例如,可以让对话以逐字显示的方式呈现,并通过点击按钮来跳转到下一段对话。本文将介绍如何实现这个功能,并提供一份完整的示例代码。

实现思路

  1. 添加一个标识变量: 使用一个布尔类型的变量来标识对话是否已经完全显示。
  2. 修改协程执行: 将 TypeText() 方法中的协程定义移动到方法外部,方便在点击按钮时判断是否需要执行。
  3. 按钮点击判断: 在 nextButton 的点击事件中添加判断,如果对话已经完全显示,则调用 UpdateChat() 方法跳转到下一段对话;否则,将 dialogdata.text 直接设置为当前对话内容,并设置标识变量为 true。
  4. 完成协程执行: 在 TypeText() 方法中,在完成循环后设置标识变量为 true,表示对话已经完全显示。

示例代码

using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections;
using System.Collections.Generic;

namespace QFramework.Escape
{
    public class ChatPanelData : UIPanelData
    {
        // 对话列表
        public List<ChatItem> chatList;
        // 对话后触发的事件Code
        public string EventCode;
    }

    public partial class ChatPanel : UIPanel
    {
        // 对话完成的回调
        public delegate void ChatCallback();

        // 对话的当前索引
        private int CurrentIndex = 0;

        // 对话列表
        private List<ChatItem> chatList;

        // 协程
        private Coroutine mCoroutine;

        // 对话是否完全显示
        private bool isTextComplete = false;

        protected override void OnInit(IUIData uiData = null)
        {
            mData = uiData as ChatPanelData ?? new ChatPanelData();
            chatList = (mData as ChatPanelData).chatList;
            CurrentIndex = 0;
            UpdateChat();
            nextButton.onClick.AddListener(() =>
            {
                Debug.Log('click');
                if (isTextComplete)
                {
                    UpdateChat();
                }
                else
                {
                    dialogdata.text = chatList[CurrentIndex].Dialog;
                    isTextComplete = true;
                }
            });
        }

        protected void UpdateChat()
        {
            if (CurrentIndex < chatList.Count)
            {
                dialogdata.text = '';
                isTextComplete = false;

                if (mCoroutine != null)
                {
                    StopCoroutine(mCoroutine);
                }

                mCoroutine = StartCoroutine(TypeText(chatList[CurrentIndex].Dialog));

                if (chatList[CurrentIndex].Position == ChatPosition.Left)
                {
                    ImageLeft.sprite = ChatSystem.Instance.imageDic[chatList[CurrentIndex].CharacterName];
                    LeftName.text = ChatSystem.Instance.GetCharacterName(chatList[CurrentIndex].CharacterName);
                    ImageLeft.gameObject.SetActive(true);
                    ImageRight.gameObject.SetActive(false);
                    LeftName.gameObject.SetActive(true);
                    RightName.gameObject.SetActive(false);
                }
                else
                {
                    ImageRight.sprite = ChatSystem.Instance.imageDic[chatList[CurrentIndex].CharacterName];
                    RightName.text = ChatSystem.Instance.GetCharacterName(chatList[CurrentIndex].CharacterName);
                    ImageLeft.gameObject.SetActive(false);
                    ImageRight.gameObject.SetActive(true);
                    LeftName.gameObject.SetActive(false);
                    RightName.gameObject.SetActive(true);
                }
                CurrentIndex++;
            }
            else
            {
                CloseSelf();
            }
        }

        private IEnumerator TypeText(string text)
        {
            int length = text.Length;
            for (int i = 0; i < length; i++)
            {
                dialogdata.text += text[i];
                yield return new WaitForSeconds(0.07f);
            }
            isTextComplete = true;
        }

        protected override void OnOpen(IUIData uiData = null)
        {
            Player.Instance.Blocking = true;
        }

        protected override void OnShow()
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnClose()
        {
            Player.Instance.Blocking = false;
            if (mData.EventCode != '' && mData.EventCode != null)
            {
                GameEventHandle.Instance.MakeEventInfluence(mData.EventCode);
            }
        }
    }
}

总结

通过添加一个标识变量和修改按钮点击事件的判断逻辑,我们成功实现了逐字显示对话并点击跳转下一段对话的功能。这将使你的游戏对话系统更加生动有趣,提升玩家的体验。


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

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