static void GenClassUsingCodeGenerator() { QNamespaceDefine namespaceDefine = new QNamespaceDefine(); namespaceDefine.Name = "QFramework.Example"; namespaceDefine.FileName = "ExampleClass.cs"; namespaceDefine.GenerateDir = Application.dataPath + "/Example/20.CodeGenerator/"; QClassDefine classDefine = new QClassDefine(); classDefine.Name = "ExampleClass"; namespaceDefine.Classes.Add(classDefine); QCodeGenerator.Generate(namespaceDefine); }
public static void GeneratePathScript() { AssetDatabase.SaveAssets(); IOUtils.CreateDirIfNotExists(m_DefaultPathScriptGenerateForder); string[] fullPathFileNames = Directory.GetFiles(m_DefaultPathConfigGenerateForder, "*PathConfig.asset", SearchOption.AllDirectories); foreach (string fullPathFileName in fullPathFileNames) { Debug.Log(fullPathFileName); if (!fullPathFileName.EndsWith(".meta")) { Debug.Log("gen: " + fullPathFileName); PathConfig config = AssetDatabase.LoadAssetAtPath <PathConfig> (fullPathFileName); QNamespaceDefine nameSpace = new QNamespaceDefine(); nameSpace.Name = string.IsNullOrEmpty(config.NameSpace) ? "QFramework" : config.NameSpace; nameSpace.FileName = config.name + ".cs"; nameSpace.GenerateDir = string.IsNullOrEmpty(config.ScriptGeneratePath) ? m_DefaultPathScriptGenerateForder : IOUtils.CreateDirIfNotExists("Assets/" + config.ScriptGeneratePath); var classDefine = new QClassDefine(); classDefine.Comment = config.Description; classDefine.Name = config.name; nameSpace.Classes.Add(classDefine); Debug.Log(nameSpace.GenerateDir); foreach (var pathItem in config.List) { if (!string.IsNullOrEmpty(pathItem.Name)) { var variable = new QVariable(QAccessLimit.Private, QCompileType.Const, QTypeDefine.String, "m_" + pathItem.Name, pathItem.Path); classDefine.Variables.Add(variable); var property = new Property(QAccessLimit.Public, QCompileType.Static, QTypeDefine.String, pathItem.Name, pathItem.PropertyGetCode, pathItem.Description); classDefine.Properties.Add(property); } } QCodeGenerator.Generate(nameSpace); EditorUtility.SetDirty(config); Resources.UnloadAsset(config); } } AssetDatabase.SaveAssets(); }
static void QCodeGeneratorExample() { //定义命名空间 QNamespaceDefine namespaceDefine = new QNamespaceDefine(); namespaceDefine.Name = "QFramework.Example"; namespaceDefine.FileName = "QExampleClass.cs"; namespaceDefine.GenerateDir = Application.dataPath + "/Example/20.CodeGenerator/"; //定义类 QClassDefine classDefine = new QClassDefine(); classDefine.Name = "QExampleClass"; namespaceDefine.Classes.Add(classDefine); // 定义变量 var variable = new QVariable(); variable.AccessLimit = QAccessLimit.Private; variable.CompileType = QCompileType.Const; variable.Type = QTypeUtil.GetCodeType(QTypeDefine.String); variable.Name = "mVariable"; variable.Value = "Hello"; classDefine.Variables.Add(variable); // 定义属性 var property = new QProperty(); property.AccessLimit = QAccessLimit.Public; property.CompileType = QCompileType.Static; property.Type = QTypeUtil.GetCodeType(QTypeDefine.String); property.Name = "Variable"; property.GetReturnCode = "mVariable"; classDefine.Properties.Add(property); // 生成 QCodeGenerator.Generate(namespaceDefine); }