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 downloadConfigPath; string previous; Dictionary dic; Dictionary indexDic; //Dictionary curDic; //bool isFinish; string addKey; string targetPath; int index; // 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(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("加载config模板"); tempConfigPath = GUILayout.TextArea(tempConfigPath); if (GUILayout.Button("Brown", 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(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); downloadConfigPath = GUILayout.TextArea(downloadConfigPath); if (GUILayout.Button("下载config模板", GUILayout.ExpandWidth(false))) { if (string.IsNullOrEmpty(downloadConfigPath)) { return; } dic.Clear(); EditorPrefs.SetString("IMPORTCCONFIG_DownLOADCONFIGPATH", downloadConfigPath); EditorCoroutineRunner.StartEditorCoroutine(DownLoadTempComfig(downloadConfigPath)); } EditorGUILayout.EndHorizontal(); 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(); if (dic != null && dic.Count > 0) { index = 0; RefreshWindowShow2(dic); } 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(); indexDic = new Dictionary(); tempConfigPath = EditorPrefs.GetString("IMPORTCCONFIG_SOURCESFOLDERPATH"); targetPath = EditorPrefs.GetString("IMPORTCCONFIG_TARGETFOLDERPATH"); configPath = EditorPrefs.GetString("IMPORTCCONFIG_DATAFOLDERPATH"); downloadConfigPath = EditorPrefs.GetString("IMPORTCCONFIG_DownLOADCONFIGPATH"); } void SetJsonDataToDic(IDictionary data,Dictionary dicc) { foreach (var item in data.Keys) { string key = item.ToString(); 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(); //} void RefreshWindowShow2(Dictionary dicc,string indexDicKey = "0") { for (int i = 0; i < dicc.Count; i++) { var item = dicc.ElementAt(i); if (!indexDic.ContainsKey(indexDicKey)) { index = index + 1; indexDic.Add(indexDicKey, index); } index = indexDic[indexDicKey]; //Debug.Log(string.Format("{0}-{1}", item.Key, item.Value)); Dictionary localData = item.Value as Dictionary; if (localData == null) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(30 * index); GUILayout.Label(item.Key); dicc[item.Key] = GUILayout.TextField(dicc[item.Key].ToString()); if (GUILayout.Button("删除", GUILayout.ExpandWidth(false))) { dicc.Remove(item.Key); } EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.BeginHorizontal(); GUILayout.Space(30 * index); GUILayout.Label(item.Key); addKey = GUILayout.TextField(addKey); if (GUILayout.Button("添加key", GUILayout.ExpandWidth(false))) { localData.Add(addKey, ""); } if (GUILayout.Button("删除", GUILayout.ExpandWidth(false))) { localData.Remove(item.Key); } EditorGUILayout.EndHorizontal(); RefreshWindowShow2(localData, item.Key); } } } } }