Unity对话流程工具开发

问题:

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

期望

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

工具截图

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

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

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

public enum SpeakerType
{
	Teacher,
	Wang,
	Li,
	Zhang,
}

Unity对话流程工具开发_第2张图片

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

/// 
/// 对话流
/// 
[Serializable]
public class DialogFlow:ISerializationCallbackReceiver
{
    /// 
    /// 对话流flag (唯一标签)
    /// 
    public string flag;
    /// 
    /// 所有说话内容
    /// 
    public List speakInfoList =new List();
    
    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;
    }
}

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

/// 
/// 对话流课本(包含所有对话流,可以检索目标流)
/// 
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 list = new List();
        
        private Dictionary dict= new Dictionary();
        
        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(json);
        return group;
    }

    /// 
    /// 获取对话流
    /// 
    /// 唯一标识
    /// 
    public DialogFlow GetDialogFlow(string flag)
    {
        return dialogFlowGroup.GetDialogFlow(flag);
    }

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

    public static void Save(List 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;
    }

}

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

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

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

    [MenuItem("Tools/对话/类型配置")]
    static void OpenWindow()
    {
        SayTypeTools window = GetWindow("类型配置");
        window.OnInit();
        window.Show();
    }
    void OnInit()
    {  
        filePath=Application.dataPath + "/Dialog/SpeakerType.cs";
        speakerTypeList=new List();
        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 dialogFlowList;
    private List bigSwitchList;
    private GUIStyle smallPageStyle;
    private Vector2 scrollPos;

    [MenuItem("Tools/对话/内容编辑")]
    static void OpenWindow()
    {
        DialogTools window = GetWindowWithRect(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();
        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("数据保存成功!");
    }
}

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

你可能感兴趣的:(Unity,c#,unity,c#,json)