unity实现打字机显示文本效果

研究了三天,累死我了,虽然自己比较笨,但终于实现了,效果:第一次调用方法按打字机显示文本,
第二次调用方法显示这句话全部文本,以此类推,因为我平时玩游戏文本都是第二次点击显示整句话,
需要string[]文本,请自己写方法转换成string[],格式是第一行人物,第二行剧情,以此类推
剧情文本格式在这里插入图片描述
效果
不知道怎么插入视频!!!以后在研究
在这里插入图片描述
在这里插入图片描述

    //方法3,携程,打字机效果,不是静态不可直接调用,请复制代码
    //string[]文本+人物名字+剧情文本+打字机速度
    public static int nameline = 1;//当前名字行数,需要可以调用访问
    public static int sayline = 2;//当前剧情行数
    private bool sw1 = true;//不用管,方法逻辑开关
    private bool sw2 = true;
    private void RorderPlot2(string[] plot, Text name, Text say, float time)
    {
        if (!sw1)
        {
            sw1 = true;
            sw2 = false;
            return;
        }
        if (sayline < plot.Length)
        {
            name.text = "";
            say.text = "";
            name.text = plot[nameline - 1];//名字
            nameline = nameline + 2;
            string plot1 = plot[sayline - 1];//剧情
            IEnumerator ienu = Ienu();
            StartCoroutine(ienu);
            IEnumerator Ienu()
            {
                int a = -1;
                while (a < plot1.Length - 1)
                {
                    a = a + 1;
                    sw1 = false;
                    say.text = say.text + plot1[a];
                    yield return new WaitForSeconds(time);
                    if (a == plot1.Length - 1)
                    {
                        sw1 = true;
                        sayline = sayline + 2;
                        break;
                    }
                    if (!sw2)
                    {
                        say.text = plot[sayline - 1];//剧情
                        sayline = sayline + 2;
                        sw1 = true;
                        sw2 = true;
                        break;
                    }
                }
            }
        }
    }

具体调用如下图所示,写好方法,在代入参数
在这里插入图片描述


版权声明:本文为xyyskl99原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。