using LitJson; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using UnityEditor; using UnityEngine; using UnityEngine.Networking; namespace GameEditor.FrameTool { public class ConfigWindowNew : EditorWindow { string tempConfigPath; string configPath; string previous; Dictionary dic; Dictionary curDic; bool isFinish; string addKey; string targetPath; // Add menu named "My Window" to the Window menu [MenuItem("Build/Config")] static void Init() { // Get existing open window or if none, make a new one: ConfigWindowNew window = (ConfigWindowNew)EditorWindow.GetWindow(typeof(ConfigWindowNew)); window.InitWindow(); window.Show(); } void OnGUI() { EditorGUILayout.BeginVertical(); if (GUILayout.Button("加载config模板", GUILayout.ExpandWidth(false))) { dic.Clear(); tempConfigPath = EditorUtility.OpenFilePanel("Resource path", tempConfigPath,"*.*"); EditorPrefs.SetString("IMPORTCCONFIG_SOURCESFOLDERPATH", tempConfigPath); StreamReader sr = File.OpenText(tempConfigPath); SetConfigData(sr.ReadToEnd()); sr.Close(); } if (GUILayout.Button("下载config模板", GUILayout.ExpandWidth(false))) { isFinish = false; dic.Clear(); string path = "http://60.1.1.12/assetbundles/config.txt"; EditorCoroutineRunner.StartEditorCoroutine(DownLoadTempComfig(path)); } if (dic != null && dic.Count > 0) { RefreshWindowShow(); } if (isFinish) { if (curDic != null && curDic.Count > 0) { RefreshWindowShow1(); } } EditorGUILayout.BeginHorizontal(); targetPath = GUILayout.TextArea(targetPath); if (GUILayout.Button("Brown", GUILayout.ExpandWidth(false))) { targetPath = EditorUtility.OpenFilePanel("Resource path", targetPath, "*.*"); EditorPrefs.SetString("IMPORTCCONFIG_TARGETFOLDERPATH", targetPath); } if (GUILayout.Button("保存数据", GUILayout.ExpandWidth(false))) { JsonData jsonData = new JsonData(); SetDicToJsonData(jsonData,dic); string json = JsonMapper.ToJson(jsonData); Debug.Log("jsonData:" + json); File.WriteAllText(targetPath, json, Encoding.UTF8); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } IEnumerator DownLoadTempComfig(string path) { UnityWebRequest request0 = UnityWebRequest.Get(path); request0.downloadHandler = new DownloadHandlerBuffer(); yield return request0.SendWebRequest(); while (!request0.isDone) { yield return 1; } if (request0.isNetworkError || request0.isHttpError) { Debug.Log(request0.error); } else { SetConfigData(request0.downloadHandler.text); } } void InitWindow() { isFinish = true; addKey = ""; dic = new Dictionary(); tempConfigPath = EditorPrefs.GetString("IMPORTCCONFIG_SOURCESFOLDERPATH"); targetPath = EditorPrefs.GetString("IMPORTCCONFIG_TARGETFOLDERPATH"); configPath = EditorPrefs.GetString("IMPORTCCONFIG_DATAFOLDERPATH"); } void SetJsonDataToDic(IDictionary data,Dictionary dicc) { foreach (var item in data.Keys) { string key = item.ToString(); //Debug.Log(string.Format("{0}", item)); try { IDictionary localData = data[item] as IDictionary; if (!dicc.ContainsKey(key)) { dicc.Add(key, new Dictionary()); } SetJsonDataToDic(localData, dicc[key] as Dictionary); } catch { if (dicc.ContainsKey(key)) { dicc.Remove(key); } dicc.Add(key, data[item]); } } } void SetDicToJsonData(JsonData jsonData, Dictionary dicc) { foreach (KeyValuePair keyValue in dicc) { Debug.Log(string.Format("{0}", keyValue.Key)); try { Dictionary localData = keyValue.Value as Dictionary; string key = keyValue.Key; jsonData[keyValue.Key] = new JsonData(); SetDicToJsonData(jsonData[keyValue.Key], localData); } catch { Debug.Log(string.Format("{0}-{1}", keyValue.Key, keyValue.Value.ToString())); jsonData[keyValue.Key] = keyValue.Value.ToString(); } } } void GetDicData(Dictionary dicc) { foreach (KeyValuePair keyValue in dicc) { Debug.Log(string.Format("{0}", keyValue.Key)); try { Dictionary localData = keyValue.Value as Dictionary; GetDicData(localData); } catch { Debug.Log(string.Format("{0}-{1}", keyValue.Key, keyValue.Value.ToString())); } } } void SetConfigData(string strinfo) { Debug.Log(strinfo); JsonData modeInfo = JsonMapper.ToObject(strinfo); IDictionary data = modeInfo; SetJsonDataToDic(data,dic); isFinish = true; //GetDicData(dic); } void RefreshWindowShow() { EditorGUILayout.BeginVertical(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("加载数据"); configPath = GUILayout.TextArea(configPath); if (GUILayout.Button("Brown", GUILayout.ExpandWidth(false))) { configPath = EditorUtility.OpenFilePanel("Resource path", configPath, "*.*"); EditorPrefs.SetString("IMPORTCCONFIG_DATAFOLDERPATH", configPath); StreamReader sr = File.OpenText(configPath); SetConfigData(sr.ReadToEnd()); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); foreach (string key in dic.Keys) { if (GUILayout.Button(key, GUILayout.ExpandWidth(false))) { curDic = dic[key] as Dictionary; } } if (isFinish) { if (curDic != null && curDic.Count > 0) { addKey = GUILayout.TextField(addKey); if (GUILayout.Button("添加key", GUILayout.ExpandWidth(false))) { curDic.Add(addKey, new Dictionary()); } } } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } void RefreshWindowShow1() { EditorGUILayout.BeginVertical(); for (int i = 0; i < curDic.Count; i++) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(30); var item = curDic.ElementAt(i); var item1 = (curDic[item.Key] as Dictionary); GUILayout.Label(item.Key); addKey = GUILayout.TextField(addKey); if (GUILayout.Button("添加key", GUILayout.ExpandWidth(false))) { item1.Add(addKey, ""); } if (GUILayout.Button("删除", GUILayout.ExpandWidth(false))) { curDic.Remove(item.Key); } EditorGUILayout.EndHorizontal(); for (int j = 0; j < item1.Count; j++) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(60); var item2 = item1.ElementAt(j); GUILayout.Label(item2.Key); item1[item2.Key] = GUILayout.TextField(item1[item2.Key].ToString()); if (GUILayout.Button("删除", GUILayout.ExpandWidth(false))) { item1.Remove(item2.Key); } EditorGUILayout.EndHorizontal(); } } EditorGUILayout.EndVertical(); } } }