public void GenScript(BaseUI ui) { rootGo = ui.gameObject; uiType = ui.GetType(); string viewClassName = uiType.Name + "View"; CodeGener gener = new CodeGener(UIView.uiViewNameSpace, viewClassName); gener.AddBaseType("UIView") .AddImport("ResetCore.UGUI"); rootGo.transform.DoToSelfAndAllChildren((tran) => { GameObject go = tran.gameObject; if (!go.name.StartsWith(UIView.genableSign)) { return; } string goName = go.name.Replace(UIView.genableSign, string.Empty); var coms = go.GetComponents <Component>(); foreach (var com in coms) { Type comType = com.GetType(); if (!UIView.uiCompTypeList.Contains(comType)) { continue; } gener.AddMemberProperty(comType, UIView.comNameDict[comType] + goName); } }); gener.GenCSharp(PathEx.Combine(Application.dataPath, UIView.uiViewScriptPath)); }
public void GenCS(IDataReadable reader) { string className = reader.currentDataTypeName; CodeGener protobufBaseGener = new CodeGener(ProtobufData.nameSpace, className); protobufBaseGener.newClass.AddMemberCostomAttribute("ProtoBuf.ProtoContract"); protobufBaseGener.AddImport("System", "ProtoBuf"); List <string> comment = reader.GetComment(); protobufBaseGener .AddBaseType("ProtobufData<" + className + ">") .AddMemberField(typeof(string), "fileName", (member) => { member.AddFieldMemberInit("\"" + className + "\""); }, System.CodeDom.MemberAttributes.Static | System.CodeDom.MemberAttributes.Final | System.CodeDom.MemberAttributes.Public); reader.GetTitle().ForEach((i, title) => { string[] titleSplit = title.Split('|'); string varName = titleSplit[0]; string typeName = titleSplit[1]; protobufBaseGener.AddMemberProperty(typeName.GetTypeByString(), varName, (member) => { member.AddComment(comment[i], true); member.AddMemberCostomAttribute("ProtoBuf.ProtoMember", (i + 1).ToString()); }); }); PathEx.MakeFileDirectoryExist(PathConfig.GetLoaclGameDataClassPath(PathConfig.DataType.Protobuf)); protobufBaseGener.GenCSharp(PathConfig.GetLoaclGameDataClassPath(PathConfig.DataType.Protobuf)); }
private static void CreateBundleClass() { CodeGener gener = new CodeGener(nameSpace, bundleClassName); gener.AddBaseType("ObjDataBundle<" + className + ">"); gener.newClass.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(SerializableAttribute)))); gener.GenCSharp(classPath); }
//生成界面代码 public void GenViewScript(BaseUI ui) { rootGo = ui.gameObject; uiType = ui.GetType(); string viewClassName = uiType.Name + "View"; if (File.Exists(PathEx.Combine(Application.dataPath, UIView.uiViewScriptPath, viewClassName + ".cs"))) { if (!EditorUtility.DisplayDialog("提示", "已存在" + viewClassName + ",是否要覆盖?", "继续", "取消")) { return; } } CodeGener gener = new CodeGener(UIView.uiViewNameSpace, viewClassName); gener.AddBaseType(typeof(UIView).Name) .AddImport("ResetCore.UGUI"); rootGo.transform.DoToSelfAndAllChildren((tran) => { GameObject go = tran.gameObject; if (!go.name.StartsWith(UIView.genableSign)) { return; } string goName = go.name.Replace(UIView.genableSign, string.Empty); if (goName.Contains(splitGoNameAndProperty[0])) { goName = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries)[0]; } gener.AddMemberProperty(typeof(GameObject), UIView.goName + goName); var coms = go.GetComponents <Component>(); foreach (var com in coms) { Type comType = com.GetType(); if (!UIView.uiCompTypeList.Contains(comType)) { continue; } gener.AddMemberProperty(comType, UIView.comNameDict[comType] + goName); } }); gener.GenCSharp(PathEx.Combine(Application.dataPath, UIView.uiViewScriptPath)); }
/// <summary> /// 生成序列化 /// </summary> /// <param name="type"></param> public static void GenSerializer(Type type) { if (type == null) { return; } var typeName = type.FullName; var finalName = GetSerializerName(type); CodeGener g = new CodeGener(serializerNamespace, finalName); g.AddBaseType("IXmlSerializer<" + type.FullName + ">"); g.AddImport("System.Xml"); //ToXElement方法 CodeMemberMethod toXElementMethod = new CodeMemberMethod(); toXElementMethod.Name = "Write"; toXElementMethod.Attributes = MemberAttributes.Public; toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(XmlElement)), "xElement")); toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "name")); toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeName), "obj")); //函数体 toXElementMethod.Statements.AddRange(GetToXElementCodeStatements(type)); g.newClass.Members.Add(toXElementMethod); //Parse方法 CodeMemberMethod parseMethod = new CodeMemberMethod(); parseMethod.Name = "Read"; parseMethod.Attributes = MemberAttributes.Public; parseMethod.ReturnType = new CodeTypeReference(type); parseMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(XmlElement)), "xElement")); parseMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "name")); //函数体 parseMethod.Statements.AddRange(GetParseXElementCodeStatements(type)); g.newClass.Members.Add(parseMethod); //生成代码 g.GenCSharp(SerializerGenerPath); }
//生成数据代码 public void GenModelScript(BaseUI ui) { rootGo = ui.gameObject; uiType = ui.GetType(); string modelClassName = uiType.Name + "Model"; if (File.Exists(PathEx.Combine(Application.dataPath, UIModel.uiModelScriptPath, modelClassName + ".cs"))) { if (!EditorUtility.DisplayDialog("提示", "已存在" + modelClassName + ",是否要覆盖?", "继续", "取消")) { return; } } CodeGener gener = new CodeGener(UIModel.uiModelNameSpace, modelClassName); gener.AddBaseType(typeof(UIModel).Name) .AddImport("ResetCore.UGUI") .AddImport("ResetCore.Event"); List <string> hasAddedProperty = new List <string>(); CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public; rootGo.transform.DoToSelfAndAllChildren((tran) => { GameObject go = tran.gameObject; if (!go.name.StartsWith(UIModel.genableSign)) { return; } string goName = go.name.Replace(UIModel.genableSign, string.Empty); string propertyName; string typeName; if (!goName.Contains(splitGoNameAndProperty[0])) { return; } string[] tempstrs = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries); goName = tempstrs[0]; if (!tempstrs[1].Contains(splitComponentAndProperty[0])) { EditorUtility.WarnPrefab(go, "命名错误", go.name + "的格式不正确,标准格式为“g-View变量名@组件名->Model属性名:变量类型”", "好的"); return; } propertyName = tempstrs[1].Split(splitComponentAndProperty, StringSplitOptions.RemoveEmptyEntries)[1]; if (!propertyName.Contains(splitPropertyAndType.ToString())) { typeName = "EventProperty<string>"; } tempstrs = propertyName.Split(splitPropertyAndType, StringSplitOptions.RemoveEmptyEntries); propertyName = tempstrs[0]; typeName = "EventProperty<" + tempstrs[1] + ">"; if (!hasAddedProperty.Contains(propertyName)) { gener.AddMemberProperty(typeName, propertyName); hasAddedProperty.Add(propertyName); constructor.Statements.AddRange(new CodeStatement[] { new CodeCommentStatement("From " + go.name), new CodeSnippetStatement(propertyName + " = new " + typeName + "();") }); } }); gener.newClass.Members.Add(constructor); gener.GenCSharp(PathEx.Combine(Application.dataPath, UIModel.uiModelScriptPath)); }
//生成自动绑定代码 public void GenBindScript(BaseUI ui) { rootGo = ui.gameObject; uiType = ui.GetType(); string binderClassName = uiType.Name + "Binder"; if (File.Exists(PathEx.Combine(Application.dataPath, UIBinder.uiBinderScriptPath, binderClassName + ".cs"))) { if (!EditorUtility.DisplayDialog("提示", "已存在" + binderClassName + ",是否要覆盖?", "继续", "取消")) { return; } } CodeGener gener = new CodeGener(UIBinder.uiBinderNameSpace, binderClassName); gener.AddBaseType(typeof(UIBinder).Name) .AddImport("ResetCore.UGUI") .AddImport("ResetCore.Event") .AddImport(UIView.uiViewNameSpace) .AddImport(UIModel.uiModelNameSpace); CodeMemberMethod initMethod = new CodeMemberMethod(); initMethod.AddComment("Used to bind view and model", true); initMethod.Name = "Bind"; string viewClassName = uiType.Name + "View"; string modelClassName = uiType.Name + "Model"; initMethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] { new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(UIView).Name), "view"), new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(UIModel).Name), "model"), }); initMethod.Attributes = MemberAttributes.Public | MemberAttributes.Override; List <string> hasAddedProperty = new List <string>(); rootGo.transform.DoToSelfAndAllChildren((tran) => { GameObject go = tran.gameObject; if (!go.name.StartsWith(UIBinder.genableSign)) { return; } string goName = go.name.Replace(UIBinder.genableSign, string.Empty); string comName; string propertyName; string typeName; if (!goName.Contains(splitGoNameAndProperty[0])) { return; } string[] tempstrs = goName.Split(splitGoNameAndProperty, StringSplitOptions.RemoveEmptyEntries); goName = tempstrs[0]; if (!tempstrs[1].Contains(splitComponentAndProperty[0])) { EditorUtility.WarnPrefab(go, "命名错误", go.name + "的格式不正确,标准格式为“g-View变量名@组件名->Model属性名:变量类型”", "好的"); return; } tempstrs = tempstrs[1].Split(splitComponentAndProperty, StringSplitOptions.RemoveEmptyEntries); comName = tempstrs[0]; propertyName = tempstrs[1]; if (!propertyName.Contains(splitPropertyAndType.ToString())) { typeName = "EventProperty<string>"; } tempstrs = propertyName.Split(splitPropertyAndType, StringSplitOptions.RemoveEmptyEntries); propertyName = tempstrs[0]; typeName = "EventProperty<" + tempstrs[1] + ">"; initMethod.Statements.AddRange(new CodeStatement[] { new CodeCommentStatement("From " + go.name), new CodeSnippetStatement("((" + viewClassName + ")view)." + comName + goName + ".Bind(((" + modelClassName + ")model)." + propertyName + ");"), }); }); gener.newClass.Members.Add(initMethod); gener.GenCSharp(PathEx.Combine(Application.dataPath, UIBinder.uiBinderScriptPath)); }