static void GenCodeByPrefab(GameObject rootObj, gamedef.CodeGenModule module) { if (rootObj == null) { return; } if (rootObj.name != module.Name) { Debug.LogError("请保证 Prefab文件名, Prefab名, Module名 三者一致: " + module.Name); return; } var rootContext = rootObj.GetComponent <DataContext>(); if (rootContext == null) { Debug.LogError("根对象组件类型没有发现DataContext组件"); return; } // 必须从View的根开始生成 if (rootContext.Type != WidgetType.View) { Debug.LogError("根对象组件类型必须是View类型 " + rootContext.Name); return; } var ctxList = new List <DataContext>(); // 深度遍历所有的DataContext foreach (Transform childTrans in rootObj.transform) { IterateDataContext(rootContext, childTrans, ref ctxList); } // 生成View代码 { var gen = new CodeGenerator(); ViewTemplate.Delete(rootContext); ViewTemplate.ClassBody(gen, rootContext, ctxList); ViewTemplate.Save(gen, rootContext); } // 生成Presenter代码 if (!module.NoGenPresenterCode) { var gen = new CodeGenerator(); PresenterTemplate.Delete(rootContext); PresenterTemplate.ClassBody(gen, rootContext, ctxList, module); PresenterTemplate.Save(gen, rootContext); } AssetDatabase.Refresh(); }
public static void ClassBody(CodeGenerator gen, DataContext rootContext, List <DataContext> widgetContextList) { gen.PrintLine("// Generated by github.com/davyxu/cellorigin"); gen.PrintLine("using UnityEngine;"); gen.PrintLine("using UnityEngine.UI;"); gen.PrintLine(); gen.PrintLine("partial class ", ClassName(rootContext), " : Framework.BaseView"); gen.PrintLine("{"); gen.In(); gen.PrintLine(PresenterTemplate.ClassName(rootContext), " _Presenter;"); gen.PrintLine(); // TODO 变量声明代码 foreach (DataContext widgetContext in widgetContextList) { WidgetDeclare(gen, widgetContext); } gen.PrintLine(); gen.PrintLine("public override void Bind( Framework.BasePresenter presenter )"); gen.PrintLine("{"); gen.In(); gen.PrintLine("_Presenter = presenter as ", PresenterTemplate.ClassName(rootContext), ";"); gen.PrintLine(); gen.PrintLine("var trans = this.transform;"); gen.PrintLine(); // TODO List中的名称重名检查 // 打印控件搜索代码 foreach (DataContext widgetContext in widgetContextList) { FindWidgetlAssignToVar(gen, rootContext, widgetContext); } gen.PrintLine(); // 打印控件绑定代码 foreach (DataContext widgetContext in widgetContextList) { WidgetBind(gen, rootContext, widgetContext); } gen.Out(); gen.PrintLine("}"); // Bind gen.PrintLine(); gen.Out(); gen.PrintLine("}"); // Class }
// 一个View变量的绑定代码 public static void WidgetBind(CodeGenerator gen, DataContext rootContext, DataContext widgetContext) { switch (widgetContext.Type) { case WidgetType.ScrollRect: { gen.PrintLine("Framework.Utility.BindCollection<int, ", PresenterTemplate.ClassItemName(widgetContext), ", ", ClassItemName(widgetContext), ">( _Presenter.", PresenterTemplate.Property(widgetContext), ", ", WidgetVariable(widgetContext), " );" ); gen.PrintLine(); } break; case WidgetType.Button: { gen.PrintLine(WidgetVariable(widgetContext), ".onClick.AddListener( _Presenter.", PresenterTemplate.Command(widgetContext), " );"); } break; case WidgetType.InputField: case WidgetType.Text: { if (widgetContext.SyncType == DataSyncType.ViewToPresenter || widgetContext.SyncType == DataSyncType.TwoWay) { gen.PrintLine(WidgetVariable(widgetContext), ".onValueChanged.AddListener( x =>"); gen.PrintLine("{"); gen.In(); gen.PrintLine("_Presenter.", PresenterTemplate.Property(widgetContext), " = x;"); gen.Out(); gen.PrintLine("} );"); // 初始赋值 gen.PrintLine("_Presenter.", PresenterTemplate.Property(widgetContext), " = ", WidgetData(widgetContext), ";"); } if (widgetContext.SyncType == DataSyncType.PresenterToView || widgetContext.SyncType == DataSyncType.TwoWay) { gen.PrintLine("_Presenter.", PresenterTemplate.Event(widgetContext), " += delegate()"); gen.PrintLine("{"); gen.In(); switch (widgetContext.Type) { case WidgetType.Text: case WidgetType.InputField: { gen.PrintLine(WidgetVariable(widgetContext), ".text = _Presenter.", PresenterTemplate.Property(widgetContext), ";"); } break; } gen.Out(); gen.PrintLine("};"); gen.PrintLine("if ( _Presenter.", PresenterTemplate.Property(widgetContext), " != null )"); gen.PrintLine("{"); gen.In(); // 初始赋值 gen.PrintLine(WidgetData(widgetContext), " = ", "_Presenter.", PresenterTemplate.Property(widgetContext), ";"); gen.Out(); gen.PrintLine("}"); } gen.PrintLine(); } break; } }
public override void OnInspectorGUI() { DataContext ctx = target as DataContext; GUILayout.BeginHorizontal(); if (GUILayout.Button("Detect Type")) { ctx.Detect(); } ctx.Type = (WidgetType)EditorGUILayout.EnumPopup(ctx.Type); GUILayout.EndHorizontal(); switch (ctx.Type) { case WidgetType.InputField: case WidgetType.Text: { ctx.SyncType = (DataSyncType)EditorGUILayout.EnumPopup("SyncType", ctx.SyncType); break; } } switch (ctx.Type) { case WidgetType.View: { EditorGUILayout.LabelField("View: " + ViewTemplate.ClassName(ctx)); EditorGUILayout.LabelField("Presenter: " + PresenterTemplate.ClassName(ctx)); break; } case WidgetType.ScrollRect: { EditorGUILayout.LabelField("Instance in View: _" + ctx.Name); EditorGUILayout.LabelField("Property in Presenter: " + ctx.Name); EditorGUILayout.LabelField("Item View: " + ViewTemplate.ClassItemName(ctx)); EditorGUILayout.LabelField("Item Presenter: " + PresenterTemplate.ClassItemName(ctx)); break; } case WidgetType.Button: { EditorGUILayout.LabelField("Command: " + PresenterTemplate.Command(ctx)); break; } case WidgetType.InputField: case WidgetType.Text: { EditorGUILayout.LabelField("Instance in View: _" + ctx.Name); EditorGUILayout.LabelField("Property in Presenter: " + ctx.Name); break; } } if (GUILayout.Button("Add To Child")) { ctx.AddToTopChild(); } if (GUILayout.Button("Remove Child")) { if (EditorUtility.DisplayDialog("移除确认?", "移除所有子节点的DataContext", "是", "否")) { ctx.RemoveAllChild(); } } if (GUILayout.Button("Detect Child")) { ctx.DetectAllChild(); } base.OnInspectorGUI(); }