当前位置:   article > 正文

unity制作对话框_unity 聊天框

unity 聊天框

最近在准备计算机设计大赛的游戏方向的比赛,研究一些游戏中的游戏效果的实现
在我们平时玩的角色扮演的游戏中比如GTA等 其中少不了故事情节 不少是用对话框来表达的
这篇博客给大家介绍一下 简单的故事情节的讲述模式
一般的都时按下鼠标左键继续的
我的这个也一样
在这里插入图片描述
然后检测到鼠标左键的按下 进行下一步

而且用到打字机的效果实现 如果不清楚的可以去前面我的博客查看
因为添加了打字的效果 代码稍微麻烦一点
而且刚开始我也走了弯路 尝试在代码中用ui来实现语句的切换

然后我发现利用数组可以很简单的实现 效果也很好

代码块

public float charsPerSecond = 0.2f;//打字时间间隔
    private string[] words = {"欢迎来到我的对话框","接下来给大家看一下","快要结束了哦" };
    //保存需要显示的文字 我这里面是三句
    public int strindex = 0;//控制语句

    private bool isActive = false;
    private float timer;//计时器
    private Text myText;
    public int currentPos = 0;//当前打字位置

    void Start()
    {
        timer = 0;
        isActive = true;
        myText = GetComponent<Text>();
    }
    void Update()
    {
        OnStartWriter();
        if (Input.GetMouseButtonDown(0))
        {
            strindex++;//下一句
            isActive = true;
            if (strindex >= 2)//防止超出数组的长度报错
            {
                strindex = 2;
            }
        }
    }

    public void StartEffect()
    {
        isActive = true;
    }
    /// 执行打字任务
    void OnStartWriter()
    {
        if (isActive)
        {
            timer += Time.deltaTime;
            if (timer >= charsPerSecond)
            {//判断计时器时间是否到达
                timer = 0;
                currentPos++;
                myText.text = words[strindex].Substring(0, currentPos);//刷新文本显示内容

                if (currentPos >= words[strindex].Length)
                {
                    OnFinish();
                }
            }

        }
    }
    /// 结束打字,初始化数据
    void OnFinish()
    {
        isActive = false ;
        timer = 0;
        currentPos = 0;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

如果大家看了我前面的 unity实现打字机的博客 https://blog.csdn.net/weixin_44302602/article/details/103912825

这个会稍微好理解一些 我们可以下载一些音效 效果会更好的

当然也有更简单的效果

 public Text[] texts;
    public  int index = 0;

    private void Update()
    {
        
        for(int i = 0; i < texts.Length; i++)
        {
            texts[i].enabled = false;
        }
        texts[index].enabled = true;
        if (Input.GetMouseButtonDown(0))
        {
            index++;

        }
        index %= texts.Length;//这个防止超出数组的界限
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

这个是需要在unity中赋值的 而且要创建好几个不同的text来切换(不会有打字机的效果)

如果你也是unity爱好者 欢迎关注我 我会持续更新我的学习博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/127609
推荐阅读
相关标签
  

闽ICP备14008679号