当前位置:   article > 正文

Unity对话流程工具开发_unity 流式对话框

unity 流式对话框

问题:

开发引导模块时,总是会碰到人物对话流程,多数情况下,产品把对话流程的文档发给程序,然后程序二次加工成自己可读的文本数据,这样程序和产品在某种意义上来说造成了时间浪费,后期的每次改动,团队的开发时间都会叠加的浪费。

期望

为了简化开发流程,希望开发一个产品用的可视化工具,产品写完数据保存成的数据文件程序可以无缝读取,然后每次迭代对话流程都用这个工具生成新的文件来替换老文件。

工具截图

在这里插入图片描述
首先我们如果开发这个工具首先需要定义一个数据类型来存储我们所需要的信息,
那么先从最小单元开始倒推着定义,我们需要一个语句信息,这个语句需要包含,这句话在整个说话流程中的id、说话者的类型、说话内容、说话时长等,如下:

/// <summary>
/// 说话信息
/// </summary>
[Serializable]
public class SpeakInfo
{
    public int id;// 在所属对话流中的id
    public SpeakerType type;//说话者类型
    public string info;//说话内容
    public float time=3;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

定义说话者枚举,为了方便产品配置,我同时也做了一个工具来生成这个枚举文件,效果附图如下:

public enum SpeakerType
{
	Teacher,
	Wang,
	Li,
	Zhang,
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

然后我们知道,每一段对话就是有多段语句连接成的,然后为了后期方便索引对话流,我们给对话流定义一个唯一flag,那么对话流数据定义如下:

/// <summary>
/// 对话流
/// </summary>
[Serializable]
public class DialogFlow:ISerializationCallbackReceiver
{
    /// <summary>
    /// 对话流flag (唯一标签)
    /// </summary>
    public string flag;
    /// <summary>
    /// 所有说话内容
    /// </summary>
    public List<SpeakInfo> speakInfoList =new List<SpeakInfo>();
    
    public void OnBeforeSerialize()
    {
    }

    public void OnAfterDeserialize()
    {
        if (speakInfoList==null||speakInfoList.Count==0)return;
        for (int i = 0; i < speakInfoList.Count; i++)speakInfoList[i].id = i;
    }
}
  • 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

既然存储的数据都已经定义清楚,那么我们需要一个对象来控制数据的读取和保存,我将其命名为DialogFlowBook:

/// <summary>
/// 对话流课本(包含所有对话流,可以检索目标流)
/// </summary>
public class DialogFlowBook
{
    private static DialogFlowBook _instance;

    public static DialogFlowBook instance
    {
        get
        {
            if (_instance==null)_instance=new DialogFlowBook();
            return _instance;
        }
    }

    public static string filePath = Application.dataPath+"/Dialog/DialogInfo.txt";
    
    [Serializable]
    private class DialogFlowGroup:ISerializationCallbackReceiver
    {
        public List<DialogFlow> list = new List<DialogFlow>();
        
        private Dictionary<string,DialogFlow> dict= new Dictionary<string, DialogFlow>();
        
        public DialogFlow GetDialogFlow(string flag)
        {
            if (dict == null || dict.Count == 0) return null;
            if (dict.ContainsKey(flag)) return dict[flag];
            return null;
        }
        
        private void OnInitDict()
        {
            if (list == null || list.Count == 0) return;
            for (int i = 0; i < list.Count; i++)
            {
                DialogFlow df = list[i];
                if (dict.ContainsKey(df.flag))
                {
                    Debug.LogErrorFormat("【Error: 对话流程中有Flag:【{0}】重复】",df.flag);
                    return;
                }
                dict.Add(df.flag,df);
            }
        }

        public void OnBeforeSerialize()
        {
            
        }

        public void OnAfterDeserialize()
        {
            OnInitDict();
        }
    }

    private static DialogFlowGroup dialogFlowGroup;

    private DialogFlowBook()
    {
        dialogFlowGroup = LoadDialogFlowGroup();
    }

    private DialogFlowGroup LoadDialogFlowGroup()
    {
        DialogFlowGroup group = null;
        string json = ReadLocal(filePath);
        if (string.IsNullOrEmpty(json))
        {
            group=new DialogFlowGroup();
            Debug.Log("本地读取对话流程信息为空");
            return group;
        }
        group=JsonUtility.FromJson<DialogFlowGroup>(json);
        return group;
    }

