另一种创建代码模版的方法

来源:互联网 发布:java 迭代器实现原理 编辑:程序博客网 时间:2024/04/28 07:07

参考【Unity编辑器】Unity基于模板生成代码的原理与应用、创建lua脚本模版
明白了可以在vs中用snippet editor创建模版代码后,放在指定目录,并重写CreateScriptAssetFromTemplate函数,根据文件名来生成对应的代码。
正巧项目是用了puremvc框架,针对ui预制,正好要生成对应的view和mediator脚本,为了提高工作效率,也想一键生成ui预制对应的的脚本。之前不了解unity的创建过程,用的方法比较笨,不多说,创建Mediator代码如下:

 private static void CreateMediator(string mediatorPath, string mediatorName, StringBuilder content,UIType type)    {        content.Remove(0, content.Length);        content.AppendLine("using PureMVC.Patterns; \n using PureMVC.Interfaces; \n");        content.AppendFormat("public class {0} : UIMediator {{ \n", mediatorName);        content.AppendFormat("public new static string NAME = \"{0}\";\n", mediatorName);        if(type==UIType.Tip)        content.AppendFormat(" public {0}(int id,UIBase ui) : base(NAME+id, ui){{ }} \n", mediatorName);        else content.AppendFormat(" public {0}(UIBase ui) : base(NAME, ui){{ }} \n", mediatorName);        content.AppendFormat(" public override void HandleNotification(INotification notification){{ }}\n }}");        using (var sw = new StreamWriter(File.Open(mediatorPath, FileMode.OpenOrCreate)))            sw.Write(content.ToString());    }这种方法的缺点是,需要针对预制去生成。并不能类似Asset-Create Script生成。
0 0
原创粉丝点击