using System.Collections.Generic; using System.IO; namespace Thousandto.DIY { /// /// 修改UIEventDefine.cs文件,添加open和close的事件定义 /// public static class ModifyUIEventDefine { public const string UIEVENT_FILE_PATH = "Assets/Code/Global/Event/UIEventDefine.cs"; public static void ModifyFile(string formName, int uiconfigID) { string openEventDefine = string.Format("{0}_OPEN", formName); string openEventValue = string.Format("{0} + EventConstDefine.EVENT_UI_BASE_ID,", uiconfigID * 10); string closeEventDefine = string.Format("{0}_CLOSE", formName); string closeEventValue = string.Format("{0} + EventConstDefine.EVENT_UI_BASE_ID,", uiconfigID * 10 + 9); List lineList = new List(); List endSignList = new List(); StreamReader sr = new StreamReader(UIEVENT_FILE_PATH); int changed = 0; string line = ""; while ((line = sr.ReadLine()) != null) { if (line.IndexOf("}") >= 0 && line.IndexOf("//") == -1) { endSignList.Add(line); continue; } int checkValue = checkLine(ref line, openEventDefine, openEventValue); if (checkValue > changed) changed = checkValue; checkValue = checkLine(ref line, closeEventDefine, closeEventValue); if (checkValue > changed) changed = checkValue; lineList.Add(line); } sr.Close(); //不存在,需要新增 if (changed == 0) { string newOpenDefine = string.Format(" {0} = {1}", openEventDefine, openEventValue); string newCloseDefine = string.Format(" {0} = {1}", closeEventDefine, closeEventValue); lineList.Add(newOpenDefine); lineList.Add(newCloseDefine); } //不存在或者做了修改 if (changed == 0 || changed == 2) { lineList.AddRange(endSignList); WriteToFile(lineList); } else UnityEngine.Debug.LogError("UIEventDefine已经定义窗体Open和Close事件: " + formName); } /// /// 检查form的事件定义是否存在,如果存在则判断是否需要修改 /// /// 已知行 /// 事件定义 /// 事件值 /// 0:不存在; 1: 存在但相同; 2:存在且不同 private static int checkLine(ref string line, string define, string value) { int ret = 0; if (line.IndexOf(define) >= 0) { if (line.IndexOf(value) == -1) { line = string.Format(" {0} = {1}", define, value); ret = 2; } else ret = 1; } return ret; } private static void WriteToFile(List contentList) { if (contentList == null || contentList.Count == 0) return; StreamWriter sw = new StreamWriter(UIEVENT_FILE_PATH); for (int i = 0; i < contentList.Count; ++i) { sw.WriteLine(contentList[i]); } sw.Close(); UnityEngine.Debug.LogError("保存到UIEventDefine成功"); } } }