IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity对话流程工具开发 -> 正文阅读

[游戏开发]Unity对话流程工具开发

问题:

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

期望

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

工具截图

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

/// <summary>
/// 说话信息
/// </summary>
[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,
}

在这里插入图片描述

然后我们知道,每一段对话就是有多段语句连接成的,然后为了后期方便索引对话流,我们给对话流定义一个唯一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;
    }
}

既然存储的数据都已经定义清楚,那么我们需要一个对象来控制数据的读取和保存,我将其命名为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;
    }

}

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

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("数据保存成功!");
    }
}

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

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-03-10 22:58:11  更:2022-03-10 22:58:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 14:41:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码