Exemple #1
0
        /// <summary>
        /// 加载数据表文件
        /// </summary>
        /// <param name="path">表格文件路径</param>
        /// <returns>保存的信息数据</returns>
        public static List <DataTableRowData> LoadDataTableFile(string path)
        {
            if (File.Exists(path) == false)
            {
                EditorUtility.DisplayDialog("提示", "文件路径不存在", "确定");
                return(null);
            }

            List <DataTableRowData> data = new List <DataTableRowData>();

            using (StreamReader sr = new StreamReader(path, new UnicodeEncoding()))
            {
                while (sr.EndOfStream == false)
                {
                    UTF8Encoding     utf8    = new UTF8Encoding();
                    string           line    = sr.ReadLine();
                    string[]         splited = line.Split('\t');
                    DataTableRowData row     = new DataTableRowData();

                    for (int i = 0; i < splited.Length; i++)
                    {
                        row.Data.Add(splited[i]);
                    }

                    data.Add(row);
                }
            }

            return(data);
        }
        public void OpenWindow(string path, Encoding encoding)
        {
            m_encoding = encoding;
            m_codePage = encoding.CodePage;
            FilePath   = path;
            RowDatas   = DataTableUtility.LoadDataTableFile(FilePath, m_encoding);

            if (RowDatas == null)
            {
                return;
            }

            RowDatasTemp = new List <DataTableRowData>();

            for (int i = 0; i < RowDatas.Count; i++)
            {
                DataTableRowData data = new DataTableRowData();

                for (int j = 0; j < RowDatas[i].Data.Count; j++)
                {
                    data.Data.Add(RowDatas[i].Data[j]);
                }

                RowDatasTemp.Add(data);
            }

            if (RowDatas == null)
            {
                return;
            }

            LightMode = EditorPrefs.GetInt("DataTableEditor_" + Application.productName + "_LightMode", 0);
        }
        private void SaveDataTable()
        {
            if (!CheckDirty())
            {
                return;
            }

            RowDatasTemp = new List <DataTableRowData>();
            for (int i = 0; i < RowDatas.Count; i++)
            {
                DataTableRowData data = new DataTableRowData();

                for (int j = 0; j < RowDatas[i].Data.Count; j++)
                {
                    data.Data.Add(RowDatas[i].Data[j]);
                }

                RowDatasTemp.Add(data);
            }

            if (m_encoding == null)
            {
                m_encoding = Encoding.GetEncoding(m_codePage);
            }

            DataTableUtility.SaveDataTableFile(FilePath, RowDatas, m_encoding);
        }
Exemple #4
0
        public static void OpenWindow(string path)
        {
            FilePath = path;
            RowDatas = DataTableUtility.LoadDataTableFile(FilePath);

            if (RowDatas == null)
            {
                return;
            }

            RowDatasTemp = new List <DataTableRowData>();

            for (int i = 0; i < RowDatas.Count; i++)
            {
                DataTableRowData data = new DataTableRowData();

                for (int j = 0; j < RowDatas[i].Data.Count; j++)
                {
                    data.Data.Add(RowDatas[i].Data[j]);
                }

                RowDatasTemp.Add(data);
            }

            if (RowDatas == null)
            {
                return;
            }

            FileInfo fileInfo = new FileInfo(path);

            Instance = DataTableEditingWindow.CreateWindow <DataTableEditingWindow>(fileInfo.Name);
            Instance.Show();

            LightMode = EditorPrefs.GetInt("DataTableEditor_" + Application.productName + "_LightMode", 0);
        }
