/// <summary> /// ViewModel部分代码生成 /// </summary> /// <param name="protocals"></param> /// <param name="types"></param> /// <param name="sb"></param> private void CreateViewModelScript(List <string> protocals, List <Type> types, StringBuilder sb) { sb.Append("\t\t"); sb.AppendLine("/// <summary>"); sb.Append("\t\t"); sb.AppendLine("/// 显示模型模板"); sb.Append("\t\t"); sb.AppendLine("/// <summary>"); sb.Append("\t\t"); sb.AppendLine("public class LogicBase : BridgeUI.Binding.ViewModel"); sb.Append("\t\t"); sb.AppendLine("{"); sb.Append("\t\t\t"); sb.AppendLine("#region BindablePropertys"); for (int i = 0; i < protocals.Count; i++) { var protocal = protocals[i]; var typeName = GenCodeUtil.TypeStringName(types[i]); sb.Append("\t\t\t"); sb.AppendFormat("protected BindableProperty<{0}> m_{1};", typeName, protocal); sb.AppendLine(); } sb.Append("\t\t\t"); sb.AppendLine("#endregion BindablePropertys"); sb.AppendLine(); sb.Append("\t\t\t"); sb.AppendLine("#region Propertys"); for (int i = 0; i < protocals.Count; i++) { var protocal = protocals[i]; var typeName = GenCodeUtil.TypeStringName(types[i]); sb.Append("\t\t\t"); sb.AppendFormat("public {0} {1}", typeName, protocal); sb.AppendLine(); sb.Append("\t\t\t"); sb.AppendLine("{"); sb.Append("\t\t\t\t"); sb.AppendLine("get"); sb.Append("\t\t\t\t"); sb.AppendLine("{"); sb.Append("\t\t\t\t\t"); sb.AppendFormat("if(m_{0} == null)", protocal); sb.AppendLine(); sb.Append("\t\t\t\t\t"); sb.AppendLine("{"); sb.Append("\t\t\t\t\t\t"); sb.AppendFormat("m_{0} = GetBindableProperty<{1}>({2});", protocal, typeName, GetKeyword(protocal)); sb.AppendLine(); sb.Append("\t\t\t\t\t"); sb.AppendLine("}"); sb.Append("\t\t\t\t\t"); sb.AppendFormat("return m_{0};", protocal); sb.AppendLine(); sb.Append("\t\t\t\t"); sb.AppendLine("}"); sb.Append("\t\t\t\t"); sb.AppendLine("set"); sb.Append("\t\t\t\t"); sb.AppendLine("{"); sb.Append("\t\t\t\t\t"); sb.AppendFormat("if(m_{0} == null)", protocal); sb.AppendLine(); sb.Append("\t\t\t\t\t"); sb.AppendLine("{"); sb.Append("\t\t\t\t\t\t"); sb.AppendFormat("m_{0} = GetBindableProperty<{1}>({2});", protocal, typeName, GetKeyword(protocal)); sb.AppendLine(); sb.Append("\t\t\t\t\t"); sb.AppendLine("}"); sb.Append("\t\t\t\t\t"); sb.AppendFormat("m_{0}.Value = value;", protocal); sb.AppendLine(); sb.Append("\t\t\t\t"); sb.AppendLine("}"); sb.Append("\t\t\t"); sb.AppendLine("}"); } sb.Append("\t\t\t"); sb.AppendLine("#endregion Propertys"); sb.Append("\t\t"); sb.AppendLine("}"); }
/// <summary> /// View部分代码生成 /// </summary> /// <param name="sb"></param> private void CreateViewScript(StringBuilder sb) { var uicontrols = componentItems.Where(x => typeof(IUIControl).IsAssignableFrom(x.componentType)).ToArray(); sb.Append("\t\t"); sb.AppendLine("/// <summary>"); sb.Append("\t\t"); sb.AppendLine("/// 代码绑定"); sb.Append("\t\t"); sb.AppendLine("/// </summary>"); sb.Append("\t\t"); sb.AppendLine("protected override void OnBinding(UnityEngine.GameObject target)"); sb.Append("\t\t"); sb.AppendLine("{"); sb.Append("\t\t\t"); sb.AppendLine("base.OnBinding(target);"); sb.Append("\t\t\t"); sb.AppendFormat("var binding = target.GetComponent<{0}>();\r\n", refClassName); sb.Append("\t\t\t"); sb.AppendLine("if (binding != null)"); sb.Append("\t\t\t"); sb.AppendLine("{"); for (int i = 0; i < componentItems.Length; i++) { var componentItem = componentItems[i]; var viewBindings = componentItem.viewItems; for (int j = 0; j < viewBindings.Count; j++) { var viewItem = viewBindings[j]; sb.Append("\t\t\t\t"); sb.AppendFormat("Binder.RegistValueChange<{0}>(x => binding.{1}.{2} = x, {3});", GenCodeUtil.TypeStringName(viewItem.bindingTargetType.type), componentItem.name, viewItem.bindingTarget, GetKeyword(viewItem.bindingSource)); sb.AppendLine(); } var eventBindings = componentItem.eventItems; for (int j = 0; j < eventBindings.Count; j++) { var eventItem = eventBindings[j]; sb.Append("\t\t\t\t"); switch (eventItem.type) { case BindingType.Simple: sb.AppendFormat("Binder.RegistEvent(binding.{0}.{1}, {2});", componentItem.name, eventItem.bindingTarget, GetKeyword(eventItem.bindingSource)); break; case BindingType.Full: sb.AppendFormat("Binder.RegistEvent(binding.{0}.{1}, {2},binding.{3});", componentItem.name, eventItem.bindingTarget, GetKeyword(eventItem.bindingSource), componentItem.name); break; default: break; } sb.AppendLine(); } } if (innerFields != null) { for (int i = 0; i < innerFields.Length; i++) { var field = innerFields[i]; if (!typeof(UnityEngine.Object).IsAssignableFrom(field.FieldType)) { sb.Append("\t\t\t\t"); sb.AppendFormat("Binder.SetValue(binding.{0}, {1});", field.Name, GetKeyword(field.Name)); sb.AppendLine(); } } } if (uicontrols != null && uicontrols.Length > 0) { for (int i = 0; i < uicontrols.Length; i++) { var item = uicontrols[i]; sb.Append("\t\t\t\t"); sb.AppendFormat("RegistUIControl(binding.{0});", item.name); sb.AppendLine(); } } sb.Append("\t\t\t"); sb.AppendLine("}"); sb.Append("\t\t"); sb.AppendLine("}"); }