Unity3D效果实现:文字对话

逻辑:按下鼠标左键,人物表情切换,文字切换

脚本代码:需要一个消息管理类和消息类

消息管理类:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MessageManager : MonoBehaviour
{
    //背景图
    public Image Bgimage;
    //头像图像
    public Image headImage;
    //讲话人
    public Text NameText;
    //内容
    public Text ContentText;
    //储存消息
    public List<Message> messages;
    //消息索引
    private int index = 0;
    void Start()
    {
        messages = new List<Message>();
        Message msg = new Message() { Name = "小黄", Content = "请问探险森林怎么走?", ImageName = "ShayHead" };
        messages.Add(msg);
    }

    Message GetMessage()
    {
        if(index<messages.Count)
        {
            return messages[index++];
        }
        return null;
    }
    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Message();
        }
    }

    void Message()
    {
        //获取对话
        Message msg = GetMessage();
        if(msg!=null)
        {
            if(msg.type==MessageType.Message)
            {
                //从文件夹动态加载
                headImage.sprite = Resources.Load<Sprite>(msg.ImageName);
                NameText.text = msg.Name;
                ContentText.text = msg.Content;
            }
            if(msg.type==MessageType.BackGround)
            {
                Bgimage.sprite = Resources.Load<Sprite>(msg.BackGround);
                Message();
            }
        }
    }
}

  消息调用类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//枚举类型,判断是更改消息还是背景
public enum MessageType
{
    Message,
    BackGround
}
public class Message
{
    //枚举类型,默认为对话类型
   public MessageType type = MessageType.Message;
    public string Name;
    public string Content;
    public string ImageName;
    public string BackGround;
}

 

posted @ 2021-11-29 21:17  不进育碧不改名  阅读(250)  评论(0编辑  收藏  举报