ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

unity2d开发——对话框实现

2022-03-20 17:33:31  阅读:187  来源: 互联网

标签:DialogData GetInstance unity2d 对话框 void List 开发 allShowTexts public


简介

实现一个类似于pokemon的对话框

功能描述

  1. 对话按照次序依次显示,而不是立刻显示
  2. 点击确认或取消键立刻显示完整对话
  3. 显示完整对话后,再次点击确认或取消键,显示下一行对话
  4. 全部显示后,退出对话

设计思路

首先,因为再pokemon里,进入对话框后是没法做其他操作的,而且全局只有一个,所以这里用单例模式。
为了设计的组件化,博主把对话界面设计成prefab,然后在对话时再实例化

为了按照次序显示对话,用列表存储对话内容

点击查看代码
public class DialogData
{
    public static DialogData instance = null;  //对话框单例

    List<string> allShowTexts = new List<string>();  //需要显示的所有文本
    public delegate void OnCloseDialog();
    public OnCloseDialog onCloseDialog = null;

    public static DialogData GetInstance()
    {
        if (instance == null)
        {
            instance = new DialogData();
        }
        return instance;
    }

    public static List<string> GetTextList()
    {
        return GetInstance().allShowTexts;
    }

    public static void SetTextList(List<string> textList)
    {
        GetInstance().allShowTexts = textList;
    }

    public static void CloseDialog()
    {
        if (GetInstance().onCloseDialog != null)
        {
            GetInstance().onCloseDialog();
        }
        GetInstance().ClearData();
    }

    public void ClearData()
    {
        GetInstance().allShowTexts = new List<string>();
        onCloseDialog = null;
    }
}

然后在创建和关闭的时候,需要截获和放开按键操作(这里的按键操作是博主自己实现的控制,因为比较简单就不放出了)

private void OnDisable()
{
    SystemControl.SetBanMove(false);
    LineShowFinish();  //当脚本在失活的时候,将数据进行重置
    DialogData.CloseDialog();
}

private void OnEnable()
{
    SystemControl.SetBanMove(true);
}

按次序显示用的是定时器,即在update中不断刷新,计算是否需要显示下一个字符。
为了方便判断【是否完整显示改行】,博主单独用一个函数封装了完整显示的操作(虽然这样会导致drawcall增加-_-||)

void OnStartWriter()
{
    if (isActive)
    {
        timer += Time.deltaTime;
        if (timer >= charsPerSecond)//判断计时器时间是否到达
        {
            lineShowFinall = false;
            timer = 0;
            currentPos++;  //这里其实还可以做一个改良,可以检测一个input用户输入,如果输入了,则让currentPos = words.Length,这样可以实现按下按键,马上就显示完毕          
            myText.text = nowShowText.Substring(0, currentPos);  //刷新文本显示内容

            if (currentPos >= nowShowText.Length)
            {
                lineShowFinall = true;
                LineShowFinish();
            }
        }
    }
}

void LineShowFinish()
{
    isActive = false;
    myText.text = nowShowText;
}

最后,就是显示下一行。这里就很简单了,将下一行的数据填充到prefab里,如果没有下一行销毁自身即可

光标实现

在pokemon中,右下角会显示一个弹跳的光标。这里我们用正弦函数控制位置即可

void OnIconJump()
{
    nowPos.y = Mathf.Abs(Mathf.Sin(Time.fixedTime * Mathf.PI * HZ)) * zhenFu + startPos.y;
    nextIcon.transform.position = nowPos;
}

完整代码

Tip.cs(绑定在上面的prefab)

点击查看代码
public class Dialog : MonoBehaviour
{
    float charsPerSecond = 0.05f;  //打字时间间隔
    string nowShowText;  //现在显示的文字
    List<string> allShowTexts = new List<string>();  //需要显示的所有文本
    int nowShowIdx = -1;  //当前显示到行数
    bool showAllFinall = false;  // 所有文本显示完毕

    bool isActive = false; //判断是否开始输出
    float timer;  //计时器
    Text myText;  //获取身上的test脚本
    int currentPos = 0;  //当前打字位置
    bool lineShowFinall = false;  // 是否显示完当前语句
    
    GameObject nextIcon;  // 下一条的箭头
    Vector3 startPos;  //记录原位置
    Vector2 nowPos;  //简写运动变化的位置
    float zhenFu = 0.2f;  //振幅
    float HZ = 1f;  //频率

    private void OnDisable()
    {
        SystemControl.SetBanMove(false);
        LineShowFinish();  //当脚本在失活的时候,将数据进行重置
        DialogData.CloseDialog();
    }

