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

参考【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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值