/// <summary> /// 生成组件代码 /// </summary> /// <param name="trans"></param> /// <returns></returns> internal static string GetComponentCode(List <TranDto> trans, IniTool iniTool) { StringBuilder sb = new StringBuilder(); foreach (var item in trans) { string typeName = UIEditorUtils.GetExportType(item.Tran.name, iniTool); string mvvmTypeName = UIEditorUtils.GetMVVMExportType(typeName); if (typeName == typeof(Transform).Name) { sb.AppendLine($" this.{item.Tran.name} = this.transform.Find(\"{item.ParentPath}{item.Tran.name}\");"); } else if (typeName == typeof(GameObject).Name) { sb.AppendLine($" this.{item.Tran.name} = this.transform.Find(\"{item.ParentPath}{item.Tran.name}\").gameObject;"); } else { if (string.IsNullOrWhiteSpace(mvvmTypeName)) { sb.AppendLine($" this.{item.Tran.name} = this.transform.Find(\"{item.ParentPath}{item.Tran.name}\").GetComponent<{typeName}>();"); } else { sb.AppendLine($" this.raw_{item.Tran.name} = this.transform.Find(\"{item.ParentPath}{item.Tran.name}\").GetComponent<{typeName}>();"); sb.AppendLine(); sb.AppendLine($" this.BindingElement(this.raw_{item.Tran.name}, out this.{item.Tran.name});"); } } sb.AppendLine(); } return(sb.ToString()); }
internal static void GenerateItem() { try { if (!UIEditorUtils.CheckUIConfig()) { EditorUtility.DisplayDialog("错误", "配置表路径没有配置,请配置与Asset同级的config文件", "OK"); return; } InitData(); if (!Directory.Exists(_itemCodePath)) { Directory.CreateDirectory(_itemCodePath); } EditorUtility.DisplayProgressBar("生成Item", "正在生成Item", 0); UIEditorUtils.ErrorList = new List <string>(); Thread.Sleep(200); string[] strs = Directory.GetFiles(Path.Combine(Application.dataPath, _itemUIPath), "*.prefab"); float length = strs.Length; float index = 0; foreach (var item in strs) { index++; EditorUtility.DisplayProgressBar("生成Item", $"正在生成{Path.GetFileNameWithoutExtension(item)}", index / length); GameObject obj = AssetDatabase.LoadAssetAtPath <GameObject>(Path.Combine("Assets", _itemUIPath, Path.GetFileName(item))); List <TranDto> trans = new List <TranDto>(); UIEditorUtils.GetTrans(obj.transform, "", trans); trans.Reverse(); GenerateUserCode(obj.name); GenerateCode(obj.name + ".gen.cs", ItemTemplate, trans, _itemCodePath, Path.Combine(_itemUIResPath, Path.GetFileNameWithoutExtension(item))); Thread.Sleep(200); } EditorUtility.DisplayProgressBar("生成Item", "生成Item完成", 1); Thread.Sleep(200); EditorUtility.ClearProgressBar(); if (UIEditorUtils.ErrorList.Count > 0) { foreach (var item in UIEditorUtils.ErrorList) { EditorUtility.DisplayDialog("警告", item, "OK"); } } AssetDatabase.Refresh(); } catch (Exception ex) { Debug.LogError(ex); } finally { if (_iniTool != null) { _iniTool.Close(); } _iniTool = null; EditorUtility.ClearProgressBar(); } }
private static void GenerateCode(string scriptName, string templateText, List <TranDto> trans, string panelCodePath, string uiPath) { uiPath = uiPath.Replace(@"\", "/"); string temp = templateText; string varCode = UIEditorUtils.GetVarCode(trans, _iniTool); string componentCode = UIEditorUtils.GetComponentCode(trans, _iniTool); FieldInfo injectField = null; string injectData = UIEditorUtils.GetInjectModelField(UIEditorUtils.GetTypeForPanel(Path.GetFileName(uiPath)), ref injectField); string bindingData = UIEditorUtils.GetBindingData(UIEditorUtils.GetTypeForPanel(Path.GetFileName(uiPath)), injectField); temp = temp.Replace("{GeneratedTime}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); temp = temp.Replace("{UIPath}", uiPath); temp = temp.Replace("{ClassName}", Path.GetFileName(uiPath)); temp = temp.Replace("{InjectData}", injectData); temp = temp.Replace("{BindData}", bindingData); temp = temp.Replace("{Variable}", varCode); temp = temp.Replace("{BindComponent}", componentCode); File.WriteAllText(Path.Combine(panelCodePath, scriptName), temp, new UTF8Encoding()); }