    /// <summary>
    /// 获取对话流
    /// </summary>
    /// <param name="flag">唯一标识</param>
    /// <returns></returns>
    public DialogFlow GetDialogFlow(string flag)
    {
        return dialogFlowGroup.GetDialogFlow(flag);
    }

#if UNITY_EDITOR
    public static List<DialogFlow> Load()
    {
        DialogFlowGroup group = null;
        string json = ReadLocal(filePath);
        if (string.IsNullOrEmpty(json))
        {
            group=new DialogFlowGroup();
            Debug.Log("本地读取对话流程信息为空");
            return group.list;
        }
        group=JsonUtility.FromJson<DialogFlowGroup>(json);
        return group?.list;
    }

    public static void Save(List<DialogFlow> list)
    {
        if (dialogFlowGroup==null)dialogFlowGroup=new DialogFlowGroup();
        dialogFlowGroup.list = list;
        string json=JsonUtility.ToJson(dialogFlowGroup);
        WriteToLocal(filePath,json);
    }
    #endif

    public static void WriteToLocal(string path,string info)
    {
        if (File.Exists(path))File.Delete(path);
        string dirPath = Path.GetDirectoryName(path);
        if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
        using (FileStream fs= new FileStream(path,FileMode.CreateNew))
        {
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(info);
            sw.Close();
            sw.Dispose();
        }
    }

    public static string ReadLocal(string path)
    {
        if (!File.Exists(path))return null;
        string info = "";
        using (FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read))
        {
            StreamReader sr = new StreamReader(fs);
            info = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
        }
        return info;
    }

}

  • 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
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143

下面开始开发编辑器,源码如下:

using System;
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;

public class SayTypeTools : EditorWindow
{
    private List<string> speakerTypeList;
    private Vector2 scrollPos;
    private string filePath;

    [MenuItem("Tools/对话/类型配置")]
    static void OpenWindow()
    {
        SayTypeTools window = GetWindow<SayTypeTools>("类型配置");
        window.OnInit();
        window.Show();
    }
    void OnInit()
    {  
        filePath=Application.dataPath + "/Dialog/SpeakerType.cs";
        speakerTypeList=new List<string>();
        speakerTypeList.AddRange(Enum.GetNames(typeof(SpeakerType)));
    }
    
    private void OnGUI()
    {
        DrawSayTypes();
    }
    
    void DrawSayTypes()
    {
        if (GUILayout.Button("增加对话类型(用英文字符命名)"))speakerTypeList.Add("");
        GUILayout.Space(10);
        scrollPos=EditorGUILayout.BeginScrollView(scrollPos);
        for (int i = 0; i < speakerTypeList.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            speakerTypeList[i] = EditorGUILayout.TextField(speakerTypeList[i]);
            if (GUILayout.Button("删除",GUILayout.Width(50)))
            {
                speakerTypeList.RemoveAt(i);
                i--;
            }
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndScrollView();
        if (GUILayout.Button("保存"))SaveSayTypeFile();
    }
    
    void SaveSayTypeFile()
    {
        StringBuilder sayTypeStr=new StringBuilder();
        sayTypeStr.AppendLine("public enum SpeakerType");
        sayTypeStr.AppendLine("{");
        for (int i = 0; i < speakerTypeList.Count; i++)
        {
            sayTypeStr.AppendLine("\t"+speakerTypeList[i]+",");
        }
        sayTypeStr.AppendLine("}");
        DialogFlowBook.WriteToLocal(filePath,sayTypeStr.ToString());
        AssetDatabase.Refresh();
    }
}

public class DialogTools : EditorWindow
{
    private List<DialogFlow> dialogFlowList;
    private List<bool> bigSwitchList;
    private GUIStyle smallPageStyle;
    private Vector2 scrollPos;

    [MenuItem("Tools/对话/内容编辑")]
    static void OpenWindow()
    {
        DialogTools window = GetWindowWithRect<DialogTools>(new Rect(0,0,800,900),false,"内容编辑");
        window.OnInit();
        window.Show();
    }

    void OnInit()
    {
        smallPageStyle=new GUIStyle();
        smallPageStyle.normal.textColor = Color.green;
        smallPageStyle.fontSize = 50;
        smallPageStyle.alignment = TextAnchor.MiddleCenter;
        bigSwitchList=new List<bool>();
        dialogFlowList = DialogFlowBook.Load();
        if (dialogFlowList!=null&&dialogFlowList.Count>0)for (int i = 0; i < dialogFlowList.Count; i++)bigSwitchList.Add(false);
    }

    private void OnGUI()
    {
        if(GUILayout.Button("增加一段对话"))
        {
            GUI.FocusControl(null);
            DialogFlow big = new DialogFlow();
            dialogFlowList.Add(big);
            bigSwitchList.Add(true);
        }

        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("展开"))
        {
            GUI.FocusControl(null);
            for (int i = 0; i < bigSwitchList.Count; i++) bigSwitchList[i] = true;
        }
        
