欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity简单实现RPG对话框

程序员文章站 2022-03-22 08:42:38
...

老规矩废话不多说 直接上效果图unity简单实现RPG对话框可以支持多个选项

对话框的类结构

工程基于本人的GameFrame框架 主要逻辑如下

    void ChangeScence()
    {
        Singleton<SceneManager>.GetInstance().LoadScene("Login", () =>
        {
        Singleton<WindowManager>.GetInstance().OpenWindow("Dialogue",true,true,new DialogueContent("dialoguedata.json","node1"));
        });
    }

UI框架的DialogueContent的类结构如下: 支持当前对话 的模型头像的一些支持 比如当前说话的角色头像模型变大之类的

public class DialogueContent : WindowContext
{
    public string FileName ;
    public string StartNode;
    public Sprite LeftIcon;
    public GameObject LeftModel;
    public Sprite RightIcon;
    public GameObject RightModel;
    public int ShowType = 0;// 不显示 1 显示ICON 2 显示 MODEL
    public DialogueContent(string file,string node,Sprite lefticon = null ,Sprite righticon = null,GameObject leftmodle = null,GameObject rightmodel = null,int show = 0)
    {
        FileName = file;
        StartNode = node;
        LeftIcon = lefticon;
        RightIcon = righticon;
        LeftModel = leftmodle;
        RightModel = rightmodel;
        ShowType = show;
    }
}
//初始化按钮的监听事件
 for (int i = 0; i < 4; i++)
        {
            Answers[i] = CacheTransform.Find("Content/AnswerBtn" + (i + 1)).gameObject;
            EventTriggerListener.Get(Answers[i].gameObject).SetEventHandle(EnumTouchEventType.OnClick, (a, b, c) =>
            {
                StopContentText();
                string nextNode = NodeData.Answers[(int) (c[0])].Target;
                if (nextNode == "end")
                {
                    //关闭界面
                    Singleton<WindowManager>.GetInstance().CloseWindow(false,"Dialogue");
                    return;
                }
                NodeData = GetDialogue(nextNode);
                Coroutine = SingletonMono<GameFrameWork>.GetInstance().StartCoroutine(ShowContentText());
            },i);
        }

//更新全局的当前对话的数据
 private DialogueNodeData GetDialogue(string nodename)
    {
        for (int i = 0; i < Data.Nodes.Length; i++)
        {
            if (Data.Nodes[i].Character == nodename)
            {
                return Data.Nodes[i];
            }
        }
        return null;
    }


//显示按钮与对话文字
 private IEnumerator ShowContentText()
    {
        TitleText.text = NodeData.Character;
        //显示按钮
        for (int i = 0; i < 4; i++)
        {
            if (NodeData.Answers.Length > i)
            {
                Answers[i].SetActive(true);
                Answers[i].transform.Find("Text").gameObject.GetComponent<Text>().text = NodeData.Answers[i].Option;
            }
            else
            {
                Answers[i].SetActive(false);
            }
        }
        ShowText.text = "";
        char[] letters = NodeData.Dialogue.ToCharArray();
        for (int i = 0; i < letters.Length; i++)
        {
            ShowText.text += letters[i];
            yield return new WaitForEndOfFrame();
        }
    }

简单实现了一个编辑对话框的Editor扩展

效果如下unity简单实现RPG对话框

有什么疑问 请联系本人或评论

相关标签: unity