    private void OnEnable()
    {
        SystemControl.SetBanMove(true);
    }

    void Start()
    {
        Debug.Log("start dialogs");
        allShowTexts = DialogData.GetTextList();
        if (allShowTexts.Count <= 0)
        {
            showAllFinall = true;
            GameObject.Destroy(gameObject, 0);
        }

        timer = 0;
        isActive = true;
        charsPerSecond = Mathf.Max(0.02f, charsPerSecond);  //将最小的出字速度限制为0.02,也可以自行调整
        myText = GameObject.Find("Dialog").GetComponent<Text>();
        NextLineShowStart();  //开始显示第一行

        nextIcon = GameObject.Find("Next");
        startPos = nextIcon.transform.position;
        nowPos = startPos;
    }

    void Update()
    {
        //点击确认或者取消键,马上完整显示
        if (SystemControl.Confirm() || SystemControl.Cancel())
        {
            if (!lineShowFinall)
            {
                lineShowFinall = true;
                LineShowFinish();
            }
            else
            {
                NextLineShowStart();
            }
        }
        OnStartWriter();
        OnIconJump();
    }

    void OnStartWriter()
    {
        if (isActive)
        {
            timer += Time.deltaTime;
            if (timer >= charsPerSecond)//判断计时器时间是否到达
            {
                lineShowFinall = false;
                timer = 0;
                currentPos++;  //这里其实还可以做一个改良,可以检测一个input用户输入,如果输入了,则让currentPos = words.Length,这样可以实现按下按键,马上就显示完毕          
                myText.text = nowShowText.Substring(0, currentPos);  //刷新文本显示内容

                if (currentPos >= nowShowText.Length)
                {
                    lineShowFinall = true;
                    LineShowFinish();
                }
            }
        }
    }

    void LineShowFinish()
    {
        isActive = false;
        myText.text = nowShowText;
    }
    
    void NextLineShowStart()
    {
        timer = 0;
        currentPos = 0;
        nowShowIdx++;
        if (nowShowIdx < allShowTexts.Count)
        {
            showAllFinall = false;
            lineShowFinall = false;
            nowShowText = allShowTexts[nowShowIdx];
            isActive = true;
            myText.text = "";
        }
        else
        {
            showAllFinall = true;
            Debug.Log("close dialogs");
            GameObject.Destroy(gameObject, 0);
            return;
        }
        //Debug.Log("nowShowIdx: " + nowShowIdx + ", showAllFinall: " + showAllFinall);
    }

    /// <summary>
    /// 箭头跳动
    /// </summary>
    void OnIconJump()
    {
        nowPos.y = Mathf.Abs(Mathf.Sin(Time.fixedTime * Mathf.PI * HZ)) * zhenFu + startPos.y;
        nextIcon.transform.position = nowPos;
    }
}

public class DialogData
{
    public static DialogData instance = null;  //对话框单例

    List<string> allShowTexts = new List<string>();  //需要显示的所有文本
    public delegate void OnCloseDialog();
    public OnCloseDialog onCloseDialog = null;

    public static DialogData GetInstance()
    {
        if (instance == null)
        {
            instance = new DialogData();
        }
        return instance;
    }

    public static List<string> GetTextList()
    {
        return GetInstance().allShowTexts;
    }

    public static void SetTextList(List<string> textList)
    {
        GetInstance().allShowTexts = textList;
    }

    public static void CloseDialog()
    {
        if (GetInstance().onCloseDialog != null)
        {
            GetInstance().onCloseDialog();
        }
        GetInstance().ClearData();
    }

    public void ClearData()
    {
        GetInstance().allShowTexts = new List<string>();
        onCloseDialog = null;
    }
}

如何调用

void CreateDeveloperTips()
{
    List<string> allShowTexts = new List<string>();
    allShowTexts.Add("您好!欢迎来到aquam的游戏开发世界。");
    allShowTexts.Add("下面介绍该游戏的操作方式。键盘的方向键控制人物的移动。");
    allShowTexts.Add("Z键确认, X键取消。");
    allShowTexts.Add("祝您游戏愉快!");
    DialogData.SetTextList(allShowTexts);
    DialogData.GetInstance().onCloseDialog = CloseDialogCB;
    Instantiate(prefab, dialogPos, Quaternion.Euler(0, 0, 0));
    isOpenTip = true;
}

标签:DialogData,GetInstance,unity2d,对话框,void,List,开发,allShowTexts,public
来源: https://www.cnblogs.com/end-emptiness/p/16030655.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有