public static string GetWidgetClassName(Type widgetType) { if (!WidgetNames.ContainsKey(widgetType)) { WidgetNames.Add(widgetType, Java.Lang.Class.FromType(widgetType).Name); } return(WidgetNames[widgetType]); }
public static void GenerateUICode() { GameObject go = Selection.activeGameObject; if (null == go) { return; } if (!go.name.StartsWith("UI_")) { return; } string defineName = go.name.Substring(go.name.IndexOf('_') + 1, go.name.Length - go.name.IndexOf('_') - 1); List <string[]> controls = new List <string[]>(); Transform transform = go.transform; WidgetNames.Clear(); if (!FindCareUI(transform, controls, "")) { return; } if (controls.Count <= 0) { return; } List <string> cslines = new List <string>(); FileStream file = new FileStream(CurrentDirectory + "\\Assets\\Script\\Editor\\UIcodeLayoutTemp.txt", FileMode.Open); if (file != null && file.Length > 0) { using (StreamReader sr = new StreamReader(file)) { string line = null; while ((line = sr.ReadLine()) != null) { cslines.Add(line); } sr.Close(); } } file.Close(); int index = 0; StringBuilder cstemp = new StringBuilder(); while (index < cslines.Count) { //有宏定义#Control if (cslines[index].Contains("#Control")) { int end = index + 1; StringBuilder sb = new StringBuilder(); while (!cslines[end].Contains("#endControl")) { sb.Append(cslines[end]); end++; } for (int i = 0; i < controls.Count; i++) { string TypeStr = controls[i][0]; string ControlName = controls[i][1]; string s = sb.ToString().Replace("${ControlName}", ControlName); s = s.Replace("${type}", TypeStr); cstemp.Append(s + "\n"); } //结束后下一行 index = end + 1; } else if (cslines[index].Contains("#ParseCode")) { int end = index + 1; StringBuilder sb = new StringBuilder(); while (!cslines[end].Contains("#endParseCode")) { sb.Append(cslines[end]); end++; } for (int i = 0; i < controls.Count; i++) { string TypeStr = controls[i][0]; string ControlName = controls[i][1]; string ControlPath = controls[i][2]; string s = sb.ToString().Replace("${ControlName}", ControlName); s = s.Replace("${type}", TypeStr); s = s.Replace("${ControlPath}", ControlPath + "_" + ControlName); cstemp.Append(s + "\n"); } //结束后下一行 index = end + 1; } else { cstemp.Append(cslines[index] + "\n"); index++; } } string findpath = AssetDatabase.GetAssetPath(go); int pathstart = findpath.LastIndexOf("UI"); int pathend = findpath.LastIndexOf('.') - pathstart; string path = findpath.Substring(pathstart, pathend); string[] pathArr = path.Split('/'); string writePath = outpath + "\\"; string namespacePath = ""; //if (pathArr.Length > 1) //{ // for (int i = 0; i < pathArr.Length - 1; i++) // { // writePath += pathArr[i] + "\\"; // namespacePath += pathArr[i] + "."; // } //} writePath += defineName + "\\"; cstemp = cstemp.Replace("${defineName}", defineName); cstemp = cstemp.Replace("${nameSpacePath}", namespacePath); //生成cs代码文件 if (!Directory.Exists(writePath)) //若文件夹不存在则新建文件夹 { Directory.CreateDirectory(writePath); //新建文件夹 } using (FileStream outfile = new FileStream(writePath + "UILayout" + defineName + ".cs", FileMode.Create)) { StreamWriter sw = new StreamWriter(outfile, Encoding.UTF8); sw.Write(cstemp); sw.Close(); outfile.Close(); } Debug.Log("预设" + go.name + "生成layout代码完成"); //判断是否已经存在uictl if (@System.IO.File.Exists(writePath + "UICtrl" + defineName + ".cs")) { return; } //读取Ctrl魔板 List <string> ctrlcslines = new List <string>(); FileStream ctrlfile = new FileStream(CurrentDirectory + "\\Assets\\Script\\Editor\\UIcodeCtlTemp.txt", FileMode.Open); if (ctrlfile != null && ctrlfile.Length > 0) { using (StreamReader sr = new StreamReader(ctrlfile)) { string line = null; while ((line = sr.ReadLine()) != null) { ctrlcslines.Add(line); } sr.Close(); } } ctrlfile.Close(); int ctlindex = 0; StringBuilder ctlsb = new StringBuilder(); while (ctlindex < ctrlcslines.Count) { ctlsb.Append(ctrlcslines[ctlindex] + "\n"); ctlindex++; } string ss = ctlsb.ToString(); ss = ss.Replace("$(defineName}", defineName); ss = ss.Replace("$(definePath}", path); ss = ss.Replace("${nameSpacePath}", namespacePath); using (FileStream outfile = new FileStream(writePath + "UICtrl" + defineName + ".cs", FileMode.Create)) { StreamWriter sw = new StreamWriter(outfile, Encoding.UTF8); sw.Write(ss); sw.Close(); outfile.Close(); Debug.LogWarning("预设" + go.name + "生成UIctrl代码完成"); } }