Exemple #5
0
        private void OnGUI()
        {
            m_scrollViewPos = GUILayout.BeginScrollView(m_scrollViewPos);

            if (RowDatas == null || RowDatas.Count == 0)
            {
                Close();
                return;
            }

            CheckColumnCount();

            if (LightMode == 0)
            {
                Theme = "ScriptText";
            }
            else if (LightMode == 1)
            {
                Theme = "PreferencesSectionBox";
            }

            if (reorderableList == null)
            {
                reorderableList =
                    new ReorderableList(RowDatas, typeof(List <DataTableRowData>), true, false, true, true);

                reorderableList.drawElementCallback = (Rect rect, int index, bool selected, bool focused) =>
                {
                    for (int i = 0; i < RowDatas[index].Data.Count; i++)
                    {
                        rect.width =
                            (Instance.position.width - 20) /
                            RowDatas[index].Data.Count;

                        rect.x = rect.width * i + 20;

                        RowDatas[index].Data[i] =
                            EditorGUI.TextField(rect, "", RowDatas[index].Data[i],
                                                Instance.Theme);
                    }
                };

                reorderableList.onAddCallback = (ReorderableList list) =>
                {
                    bool result =
                        EditorUtility.DisplayDialog("提示", "添加 行 或 列", "行", "列");

                    if (result)
                    {
                        if (RowDatas.Count == 0)
                        {
                            RowDatas.Add(new DataTableRowData()
                            {
                                Data = new List <string>()
                                {
                                    "", "", "", ""
                                }
                            });
                        }
                        else
                        {
                            DataTableRowData data = new DataTableRowData();

                            for (int i = 0; i < RowDatas[0].Data.Count - 1; i++)
                            {
                                data.Data.Add("");
                            }

                            RowDatas.Add(data);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < RowDatas.Count; i++)
                        {
                            RowDatas[i].Data.Add("");
                        }
                    }
                };

                reorderableList.onRemoveCallback = (ReorderableList list) =>
                {
                    bool result =
                        EditorUtility.DisplayDialog("提示", "移除 行 或 列", "行", "列");

                    if (result)
                    {
                        RowDatas.RemoveAt(list.index);
                    }
                    else
                    {
                        for (int i = 0; i < RowDatas.Count; i++)
                        {
                            RowDatas[i].Data.RemoveAt(RowDatas[i].Data.Count - 1);
                        }
                    }
                };

                reorderableList.drawHeaderCallback = (Rect rect) =>
                {
                    EditorGUI.LabelField(rect, FilePath);
                    rect.x = rect.width - 70;
                    EditorGUI.LabelField(rect, "高亮模式");
                    rect.x = rect.width - 20;

                    LightMode =
                        EditorGUI.Toggle(rect, LightMode == 0 ? true : false)
                            ? 0
                            : 1;

                    EditorPrefs
                    .SetInt("DataTableEditor_" + Application.productName + "_LightMode",
                            LightMode);
                };
            }

            reorderableList.DoLayoutList();

            GUILayout.EndScrollView();
        }
        private void OnGUI()
        {
            m_scrollViewPos = GUILayout.BeginScrollView(m_scrollViewPos);
            if (RowDatas == null || RowDatas.Count == 0)
            {
                Close();
                GUILayout.EndScrollView();
                return;
            }

            CheckColumnCount();

            if (LightMode == 0)
            {
                Theme = "ScriptText";
            }
            else if (LightMode == 1)
            {
                Theme = "PreferencesSectionBox";
            }

            if (reorderableList == null)
            {
#if UNITY_2019_1_OR_NEWER
                reorderableList =
                    new UnityInternalBridge.ReorderableList(RowDatas, typeof(List <DataTableRowData>), true, false, true,
                                                            true);
#else
                reorderableList =
                    new ReorderableList(RowDatas, typeof(List <DataTableRowData>), true, false, true, true);
#endif

                reorderableList.drawElementCallback = (Rect rect, int index, bool selected, bool focused) =>
                {
                    for (int i = 0; i < RowDatas[index].Data.Count; i++)
                    {
                        if (RowDatas[index].Data.Count > 10)
                        {
                            rect.width =
                                (this.position.width - 20) /
                                10;
                        }
                        else
                        {
                            rect.width =
                                (this.position.width - 20) /
                                RowDatas[index].Data.Count;
                        }

                        rect.x = rect.width * i + 20;
                        RowDatas[index].Data[i] =
                            EditorGUI.TextField(rect, "", RowDatas[index].Data[i],
                                                this.Theme);
                    }
                };

                reorderableList.onAddCallback = list =>
                {
                    bool result =
                        EditorUtility.DisplayDialog("提示", "添加 行 或 列", "行", "列");

                    if (result)
                    {
                        if (RowDatas.Count == 0)
                        {
                            RowDatas.Add(new DataTableRowData()
                            {
                                Data = new List <string>()
                                {
                                    "", "", "", ""
                                }
                            });
                        }
                        else
                        {
                            DataTableRowData data = new DataTableRowData();

                            for (int i = 0; i < RowDatas[0].Data.Count - 1; i++)
                            {
                                data.Data.Add("");
                            }

                            RowDatas.Add(data);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < RowDatas.Count; i++)
                        {
                            RowDatas[i].Data.Add("");
                        }
                    }
                    Focus();
                };

                reorderableList.onRemoveCallback = list =>
                {
                    bool result =
                        EditorUtility.DisplayDialog("提示", "移除 行 或 列", "行", "列");

                    if (result)
                    {
                        RowDatas.RemoveAt(list.index);
                    }
                    else
                    {
                        for (int i = 0; i < RowDatas.Count; i++)
                        {
                            RowDatas[i].Data.RemoveAt(RowDatas[i].Data.Count - 1);
                        }
                    }
                    Focus();
                };

                reorderableList.drawHeaderCallback = (Rect rect) =>
                {
                    EditorGUI.LabelField(rect, FilePath);
                    rect.x = rect.width - 70;
                    EditorGUI.LabelField(rect, "高亮模式");
                    rect.x = rect.width - 20;

                    LightMode =
                        EditorGUI.Toggle(rect, LightMode == 0 ? true : false)
                            ? 0
                            : 1;

                    EditorPrefs
                    .SetInt("DataTableEditor_" + Application.productName + "_LightMode",
                            LightMode);
                };
            }

            reorderableList.DoLayoutList();

            if (RowDatas != null && RowDatas.Count > 0)
            {
                if (RowDatas[0].Data.Count > 10)
                {
                    float listItemWidth = 0f;
                    float listX         = 0f;
                    listItemWidth = (position.width - 20) / 10;
                    listX         = listItemWidth * (RowDatas[0].Data.Count - 1) + 20;
                    GUILayout.Label("", new GUIStyle()
                    {
                        fixedWidth = listX
                    });
                }
            }

            GUILayout.EndScrollView();
            if (IsCombinationKey(EventModifiers.Control, KeyCode.S, EventType.KeyDown))
            {
                SaveDataTable();
            }
        }