From 5d9f14c57386a8e04becb16cfa9ffd12ce087c85 Mon Sep 17 00:00:00 2001 From: Caster Date: Mon, 19 Jul 2021 16:52:51 +0800 Subject: [PATCH] fix: fix `using` missing and refactor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.启动界面修改为GetWindows 只需要一个 2.修改因为代码修改编辑器重载 导致m_encoding 为null的问题 3.增加Ctrl+S 保存数据表功能 避免每次修改都需要关闭界面。 4.添加缺失命名空间 5.修改创建编辑器窗口错误 6.修改数据表每行显示10条数据 可用滑动条拖动 防止列数太多导致无法看清 --- .../DataTableEditor/DataTableEditingWindow.cs | 101 +++++++++++++++--- .../DataTableEditor/LauncherEditorWindow.cs | 16 +-- 2 files changed, 90 insertions(+), 27 deletions(-) diff --git a/Editor/DataTableEditor/DataTableEditingWindow.cs b/Editor/DataTableEditor/DataTableEditingWindow.cs index 03e6778..c97c831 100644 --- a/Editor/DataTableEditor/DataTableEditingWindow.cs +++ b/Editor/DataTableEditor/DataTableEditingWindow.cs @@ -1,14 +1,15 @@ +using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEditor; +using UnityEditorInternal; using UnityEngine; using static DataTableEditor.Utility; namespace DataTableEditor { - public class DataTableEditingWindowInstance { public DataTableEditingWindow Instance { get; private set; } @@ -30,7 +31,6 @@ public void SetData(string path, Encoding encoding) public class DataTableEditingWindow : EditorWindow { - public List RowDatas { get; private set; } private List RowDatasTemp; @@ -48,10 +48,11 @@ public class DataTableEditingWindow : EditorWindow private Vector2 m_scrollViewPos; private Encoding m_encoding; - + private int m_codePage; public void OpenWindow(string path, Encoding encoding) { m_encoding = encoding; + m_codePage = encoding.CodePage; FilePath = path; RowDatas = DataTableUtility.LoadDataTableFile(FilePath, m_encoding); @@ -81,7 +82,6 @@ public void OpenWindow(string path, Encoding encoding) private void OnGUI() { m_scrollViewPos = GUILayout.BeginScrollView(m_scrollViewPos); - if (RowDatas == null || RowDatas.Count == 0) { Close(); @@ -108,7 +108,7 @@ private void OnGUI() true); #else - reorderableList = + reorderableList = new ReorderableList(RowDatas, typeof(List), true, false, true, true); #endif @@ -117,12 +117,20 @@ private void OnGUI() { for (int i = 0; i < RowDatas[index].Data.Count; i++) { - rect.width = - (this.position.width - 20) / - RowDatas[index].Data.Count; + 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); @@ -140,7 +148,7 @@ private void OnGUI() { RowDatas.Add(new DataTableRowData() { - Data = new List() { "", "", "", "" } + Data = new List() {"", "", "", ""} }); } else @@ -160,6 +168,7 @@ private void OnGUI() for (int i = 0; i < RowDatas.Count; i++) RowDatas[i].Data.Add(""); } + Focus(); }; reorderableList.onRemoveCallback = list => @@ -178,6 +187,7 @@ private void OnGUI() RowDatas[i].Data.RemoveAt(RowDatas[i].Data.Count - 1); } } + Focus(); }; reorderableList.drawHeaderCallback = (Rect rect) => @@ -200,7 +210,72 @@ private void OnGUI() 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(); + } + } + + private void SaveDataTable() + { + if (!CheckDirty()) + return; + + RowDatasTemp = new List(); + 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); + } + + private bool IsCombinationKey(EventModifiers preKey, KeyCode postKey, EventType postKeyEvent) + { + if (preKey != EventModifiers.None) + { + bool eventDown = (Event.current.modifiers & preKey) != 0; + if (eventDown && Event.current.rawType == postKeyEvent && Event.current.keyCode == postKey) + { + Event.current.Use(); + return true; + } + } + else + { + if (Event.current.rawType == postKeyEvent && Event.current.keyCode == postKey) + { + Event.current.Use(); + return true; + } + } + + return false; } private void OnDisable() @@ -209,11 +284,11 @@ private void OnDisable() return; bool result = EditorUtility.DisplayDialog("提示", "你已经对表格进行了修改,是否需要保存?", "是", "否"); - if (result) { - DataTableUtility.SaveDataTableFile(FilePath, RowDatas, m_encoding); + SaveDataTable(); } + Focus(); } /// @@ -268,4 +343,4 @@ private bool CheckDirty() return false; } } -} +} \ No newline at end of file diff --git a/Editor/DataTableEditor/LauncherEditorWindow.cs b/Editor/DataTableEditor/LauncherEditorWindow.cs index 6ef1d43..5f5ca51 100644 --- a/Editor/DataTableEditor/LauncherEditorWindow.cs +++ b/Editor/DataTableEditor/LauncherEditorWindow.cs @@ -11,7 +11,6 @@ namespace DataTableEditor public class LauncherEditorWindow : EditorWindow { public static float ButtonHeight = 50; - public static LauncherEditorWindow Instance; private Encoding m_CurrentEncoding; private bool m_IsValidEncode; @@ -23,19 +22,8 @@ public class LauncherEditorWindow : EditorWindow [MenuItem("表格编辑器/打开 &1", priority = 2)] public static void OpenWindow() { - if (Instance != null) - { - Instance.Close(); - return; - } - -#if UNITY_2019_1_OR_NEWER - Instance = LauncherEditorWindow.CreateWindow("数据表编辑器"); -#else - Instance = EditorWindowUtility.CreateWindow("数据表编辑器"); -#endif - - Instance.Show(); + LauncherEditorWindow window = GetWindow(); + window.Show(); } private void OnEnable()