在对之前写的对话系统的使用过程中,发现了一些问题,这是一篇代码优化的文章,还没看过的小伙伴可以看一下Unity简单对话系统的实现
1、场景上
在场景中,我们把左右角色对话的左右分别单独创建
2、代码上
我们把对话双方的头像或者立绘和名字存储在对话的数据文件中
using System;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu( menuName = "对话/DialogueData")]
public class DialogueData : ScriptableObject
{
public int ID;
public List<Dialogue> dialogueList;
public string speakerNameLeft;
public string speakerNameRight;
public Sprite speakerLeft;
public Sprite speakerRight;
}
[Serializable]
public class Dialogue
{
public Row row;
[TextArea(5,4)]
public string speech;
public List<Choose> chooseList = new List<Choose>();//对话是否有选项
}
[Serializable]
public class Choose
{
public int ID;
public string chooseBtnText;
}
3、DialoguePanel中
获取刚创建的组件
4、DialogueSystem中
同步信息方法改为同步到对话数据中的内容
在根据角色位置切换显示的方法中改为根据角色位置切换对应图片的显隐
最后在相应位置赋值即可