/// <summary> /// 先排序数据,再通过 key 选中某条数据,切到对应页,并高亮显示。 /// </summary> /// <param name="key">Key.</param> public void SelectByKey(string key) { m_data.Sort(); if (m_state == LanguageWindowState.Query) { m_result.Sort(); } bool isContainsKey = false; List <LanguageItem> list = m_state == LanguageWindowState.Query ? m_result : m_data; for (int i = 0; i < list.Count; i++) { LanguageItem item = list[i]; if (item.key == key) { isContainsKey = true; m_curPage = Mathf.FloorToInt((float)i / PAGE_SIZE) + 1; break; } } if (isContainsKey) { m_selectedKey = key; m_selectCount = 10; } else if (key.Trim() != "") { m_oKey = m_oValue = m_value = ""; m_key = key; ChangeState(LanguageWindowState.Append); } }
void OnGUI() { Initialize(); if (m_language == string.Empty) { ShowErrorMessage("无法获取当前使用的语种地区代码!\n请检查:Assets/Lua/Data/Config.lua 文件,是否包含 Config.language 变量!"); return; } if (m_languages.Length == 0) { ShowErrorMessage("目录:Assets/Lua/Data/Languages 中不包含任何语言包文件!"); return; } if (m_languageIdx == -1) { ShowErrorMessage("目录:Assets/Lua/Data/Languages 中不包含 Config.language 指定的语言包文件!"); return; } if (m_state == LanguageWindowState.Edit || m_state == LanguageWindowState.Append) { EditOrAppend(); return; } TextAnchor textFieldAlignment = GUI.skin.textField.alignment; float wndWidth = this.position.width; GUILayout.Space(13); GUILayout.BeginHorizontal(); GUILayout.Space(10); GUILayout.Label("使用语言包:", m_w70); int languageIdx = m_languageIdx; m_languageIdx = EditorGUILayout.Popup(m_languageIdx, m_languages, m_w90); if (m_languageIdx != languageIdx) { ParseLanguage(); if (languageIdx != -1) { ChangeLocalization(); } } GUILayout.Space(70); GUI.skin.textField.alignment = TextAnchor.UpperLeft; m_query = EditorGUILayout.TextField(m_query, m_w120); GUI.skin.textField.alignment = textFieldAlignment; GUILayout.BeginVertical(m_w60); bool isClearQuery = m_state == LanguageWindowState.Query && m_query.Trim() == ""; if (GUILayout.Button(isClearQuery ? "显示全部" : "查询")) { if (isClearQuery) { ChangeState(LanguageWindowState.Normal); } else { m_result.Clear(); m_curPage = 1; string query = m_query.ToLower(); foreach (var item in m_data) { if (item.key_lower.Contains(query) || item.value_lower.Contains(query)) { m_result.Add(item); } } ChangeState(LanguageWindowState.Query); } } GUILayout.EndVertical(); GUILayout.Space(wndWidth - 508); GUILayout.BeginVertical(m_w60); if (GUILayout.Button("+ 新增")) { m_oKey = m_oValue = m_key = m_value = ""; ChangeState(LanguageWindowState.Append); } GUILayout.EndVertical(); // list GUILayout.EndHorizontal(); GUILayout.Space(12); GUILayoutOption keyW = GUILayout.Width((wndWidth - 110) * 0.35f); GUILayoutOption valueW = GUILayout.Width((wndWidth - 110) * 0.65f); Color labelColor = GUI.skin.label.normal.textColor; List <LanguageItem> list = m_state == LanguageWindowState.Query ? m_result : m_data; int startIndex = (m_curPage - 1) * PAGE_SIZE; for (int i = 0; i < PAGE_SIZE; i++) { int index = startIndex + i; LanguageItem item = (index < list.Count) ? list[index] : null; GUIStyle bg; if (item != null && item.key == m_selectedKey && m_selectCount > 0) { m_selectCount--; bg = m_bg3; GUI.skin.label.normal.textColor = m_color2; } else { bg = (i % 2 == 0) ? m_bg1 : m_bg2; GUI.skin.label.normal.textColor = m_color1; } GUILayout.BeginHorizontal(bg, m_h28); GUILayout.Space(10); if (item != null) { GUILayout.BeginVertical(); GUILayout.Space(6); if (GUILayout.Button(item.key, GUI.skin.label, keyW)) { m_selectedKey = item.key; m_selectCount = 10; GUIUtility.systemCopyBuffer = item.key; } GUILayout.EndVertical(); GUILayout.Space(10); GUILayout.BeginVertical(); GUILayout.Space(6); GUILayout.Label(item.value, valueW); GUILayout.EndVertical(); GUILayout.Space(10); GUILayout.BeginVertical(); GUILayout.Space(5); if (GUILayout.Button("编辑", m_w60)) { m_oKey = m_key = item.key; m_oValue = item.value; m_value = item.value.Replace("\\n", "\n"); ChangeState(LanguageWindowState.Edit); } GUILayout.EndVertical(); } GUILayout.Space(10); GUILayout.EndHorizontal(); } GUI.skin.label.normal.textColor = labelColor; // page GUILayout.Space(13); GUILayout.BeginHorizontal(); GUILayout.Space(wndWidth / 2 - 100); EditorGUI.BeginDisabledGroup(m_curPage <= 1); GUILayout.BeginVertical(m_w60); if (GUILayout.Button("上一页")) { m_curPage--; } GUILayout.EndVertical(); EditorGUI.EndDisabledGroup(); GUILayout.Space(3); int pageNum = GetPageNum(); GUI.skin.textField.alignment = TextAnchor.MiddleCenter; m_curPage = EditorGUILayout.IntField(m_curPage, m_w50); m_curPage = Mathf.Min(Mathf.Max(1, m_curPage), pageNum); GUI.skin.textField.alignment = textFieldAlignment; GUILayout.Label("/" + pageNum, m_w30); EditorGUI.BeginDisabledGroup(m_curPage >= pageNum); GUILayout.BeginVertical(m_w60); if (GUILayout.Button("下一页")) { m_curPage++; } GUILayout.EndVertical(); EditorGUI.EndDisabledGroup(); GUILayout.EndHorizontal(); }