另一種創建代碼模版的方法

參考【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生成。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章