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(); }
static void ModelInit(CodeGenerator gen, DataContext rootContext, gamedef.CodeGenModule module) { switch (module.ModelGen) { case gamedef.ModelGenType.MGT_Singleton: { gen.PrintLine("_Model = Framework.ModelManager.Instance.Get<", ModelTemplate.ClassName(rootContext), ">();"); gen.PrintLine(); } break; case gamedef.ModelGenType.MGT_Instance: { gen.PrintLine("_Model = new ", ModelTemplate.ClassName(rootContext), "();"); gen.PrintLine(); } break; } }
public static void ClassBody(CodeGenerator gen, DataContext rootContext, List <DataContext> propContextList, gamedef.CodeGenModule module) { gen.PrintLine("// Generated by github.com/davyxu/cellorigin"); gen.PrintLine("using UnityEngine;"); gen.PrintLine("using UnityEngine.UI;"); gen.PrintLine("using System;"); gen.PrintLine(); gen.PrintLine("partial class ", ClassName(rootContext), " : Framework.BasePresenter"); gen.PrintLine("{"); gen.In(); gen.PrintLine(ModelTemplate.ClassName(rootContext), " _Model;"); gen.PrintLine(); // 网络Peer声明 foreach (gamedef.CodeGenPeer peer in module.Peer) { NetworkDeclare(gen, peer); } if (module.ModelGen != gamedef.ModelGenType.MGT_Manual) { // 属性声明 foreach (DataContext propContext in propContextList) { PropertyBody(gen, rootContext, propContext); } } gen.PrintLine("public void Init( )"); gen.PrintLine("{"); gen.In(); ModelInit(gen, rootContext, module); if (module.ModelGen != gamedef.ModelGenType.MGT_Manual) { foreach (DataContext propContext in propContextList) { PropertyInit(gen, rootContext, propContext); } } // 网络获取及消息绑定 foreach (gamedef.CodeGenPeer peer in module.Peer) { NetworkRegisterBody(gen, peer); } gen.Out(); gen.PrintLine("}"); // Bind gen.PrintLine(); foreach (DataContext propContext in propContextList) { CommandBody(gen, rootContext, propContext); } foreach (gamedef.CodeGenPeer peer in module.Peer) { foreach (string msgType in peer.RecvMessage) { NetworkCallbackBody(gen, rootContext, peer, msgType); } } gen.Out(); gen.PrintLine("}"); // Class }