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(); } }