        if (GUILayout.Button("收缩"))
        {
            GUI.FocusControl(null);
            for (int i = 0; i < bigSwitchList.Count; i++) bigSwitchList[i] = false;
        }
        EditorGUILayout.EndHorizontal();
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
        DrawMain();
        EditorGUILayout.EndScrollView();
        if (GUILayout.Button("保存"))
        {
            GUI.FocusControl(null);
            Save();
        }
    }

    void DrawMain()
    {
        void DrawBigTitle(int id,out bool isDelete)
        {
            isDelete = false;
            EditorGUILayout.BeginHorizontal();
            
            bigSwitchList[id]=EditorGUILayout.Foldout(bigSwitchList[id],"段"+(id+1)+"、");
            EditorGUILayout.LabelField("标签",GUILayout.Width(30));
            DialogFlow flow = dialogFlowList[id];
            flow.flag=EditorGUILayout.TextField(flow.flag);
            
            if (GUILayout.Button("增加对话"))
            {
                GUI.FocusControl(null);
                flow.speakInfoList.Add(new SpeakInfo());
                bigSwitchList[id] = true;
            }

            if (GUILayout.Button("删除"))
            {
                GUI.FocusControl(null);
                dialogFlowList.RemoveAt(id);
                bigSwitchList.RemoveAt(id);
                isDelete = true;
            }
            EditorGUILayout.EndHorizontal();
        }

        if (dialogFlowList == null || dialogFlowList.Count == 0) return;
        for (int i = dialogFlowList.Count-1; i >=0 ; i--)
        {
            bool isDelete;
            DrawBigTitle(i,out isDelete);
            if (isDelete)continue;
            if (bigSwitchList[i])DrawDialogFlow(dialogFlowList[i]);
            EditorGUILayout.LabelField("---------------------------------------------------------------------------------------------------------------------");
        }
    }

    void DrawDialogFlow(DialogFlow dialogFlow)
    {
        if (dialogFlow == null) return;
        if (dialogFlow.speakInfoList == null || dialogFlow.speakInfoList.Count == 0) return;
        for (int i = dialogFlow.speakInfoList.Count-1; i >=0 ; i--)
        {
            SpeakInfo speakInfo = dialogFlow.speakInfoList[i];
            bool isDelete;
            DrawSayInfo(speakInfo,i,out isDelete);
            if (isDelete)dialogFlow.speakInfoList.RemoveAt(i);
        }
    }

    void DrawSayInfo(SpeakInfo speakInfo,int id,out bool isDelete)
    {
        isDelete = false;
        if (speakInfo == null) return;
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.BeginVertical(GUILayout.Width(20));
        speakInfo.type = (SpeakerType)EditorGUILayout.EnumPopup(speakInfo.type,GUILayout.Width(100));
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("时长",GUILayout.Width(40));
        speakInfo.time = EditorGUILayout.FloatField(speakInfo.time,GUILayout.Width(60));
        EditorGUILayout.EndHorizontal();
        GUILayout.Space(14);
        EditorGUILayout.LabelField((id+1).ToString(),smallPageStyle,GUILayout.Width(100));
        EditorGUILayout.EndVertical();
        speakInfo.info = EditorGUILayout.TextArea(speakInfo.info,GUILayout.Height(EditorGUIUtility.singleLineHeight*5));
        if (GUILayout.Button("删除",GUILayout.Width(35),GUILayout.Height(EditorGUIUtility.singleLineHeight*5)))
        {
            GUI.FocusControl(null);
            isDelete = true;
        }
        EditorGUILayout.EndHorizontal();
    }

    void Save()
    {
        int bigCount = dialogFlowList == null ? 0 : dialogFlowList.Count;
        if (dialogFlowList.Count==0)
        {
            if (!EditorUtility.DisplayDialog("提示:", "\n数据为空,确定要替换本地数据?", "是", "否"))
            {
                AssetDatabase.Refresh();
                return;
            }
        }

        for (int i = 0; i < bigCount; i++)
        {
            if (string.IsNullOrEmpty(dialogFlowList[i].flag))
            {
                if (EditorUtility.DisplayDialog("提示:", "\n有语段标签为空,不可保存", "是"))
                {
                    AssetDatabase.Refresh();
                }
                return;
            }
        }
        DialogFlowBook.Save(dialogFlowList);
        AssetDatabase.Refresh();
        Debug.Log("数据保存成功!");
    }
}

  • 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
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230

ok 工具开发完成。
下面是我为了适配这个工具写了一个简单的对话流程框架,包括,播放对话,停止对话,每段对话的开始结束的回调,以及流程对话结束的回调等。这些常用的功能模块都已经开发完成,有兴趣的童鞋,可以下载源码看一下。
Demo地址

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

闽ICP备14008679号