当前位置:   article > 正文

C#模板生成_c# 模板

c# 模板
简要说明

 创建代码有多种方式。最简单的,就按编辑器最初始的方法生成。稍微会点的就可以自己修改编辑器的模板。再深入一点的就可以自己定义一系列代码模板,保存到文本文件,再读出来,然后修改一些模板信息。用unity3d程序,我倒是习惯了在这程序中创建一个script而不是在vistual studio中去新建代码。这样的话,所有的代码类型,如Interface,struct,enum都需要在创建出一个”不正确“的类后修改,修改是个重复的过程,这个过程必须去掉。

 而遗憾的是unity至今还是对所有的代码一视同仁,连图标都是用c#这么“经典”的一个图标。前一段时候,用过一些方法,无非是编写一个代码模板文档,创建时,调用这些模板。最大的缺陷是太复杂,在不同的项目或电脑上都要靠一些不想要的模板文件。

 本文将使用Microsoft自己带的代码格式方案实现代码的生成(因为考虑到unity3d中暂不能很好的使用.net4.5,代码解析的模块还没有开发)。

成果展示

 1.如果你喜欢简单一点,直接点右键快速创建不同的类


2.当然,也有基本的配制窗口

 

 3.如果你需要创建一个不继承于其他类的数据模型类,你可以这样配制

 

 4.以上这个配制后,点击create就可以生成代码模板到指定的文件夹了

 

5.以下是一个接口,创建效果。此外你已经熟悉代码规范了就可以在设置里面取消显示

 

技术要点

 1.将c#代码单元生成字符串

  代码也是一个个的文档,官方都有文档的格式,c#也不例外。这里只是按格式写,然后生成字符串,

需要说明的是c#并不能真的从string变成文档结构,至少官方貌似没有提供,但如果你可以使用net4.5以上的环境,

倒是可以研究下NRefactory

  1. using (Microsoft.CSharp.CSharpCodeProvider cprovider = new Microsoft.CSharp.CSharpCodeProvider())
  2. {
  3. StringBuilder fileContent = new StringBuilder();
  4. var option = new System.CodeDom.Compiler.CodeGeneratorOptions();
  5. option.BlankLinesBetweenMembers = false;
  6. using (StringWriter sw = new StringWriter(fileContent))
  7. {
  8. cprovider.GenerateCodeFromNamespace(nameSpace, sw, option);
  9. }
  10. return fileContent.ToString();
  11. }

2.结合unity自己创建代码的方式

因为还没有找到,unity3d当前打开的文件夹路径,所以用了一个妥协的方案。就是先调用自带的方法创建一个脚本,

然后去修改里面的字符串(如果你找到了,不防提醒了一下)

  1. internal static void QuickCreateTemplate<T>() where T : ScriptTemplate, new()
  2. {
  3. EditorApplication.ExecuteMenuItem("Assets/Create/C# Script");
  4. EditorApplication.update = () =>
  5. {
  6. if (Selection.activeObject)
  7. {
  8. var path = AssetDatabase.GetAssetPath(Selection.activeObject);
  9. var temp = new T();
  10. temp.headerInfo.scriptName = Path.GetFileNameWithoutExtension(path);
  11. var dir = Path.GetDirectoryName(path);
  12. var scr = temp.CreateScript();
  13. temp.SaveToFile(dir, scr);
  14. EditorApplication.update = null;
  15. }
  16. };
  17. }
 3.窗体缓存

因为,将窗体的信息保存到本地文件,但又想要缓存一点信息,所以用了EditorPrefs和PlayerPrefs,

前面用来保存跨工程的用户信息和窗体信息,后面用来保存当前程序的信息。即使这样,因为要缓存模板抽象类的信息,

还用到了类型记录的一些方案

  1. public static TempScriptWindow GetWindow()
  2. {
  3. var window = EditorWindow.GetWindow<TempScriptWindow>();
  4. if (EditorPrefs.HasKey(prefer_key))
  5. {
  6. var json = EditorPrefs.GetString(prefer_window);
  7. JsonUtility.FromJsonOverwrite(json, window);
  8. window.LoadOldTemplates();
  9. return window;
  10. }
  11. return window;
  12. }
 
  1. internal static ScriptTemplate LoadFromJson(ScriptTemplate old)
  2. {
  3. if (!string.IsNullOrEmpty(old.type) && old.GetType().FullName != old.type)
  4. {
  5. var type = Type.GetType(old.type);
  6. if(type != null)
  7. {
  8. var temp = Activator.CreateInstance(type);
  9. JsonUtility.FromJsonOverwrite(old.json, temp);
  10. return temp as ScriptTemplate;
  11. }
  12. else
  13. {
  14. return old;
  15. }
  16. }
  17. else
  18. {
  19. old.type = old.GetType().FullName;
  20. return old;
  21. }
  22. }
 4.unity3d自带列表ReorderableList

 以前没发现这个类,所以用了其他的插件来写列表。以上你看到的列表效果就是这样写的

  1. protected void InitDetailList()
  2. {
  3. detailList = new ReorderableList(headerInfo.detailInfo, typeof(string), true, false, true, true);
  4. detailList.onAddCallback += (x) => { headerInfo.detailInfo.Add(""); };
  5. detailList.drawHeaderCallback = (x) => { EditorGUI.LabelField(x, "详细信息"); };
  6. detailList.drawElementCallback += (x, y, z, w) => { headerInfo.detailInfo[y] = EditorGUI.TextField(x, headerInfo.detailInfo[y]); };
  7. }


源码分享

 为了方便拷贝使用,我直接在一个脚本中写了这些功能。虽然用了面对象的基本思路,但实际上在个文档中写,实在是难过,以后也尽量不要这样干。主要的是这个代码后面应该还会更新,如果喜欢,或者有建议可以到我github中去找:https://github.com/zouhunter/Editor_Extends/blob/master/Core/Editor/TempScriptWindow.cs

  1. #region statement
  2. /*************************************************************************************
  3. * 作 者: zouhunter
  4. * 时 间: 2018-02-02
  5. * 详 细: 1.支持枚举、模型、结构和继承等类的模板。
  6. 2.支持快速创建通用型UI界面脚本
  7. 3.支持自定义模板类
  8. 4.自动生成作者、创建时间、描述等功能
  9. 5.支持工程同步(EditorPrefer)
  10. *************************************************************************************/
  11. #endregion
  12. using System;
  13. using UnityEngine;
  14. using UnityEditor;
  15. using System.Collections.Generic;
  16. using UnityEditorInternal;
  17. using System.CodeDom;
  18. using System.IO;
  19. using System.Text;
  20. using NUnit.Framework.Constraints;
  21. using NUnit.Framework;
  22. namespace EditorTools
  23. {
  24. #region Window
  25. /// <summary>
  26. /// 一个创建脚本模板的窗口
  27. /// </summary>
  28. public class TempScriptWindow : EditorWindow
  29. {
  30. [MenuItem("Window/TempScriptWindow")]
  31. static void Open()
  32. {
  33. var window = TempScriptHelper.GetWindow();
  34. window.wantsMouseMove = true;
  35. }
  36. [SerializeField]
  37. private List<ScriptTemplate> templates;
  38. ScriptTemplate currentTemplates { get { if (templates != null && templates.Count > currentIndex) return templates[currentIndex]; return null; } }
  39. private MonoScript script;
  40. [SerializeField]
  41. private bool isSetting;
  42. [SerializeField]
  43. private string authorName;
  44. [SerializeField]
  45. private string[] templateNames;
  46. [SerializeField]
  47. private int currentIndex;
  48. private Vector2 scrollPos;
  49. [SerializeField]
  50. private string[] templateType;
  51. private bool showRule;
  52. private string codeRule;
  53. private void OnEnable()
  54. {
  55. InitEnviroment();
  56. }
  57. private void OnDisable()
  58. {
  59. if (templates != null)
  60. {
  61. foreach (var item in templates)
  62. {
  63. item.SaveToJson();
  64. }
  65. }
  66. EditorUtility.SetDirty(this);
  67. TempScriptHelper.SaveWindow(this);
  68. }
  69. private void OnGUI()
  70. {
  71. DrawHead();
  72. if (isSetting)
  73. {
  74. //绘制设置信息
  75. DrawSettings();
  76. }
  77. else
  78. {
  79. if (templates == null)
  80. {
  81. Debug.Log("template == null");
  82. templates = new List<ScriptTemplate>();
  83. }
  84. if (templates.Count == 0)
  85. {
  86. Debug.Log("AddTemplates");
  87. AddTemplates();
  88. }
  89. currentIndex = GUILayout.Toolbar(currentIndex, templateNames);
  90. using (var scroll = new EditorGUILayout.ScrollViewScope(scrollPos))
  91. {
  92. scrollPos = scroll.scrollPosition;
  93. if (currentTemplates != null)
  94. {
  95. if (currentTemplates.GetType().FullName != currentTemplates.type)
  96. {
  97. templates[currentIndex] = TempScriptHelper.LoadFromJson(currentTemplates);
  98. }
  99. currentTemplates.OnBodyGUI();
  100. if (currentTemplates.GetType().FullName == typeof(ScriptTemplate).FullName)
  101. {
  102. if (templateType.Length > currentIndex)
  103. {
  104. var type = Type.GetType(templateType[currentIndex]);
  105. if(type != null)
  106. {
  107. templates[currentIndex] = Activator.CreateInstance(type) as ScriptTemplate;
  108. Debug.Log("create new:" + currentTemplates.GetType());
  109. }
  110. else
  111. {
  112. Debug.LogFormat("{0} missing: clear templates", currentTemplates.GetType().FullName);
  113. templates.Clear();
  114. }
  115. }
  116. else
  117. {
  118. Debug.Log("unknow err: clear templates");
  119. templates.Clear();
  120. }
  121. }
  122. }
  123. else
  124. {
  125. templates.Clear();
  126. Debug.Log("templates.Count <= currentIndex");
  127. }
  128. }
  129. if (currentTemplates != null)
  130. {
  131. currentTemplates.OnFootGUI();
  132. }
  133. }
  134. }
  135. private void InitEnviroment()
  136. {
  137. if (script == null) script = MonoScript.FromScriptableObject(this);
  138. showRule = TempScriptHelper.GetRuleShowState();
  139. if (string.IsNullOrEmpty(codeRule)) codeRule = TempScriptHelper.GetCodeRule();
  140. if (string.IsNullOrEmpty(authorName)) authorName = TempScriptHelper.GetAuthor();
  141. if (string.IsNullOrEmpty(authorName))
  142. {
  143. isSetting = true;
  144. }
  145. }
  146. private void AddTemplates()
  147. {
  148. var assemble = this.GetType().Assembly;
  149. var allTypes = assemble.GetTypes();
  150. foreach (var item in allTypes)
  151. {
  152. if (item.IsSubclassOf(typeof(ScriptTemplate)))
  153. {
  154. var template = Activator.CreateInstance(item);
  155. templates.Add(template as ScriptTemplate);
  156. }
  157. }
  158. foreach (var item in templates)
  159. {
  160. item.OnEnable();
  161. }
  162. templateNames = templates.ConvertAll<string>(x => x.Name).ToArray();
  163. templateType = templates.ConvertAll<string>(x => x.GetType().FullName).ToArray();
  164. }
  165. public void LoadOldTemplates()
  166. {
  167. for (int i = 0; i < templates.Count; i++)
  168. {
  169. if (templates[i] == null)
  170. {
  171. templates = null;
  172. return;
  173. }
  174. var newitem = TempScriptHelper.LoadFromJson(templates[i]);
  175. if (newitem == null)
  176. {
  177. templates = null;
  178. return;
  179. }
  180. templates[i] = newitem;
  181. }
  182. templateNames = templates.ConvertAll<string>(x => x.Name).ToArray();
  183. templateType = templates.ConvertAll<string>(x => x.GetType().FullName).ToArray();
  184. }
  185. private void DrawHead()
  186. {
  187. using (var hor = new EditorGUILayout.HorizontalScope())
  188. {
  189. EditorGUILayout.ObjectField(script, typeof(MonoScript), false);
  190. if (!isSetting && GUILayout.Button("setting", EditorStyles.miniButtonRight, GUILayout.Width(60)))
  191. {
  192. isSetting = true;
  193. }
  194. else if (isSetting && GUILayout.Button("confer", EditorStyles.miniButtonRight, GUILayout.Width(60)) && !string.IsNullOrEmpty(authorName))
  195. {
  196. isSetting = false;
  197. }
  198. }
  199. if (!isSetting && GUILayout.Button("Clear", EditorStyles.toolbarButton))
  200. {
  201. if (currentTemplates != null)
  202. currentTemplates.headerInfo = new TempScriptHeader() ;
  203. templates.Clear();
  204. }
  205. }
  206. private void DrawSettings()
  207. {
  208. using (var hor = new EditorGUILayout.HorizontalScope())
  209. {
  210. EditorGUI.BeginChangeCheck();
  211. EditorGUILayout.SelectableLabel("作者:", EditorStyles.miniLabel, GUILayout.Width(60));
  212. authorName = EditorGUILayout.TextField(authorName);
  213. if (EditorGUI.EndChangeCheck())
  214. {
  215. if (!string.IsNullOrEmpty(authorName))
  216. {
  217. TempScriptHelper.SaveAuthor(authorName);
  218. }
  219. }
  220. }
  221. EditorGUI.BeginChangeCheck();
  222. showRule = EditorGUILayout.ToggleLeft("在生成的代码末尾显示规范:(熟悉后可关闭此功能)", showRule);
  223. if (EditorGUI.EndChangeCheck())
  224. {
  225. TempScriptHelper.SetRuleShowState(showRule);
  226. }
  227. if (showRule)
  228. {
  229. using (var scrop = new EditorGUILayout.ScrollViewScope(scrollPos))
  230. {
  231. scrollPos = scrop.scrollPosition;
  232. EditorGUILayout.TextArea(codeRule);
  233. }
  234. }
  235. }
  236. }
  237. #endregion
  238. #region Tools
  239. /// <summary>
  240. /// 任何脚本的头
  241. /// </summary>
  242. [System.Serializable]
  243. public class TempScriptHeader
  244. {
  245. public string author;
  246. public string time;
  247. public string description;
  248. public List<string> detailInfo = new List<string>();
  249. public string scriptName;
  250. public string nameSpace;
  251. public void Update()
  252. {
  253. author = TempScriptHelper.GetAuthor();
  254. time = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  255. }
  256. }
  257. /// <summary>
  258. /// 静态工具类
  259. /// </summary>
  260. public static class TempScriptHelper
  261. {
  262. private const string prefer_key = "temp_script_autor_name";
  263. private const string prefer_window = "temp_script_window";
  264. private const string code_rule_show = "temp_script_code_rule_show";
  265. public static void SaveAuthor(string author)
  266. {
  267. EditorPrefs.SetString(prefer_key, author);
  268. }
  269. public static string GetAuthor()
  270. {
  271. return EditorPrefs.GetString(prefer_key);
  272. }
  273. public static void SaveWindow(TempScriptWindow window)
  274. {
  275. var json = JsonUtility.ToJson(window);
  276. EditorPrefs.SetString(prefer_window, json);
  277. }
  278. public static TempScriptWindow GetWindow()
  279. {
  280. var window = EditorWindow.GetWindow<TempScriptWindow>();
  281. if (EditorPrefs.HasKey(prefer_key))
  282. {
  283. var json = EditorPrefs.GetString(prefer_window);
  284. JsonUtility.FromJsonOverwrite(json, window);
  285. window.LoadOldTemplates();
  286. return window;
  287. }
  288. return window;
  289. }
  290. internal static ScriptTemplate LoadFromJson(ScriptTemplate old)
  291. {
  292. if (!string.IsNullOrEmpty(old.type) && old.GetType().FullName != old.type)
  293. {
  294. var type = Type.GetType(old.type);
  295. if(type != null)
  296. {
  297. var temp = Activator.CreateInstance(type);
  298. JsonUtility.FromJsonOverwrite(old.json, temp);
  299. return temp as ScriptTemplate;
  300. }
  301. else
  302. {
  303. return old;
  304. }
  305. }
  306. else
  307. {
  308. old.type = old.GetType().FullName;
  309. return old;
  310. }
  311. }
  312. internal static void SetRuleShowState(bool enabled)
  313. {
  314. PlayerPrefs.SetInt(code_rule_show, enabled ? 1 : 0);
  315. }
  316. internal static bool GetRuleShowState()
  317. {
  318. if (PlayerPrefs.HasKey(code_rule_show))
  319. {
  320. return PlayerPrefs.GetInt(code_rule_show) == 1;
  321. }
  322. return true;
  323. }
  324. /*
  325. 1.私有字段:_field,m_field
  326. 2.公有字段:field
  327. 2.属性:Property
  328. 3.常量:CONST
  329. 4.静态变量:Field,Property
  330. */
  331. internal static string GetCodeRule()
  332. {
  333. return @"#region 代码规范
  334. /*************************************************************************************
  335. 【变量命名】:
  336. 1.私有字段:_field,m_field
  337. 2.公有字段:field
  338. 2.属性:Property
  339. 3.常量:CONST
  340. 4.静态变量:Field,Property
  341. **************************************************************************************/
  342. #endregion
  343. ";
  344. }
  345. internal static void QuickCreateTemplate<T>() where T : ScriptTemplate, new()
  346. {
  347. EditorApplication.ExecuteMenuItem("Assets/Create/C# Script");
  348. EditorApplication.update = () =>
  349. {
  350. if (Selection.activeObject)
  351. {
  352. var path = AssetDatabase.GetAssetPath(Selection.activeObject);
  353. var temp = new T();
  354. temp.headerInfo.scriptName = Path.GetFileNameWithoutExtension(path);
  355. var dir = Path.GetDirectoryName(path);
  356. var scr = temp.CreateScript();
  357. temp.SaveToFile(dir, scr);
  358. EditorApplication.update = null;
  359. }
  360. };
  361. }
  362. }
  363. #endregion
  364. #region Templates
  365. /// <summary>
  366. /// 代码创建模板的模板
  367. /// </summary>
  368. [System.Serializable]
  369. public class ScriptTemplate
  370. {
  371. [System.Serializable]
  372. public class FieldItem
  373. {
  374. public string type;
  375. public string elementName;
  376. public string comment;
  377. }
  378. [System.Serializable]
  379. public class PropertyItem
  380. {
  381. public string type;
  382. public string elementName;
  383. public string comment;
  384. public bool get;
  385. public bool set;
  386. public PropertyItem()
  387. {
  388. get = set = true;
  389. }
  390. }
  391. [SerializeField]
  392. protected List<FieldItem> fields = new List<FieldItem>();
  393. [SerializeField]
  394. protected List<PropertyItem> propertys = new List<PropertyItem>();
  395. public string json;
  396. public string type;
  397. public TempScriptHeader headerInfo = new TempScriptHeader();
  398. public string path;
  399. public virtual string Name { get { return null; } }
  400. private ReorderableList detailList;
  401. public virtual void OnBodyGUI() { }
  402. public virtual void OnFootGUI()
  403. {
  404. if (detailList == null)
  405. {
  406. InitDetailList();
  407. }
  408. if (detailList.list != headerInfo.detailInfo)
  409. {
  410. detailList.list = headerInfo.detailInfo;
  411. }
  412. using (var horm = new EditorGUILayout.HorizontalScope())
  413. {
  414. EditorGUILayout.LabelField("Namespace", GUILayout.Width(70));
  415. headerInfo.nameSpace = EditorGUILayout.TextField(headerInfo.nameSpace, GUILayout.Width(60));
  416. EditorGUILayout.LabelField("Type", GUILayout.Width(40));
  417. headerInfo.scriptName = EditorGUILayout.TextField(headerInfo.scriptName, GUILayout.Width(60));
  418. EditorGUILayout.LabelField("简介", GUILayout.Width(40));
  419. headerInfo.description = EditorGUILayout.TextField(headerInfo.description);
  420. if (GUILayout.Button("Load", EditorStyles.miniButtonRight, GUILayout.Width(60)))
  421. {
  422. OnLoadButtonClicked();
  423. }
  424. }
  425. using (var hor = new EditorGUILayout.HorizontalScope())
  426. {
  427. using (var vertical = new EditorGUILayout.VerticalScope())
  428. {
  429. detailList.DoLayoutList();
  430. }
  431. using (var vertical = new EditorGUILayout.VerticalScope(GUILayout.Width(60)))
  432. {
  433. if (GUILayout.Button("Create", EditorStyles.miniButtonRight, GUILayout.Height(60)))
  434. {
  435. OnCreateButtonClicked();
  436. }
  437. }
  438. }
  439. }
  440. public void OnEnable()
  441. {
  442. type = this.GetType().FullName;
  443. Debug.Log(type);
  444. }
  445. public string CreateScript()
  446. {
  447. var ns = CreateNameSpace();
  448. if (ns == null) return null;
  449. var nsstr = ComplieNameSpaceToString(ns);
  450. if (string.IsNullOrEmpty(nsstr)) return null;
  451. var scriptStr = GetHeader() + nsstr + GetFooter();
  452. if (string.IsNullOrEmpty(scriptStr))
  453. {
  454. EditorUtility.DisplayDialog("生成失败", "请看日志!", "确认");
  455. return null;
  456. }
  457. return scriptStr;
  458. }
  459. public void SaveToJson()
  460. {
  461. json = null;
  462. json = JsonUtility.ToJson(this);
  463. if (string.IsNullOrEmpty(type))
  464. {
  465. type = this.GetType().FullName;
  466. }
  467. }
  468. protected void InitDetailList()
  469. {
  470. detailList = new ReorderableList(headerInfo.detailInfo, typeof(string), true, false, true, true);
  471. detailList.onAddCallback += (x) => { headerInfo.detailInfo.Add(""); };
  472. detailList.drawHeaderCallback = (x) => { EditorGUI.LabelField(x, "详细信息"); };
  473. detailList.drawElementCallback += (x, y, z, w) => { headerInfo.detailInfo[y] = EditorGUI.TextField(x, headerInfo.detailInfo[y]); };
  474. }
  475. protected void DrawFieldItem(Rect rect, FieldItem dataItem, bool haveType)
  476. {
  477. if (haveType)
  478. {
  479. var rect01 = new Rect(rect.x, rect.y, rect.width * 0.2f, EditorGUIUtility.singleLineHeight);
  480. var typeRect = new Rect(rect.x + 0.2f * rect.width, rect.y, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  481. var rect02 = new Rect(rect.x + rect.width * 0.3f, rect.y, rect.width * 0.3f, EditorGUIUtility.singleLineHeight);
  482. var commentRect = new Rect(rect.x + 0.6f * rect.width, rect.y, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  483. var rect03 = new Rect(rect.x + rect.width * 0.7f, rect.y, rect.width * 0.3f, EditorGUIUtility.singleLineHeight);
  484. dataItem.elementName = EditorGUI.TextField(rect01, dataItem.elementName);
  485. EditorGUI.LabelField(typeRect, "Type");
  486. dataItem.type = EditorGUI.TextField(rect02, dataItem.type);
  487. EditorGUI.LabelField(commentRect, "Comment");
  488. dataItem.comment = EditorGUI.TextField(rect03, dataItem.comment);
  489. }
  490. else
  491. {
  492. var left = new Rect(rect.x, rect.y, rect.width * 0.3f, EditorGUIUtility.singleLineHeight);
  493. var right = new Rect(rect.x + rect.width * 0.4f, rect.y, rect.width * 0.6f, EditorGUIUtility.singleLineHeight);
  494. var center = new Rect(rect.x + rect.width * 0.3f, rect.y, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  495. dataItem.elementName = EditorGUI.TextField(left, dataItem.elementName);
  496. EditorGUI.LabelField(center, "Comment");
  497. dataItem.comment = EditorGUI.TextField(right, dataItem.comment);
  498. }
  499. }
  500. protected void DrawPropertyItem(Rect rect, PropertyItem propertyItem)
  501. {
  502. var rect01 = new Rect(rect.x, rect.y, rect.width * 0.2f, EditorGUIUtility.singleLineHeight);
  503. var typeRect = new Rect(rect.x + 0.2f * rect.width, rect.y, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  504. var rect02 = new Rect(rect.x + rect.width * 0.3f, rect.y, rect.width * 0.3f, EditorGUIUtility.singleLineHeight);
  505. var commentRect = new Rect(rect.x + 0.6f * rect.width, rect.y, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  506. var rect03 = new Rect(rect.x + rect.width * 0.7f, rect.y, rect.width * 0.3f, EditorGUIUtility.singleLineHeight);
  507. propertyItem.elementName = EditorGUI.TextField(rect01, propertyItem.elementName);
  508. EditorGUI.LabelField(typeRect, "Type");
  509. propertyItem.type = EditorGUI.TextField(rect02, propertyItem.type);
  510. EditorGUI.LabelField(commentRect, "Comment");
  511. propertyItem.comment = EditorGUI.TextField(rect03, propertyItem.comment);
  512. var getLabelRect = new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  513. var getRect = new Rect(rect.x + 0.1f * rect.width, rect.y + EditorGUIUtility.singleLineHeight, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  514. var setLabelRect = new Rect(rect.x + 0.2f * rect.width, rect.y + EditorGUIUtility.singleLineHeight, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  515. var setRect = new Rect(rect.x + 0.3f * rect.width, rect.y + EditorGUIUtility.singleLineHeight, rect.width * 0.1f, EditorGUIUtility.singleLineHeight);
  516. EditorGUI.LabelField(getLabelRect, "get");
  517. propertyItem.get = EditorGUI.Toggle(getRect, propertyItem.get);
  518. EditorGUI.LabelField(setLabelRect, "set");
  519. propertyItem.set = EditorGUI.Toggle(setRect, propertyItem.set);
  520. }
  521. protected virtual CodeNamespace CreateNameSpace()
  522. {
  523. return null;
  524. }
  525. protected string GetHeader()
  526. {
  527. headerInfo.Update();
  528. var str1 = "#region statement\r\n" +
  529. "/************************************************************************************* \r\n" +
  530. " * 作 者: {0}\r\n" +
  531. " * 时 间: {1}\r\n" +
  532. " * 说 明: ";
  533. var str2 = "\r\n ";
  534. var str3 = "\r\n* ************************************************************************************/" +
  535. "\r\n#endregion\r\n";
  536. var headerStr = string.Format(str1, headerInfo.author, headerInfo.time);
  537. for (int i = 0; i < headerInfo.detailInfo.Count; i++)
  538. {
  539. if (i == 0)
  540. {
  541. headerStr += string.Format("{0}.{1}", i + 1, headerInfo.detailInfo[i]);
  542. }
  543. else
  544. {
  545. headerStr += string.Format("{0}{1}.{2}", str2, i + 1, headerInfo.detailInfo[i]);
  546. }
  547. }
  548. headerStr += str3;
  549. return headerStr;
  550. }
  551. protected string ComplieNameSpaceToString(CodeNamespace nameSpace)
  552. {
  553. using (Microsoft.CSharp.CSharpCodeProvider cprovider = new Microsoft.CSharp.CSharpCodeProvider())
  554. {
  555. StringBuilder fileContent = new StringBuilder();
  556. var option = new System.CodeDom.Compiler.CodeGeneratorOptions();
  557. option.BlankLinesBetweenMembers = false;
  558. using (StringWriter sw = new StringWriter(fileContent))
  559. {
  560. cprovider.GenerateCodeFromNamespace(nameSpace, sw, option);
  561. }
  562. return fileContent.ToString();
  563. }
  564. }
  565. protected string GetFooter()
  566. {
  567. if (TempScriptHelper.GetRuleShowState())
  568. {
  569. var rule = TempScriptHelper.GetCodeRule();
  570. return rule;
  571. }
  572. return null;
  573. }
  574. /// <summary>
  575. /// 点击创建
  576. /// </summary>
  577. private void OnCreateButtonClicked()
  578. {
  579. if (string.IsNullOrEmpty(headerInfo.scriptName))
  580. {
  581. EditorUtility.DisplayDialog("脚本名为空", "请填写代码名称!", "确认");
  582. return;
  583. }
  584. if (string.IsNullOrEmpty(path))
  585. {
  586. if (ProjectWindowUtil.IsFolder(Selection.activeInstanceID))
  587. {
  588. path = AssetDatabase.GetAssetPath(Selection.activeInstanceID);
  589. }
  590. else if (Selection.activeObject != null)
  591. {
  592. var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  593. if (!string.IsNullOrEmpty(assetPath))
  594. {
  595. path = assetPath.Replace(System.IO.Path.GetFileName(assetPath), "");
  596. }
  597. }
  598. }
  599. var scriptStr = CreateScript();
  600. if (!string.IsNullOrEmpty(scriptStr))
  601. {
  602. SaveToFile(path, scriptStr);
  603. }
  604. }
  605. /// <summary>
  606. /// 保存到文件
  607. /// </summary>
  608. /// <param name="path"></param>
  609. /// <param name="scriptStr"></param>
  610. public void SaveToFile(string path, string scriptStr)
  611. {
  612. if (!string.IsNullOrEmpty(path))
  613. {
  614. var scriptPath = string.Format("{0}/{1}.cs", path, headerInfo.scriptName);
  615. System.IO.File.WriteAllText(scriptPath, scriptStr, System.Text.Encoding.UTF8);
  616. AssetDatabase.Refresh();
  617. }
  618. else
  619. {
  620. EditorUtility.DisplayDialog("路径不明", "请选中文件夹后重试", "确认");
  621. }
  622. }
  623. /// <summary>
  624. /// 点击加载代码
  625. /// </summary>
  626. private void OnLoadButtonClicked()
  627. {
  628. if (!(Selection.activeObject is TextAsset))
  629. {
  630. EditorUtility.DisplayDialog("未选中", "请选中需要解析的代码后继续", "确认");
  631. return;
  632. }
  633. var path = AssetDatabase.GetAssetPath(Selection.activeObject);
  634. if (!path.EndsWith(".cs"))
  635. {
  636. EditorUtility.DisplayDialog("未选中", "请选中需要解析的代码后继续", "确认");
  637. return;
  638. }
  639. using (var provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"))
  640. {
  641. EditorUtility.DisplayDialog("未开发", ".net 3.5暂无该实现", "确认");
  642. var fileContent = System.IO.File.ReadAllText(path, Encoding.UTF8);
  643. using (StringReader sr = new StringReader(fileContent))
  644. {
  645. var nameSpaceUnit = provider.Parse(sr);
  646. Debug.Log(nameSpaceUnit);
  647. }
  648. }
  649. }
  650. }
  651. #endregion
  652. #region Enum
  653. /// <summary>
  654. /// 1.枚举类型脚本
  655. /// </summary>
  656. [Serializable]
  657. public class EnumScriptTemplate : ScriptTemplate
  658. {
  659. [MenuItem("Assets/Create/C# TempScripts/Enum", priority = 5)]
  660. static void CreateEnum()
  661. {
  662. TempScriptHelper.QuickCreateTemplate<EnumScriptTemplate>();
  663. }
  664. public override string Name
  665. {
  666. get
  667. {
  668. return "Enum";
  669. }
  670. }
  671. private ReorderableList reorderableList;
  672. public EnumScriptTemplate()
  673. {
  674. reorderableList = new ReorderableList(fields, typeof(string));
  675. reorderableList.onAddCallback += (x) => { fields.Add(new FieldItem()); };
  676. reorderableList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "枚举列表"); };
  677. reorderableList.drawElementCallback += (x, y, z, w) =>
  678. {
  679. DrawFieldItem(x, fields[y], false);
  680. };
  681. }
  682. protected override CodeNamespace CreateNameSpace()
  683. {
  684. List<CodeMemberField> fields = new List<CodeMemberField>();
  685. foreach (var item in base.fields)
  686. {
  687. CodeMemberField prop = new CodeMemberField();
  688. prop.Name = item.elementName;
  689. prop.Comments.Add(new CodeCommentStatement(item.comment));
  690. fields.Add(prop);
  691. }
  692. CodeTypeDeclaration wrapProxyClass = new CodeTypeDeclaration(headerInfo.scriptName);
  693. wrapProxyClass.TypeAttributes = System.Reflection.TypeAttributes.Public;
  694. wrapProxyClass.IsEnum = true;
  695. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  696. wrapProxyClass.Comments.Add(new CodeCommentStatement(headerInfo.description, true));
  697. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  698. foreach (var field in fields)
  699. {
  700. wrapProxyClass.Members.Add(field);
  701. }
  702. CodeNamespace nameSpace = new CodeNamespace(headerInfo.nameSpace);
  703. nameSpace.Types.Add(wrapProxyClass);
  704. return nameSpace;
  705. }
  706. public override void OnBodyGUI()
  707. {
  708. reorderableList.DoLayoutList();
  709. }
  710. }
  711. #endregion
  712. #region DataModel
  713. /// <summary>
  714. /// 2.数据模拟类
  715. /// </summary>
  716. [Serializable]
  717. public class DataModelTemplate : ScriptTemplate
  718. {
  719. [MenuItem("Assets/Create/C# TempScripts/Model", priority = 5)]
  720. static void CreateModel()
  721. {
  722. TempScriptHelper.QuickCreateTemplate<DataModelTemplate>();
  723. }
  724. public override string Name
  725. {
  726. get
  727. {
  728. return "Model";
  729. }
  730. }
  731. [SerializeField]
  732. private List<string> imports = new List<string>() {
  733. "System",
  734. "UnityEngine",
  735. "UnityEngine.UI",
  736. "System.Collections",
  737. "System.Collections.Generic",
  738. };
  739. private ReorderableList nameSpaceList;
  740. private ReorderableList reorderableList;
  741. public DataModelTemplate()
  742. {
  743. reorderableList = new ReorderableList(fields, typeof(string));
  744. reorderableList.onAddCallback += (x) => { fields.Add(new FieldItem()); };
  745. reorderableList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "模型名"); };
  746. reorderableList.drawElementCallback += (x, y, z, w) =>
  747. {
  748. DrawFieldItem(x, fields[y], true);
  749. };
  750. nameSpaceList = new ReorderableList(imports, typeof(string));
  751. nameSpaceList.onAddCallback += (x) => { imports.Add(""); };
  752. nameSpaceList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "命名空间"); };
  753. nameSpaceList.drawElementCallback += (x, y, z, w) =>
  754. {
  755. imports[y] = DrawNameSpace(x, imports[y]);
  756. };
  757. }
  758. protected override CodeNamespace CreateNameSpace()
  759. {
  760. List<CodeMemberField> fields = new List<CodeMemberField>();
  761. foreach (var item in base.fields)
  762. {
  763. CodeMemberField prop = new CodeMemberField();
  764. prop.Type = new CodeTypeReference(item.type, CodeTypeReferenceOptions.GenericTypeParameter);
  765. prop.Attributes = MemberAttributes.Public;
  766. prop.Name = item.elementName;
  767. prop.Comments.Add(new CodeCommentStatement(item.comment));
  768. fields.Add(prop);
  769. }
  770. CodeTypeDeclaration wrapProxyClass = new CodeTypeDeclaration(headerInfo.scriptName);
  771. wrapProxyClass.TypeAttributes = System.Reflection.TypeAttributes.Public;
  772. wrapProxyClass.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(System.SerializableAttribute).FullName));
  773. wrapProxyClass.IsClass = true;
  774. var destription = string.IsNullOrEmpty(headerInfo.description) ? "数据模型" : headerInfo.description;
  775. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  776. wrapProxyClass.Comments.Add(new CodeCommentStatement(destription, true));
  777. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  778. foreach (var field in fields)
  779. {
  780. wrapProxyClass.Members.Add(field);
  781. }
  782. CodeNamespace nameSpace = new CodeNamespace(headerInfo.nameSpace);
  783. nameSpace.Types.Add(wrapProxyClass);
  784. nameSpace.Imports.AddRange(imports.ConvertAll<CodeNamespaceImport>(x => new CodeNamespaceImport(x)).ToArray());
  785. return nameSpace;
  786. }
  787. private string DrawNameSpace(Rect rect, string dataItem)
  788. {
  789. var rect1 = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
  790. return EditorGUI.TextField(rect1, dataItem);
  791. }
  792. public override void OnBodyGUI()
  793. {
  794. nameSpaceList.DoLayoutList();
  795. reorderableList.DoLayoutList();
  796. }
  797. }
  798. #endregion
  799. #region Static
  800. /// <summary>
  801. /// 3.静态类
  802. /// </summary>
  803. [Serializable]
  804. public class StaticClassTemplate : ScriptTemplate
  805. {
  806. [MenuItem("Assets/Create/C# TempScripts/Static", priority = 5)]
  807. static void CreateModel()
  808. {
  809. TempScriptHelper.QuickCreateTemplate<StaticClassTemplate>();
  810. }
  811. public override string Name
  812. {
  813. get
  814. {
  815. return "Static";
  816. }
  817. }
  818. [SerializeField]
  819. private List<string> imports = new List<string>() {
  820. "System",
  821. "UnityEngine"
  822. };
  823. private ReorderableList nameSpaceList;
  824. private ReorderableList propertyList;
  825. private ReorderableList fieldList;
  826. public StaticClassTemplate()
  827. {
  828. fieldList = new ReorderableList(fields, typeof(string));
  829. fieldList.onAddCallback += (x) => { fields.Add(new FieldItem()); };
  830. fieldList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "字段"); };
  831. fieldList.drawElementCallback += (x, y, z, w) =>
  832. {
  833. DrawFieldItem(x, fields[y], true);
  834. };
  835. propertyList = new ReorderableList(propertys, typeof(string));
  836. propertyList.onAddCallback += (x) => { propertys.Add(new PropertyItem()); };
  837. propertyList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "属性"); };
  838. propertyList.elementHeightCallback = (x) => { return 2 * EditorGUIUtility.singleLineHeight; };
  839. propertyList.drawElementCallback += (x, y, z, w) =>
  840. {
  841. DrawPropertyItem(x, propertys[y]);
  842. };
  843. nameSpaceList = new ReorderableList(imports, typeof(string));
  844. nameSpaceList.onAddCallback += (x) => { imports.Add(""); };
  845. nameSpaceList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "命名空间"); };
  846. nameSpaceList.drawElementCallback += (x, y, z, w) =>
  847. {
  848. imports[y] = DrawNameSpace(x, imports[y]);
  849. };
  850. }
  851. protected override CodeNamespace CreateNameSpace()
  852. {
  853. List<CodeMemberField> codeFields = new List<CodeMemberField>();
  854. foreach (var item in fields)
  855. {
  856. CodeMemberField field = new CodeMemberField();
  857. field.Type = new CodeTypeReference(item.type, CodeTypeReferenceOptions.GenericTypeParameter);
  858. field.Attributes = MemberAttributes.Public | MemberAttributes.Static;
  859. field.Name = item.elementName;
  860. //field.InitExpression = invokeExpression;
  861. field.Comments.Add(new CodeCommentStatement(item.comment));
  862. codeFields.Add(field);
  863. }
  864. List<CodeMemberProperty> propertysMemper = new List<CodeMemberProperty>();
  865. foreach (var item in propertys)
  866. {
  867. CodeMemberProperty prop = new CodeMemberProperty();
  868. prop.Type = new CodeTypeReference(item.type, CodeTypeReferenceOptions.GenericTypeParameter);
  869. prop.Attributes = MemberAttributes.Public|MemberAttributes.Static;
  870. prop.Name = item.elementName;
  871. prop.HasGet = item.get;
  872. prop.HasSet = item.set;
  873. //CodeExpression invokeExpression = new CodePropertyReferenceExpression();
  874. prop.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(null)));
  875. prop.Comments.Add(new CodeCommentStatement(item.comment));
  876. propertysMemper.Add(prop);
  877. }
  878. CodeTypeDeclaration wrapProxyClass = new CodeTypeDeclaration(headerInfo.scriptName);
  879. wrapProxyClass.TypeAttributes = System.Reflection.TypeAttributes.Public;//没有静态?
  880. wrapProxyClass.Attributes = MemberAttributes.Static;
  881. wrapProxyClass.IsClass = true;
  882. var destription = string.IsNullOrEmpty(headerInfo.description) ? "静态类" : headerInfo.description;
  883. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  884. wrapProxyClass.Comments.Add(new CodeCommentStatement(destription, true));
  885. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  886. foreach (var prop in propertysMemper)
  887. {
  888. wrapProxyClass.Members.Add(prop);
  889. }
  890. foreach (var field in codeFields)
  891. {
  892. wrapProxyClass.Members.Add(field);
  893. }
  894. CodeNamespace nameSpace = new CodeNamespace(headerInfo.nameSpace);
  895. nameSpace.Types.Add(wrapProxyClass);
  896. nameSpace.Imports.AddRange(imports.ConvertAll<CodeNamespaceImport>(x => new CodeNamespaceImport(x)).ToArray());
  897. return nameSpace;
  898. }
  899. private string DrawNameSpace(Rect rect, string dataItem)
  900. {
  901. var rect1 = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
  902. return EditorGUI.TextField(rect1, dataItem);
  903. }
  904. public override void OnBodyGUI()
  905. {
  906. nameSpaceList.DoLayoutList();
  907. fieldList.DoLayoutList();
  908. propertyList.DoLayoutList();
  909. }
  910. }
  911. #endregion
  912. #region Struct
  913. /// <summary>
  914. /// 5.结构体模板
  915. /// </summary>
  916. [Serializable]
  917. public class StructTempate : ScriptTemplate
  918. {
  919. [MenuItem("Assets/Create/C# TempScripts/Struct", priority = 5)]
  920. static void CreateStruct()
  921. {
  922. TempScriptHelper.QuickCreateTemplate<StructTempate>();
  923. }
  924. public override string Name
  925. {
  926. get
  927. {
  928. return "Struct";
  929. }
  930. }
  931. [SerializeField]
  932. private List<string> imports = new List<string>() {
  933. "System",
  934. "UnityEngine",
  935. "UnityEngine.UI",
  936. "System.Collections",
  937. "System.Collections.Generic",
  938. };
  939. private ReorderableList nameSpaceList;
  940. private ReorderableList reorderableList;
  941. public StructTempate()
  942. {
  943. reorderableList = new ReorderableList(fields, typeof(string));
  944. reorderableList.onAddCallback += (x) => { fields.Add(new FieldItem()); };
  945. reorderableList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "模型名"); };
  946. reorderableList.drawElementCallback += (x, y, z, w) =>
  947. {
  948. DrawFieldItem(x, fields[y], true);
  949. };
  950. nameSpaceList = new ReorderableList(imports, typeof(string));
  951. nameSpaceList.onAddCallback += (x) => { imports.Add(""); };
  952. nameSpaceList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "命名空间"); };
  953. nameSpaceList.drawElementCallback += (x, y, z, w) =>
  954. {
  955. imports[y] = DrawNameSpace(x, imports[y]);
  956. };
  957. }
  958. protected override CodeNamespace CreateNameSpace()
  959. {
  960. List<CodeMemberField> fields = new List<CodeMemberField>();
  961. foreach (var item in base.fields)
  962. {
  963. CodeMemberField prop = new CodeMemberField();
  964. prop.Type = new CodeTypeReference(item.type, CodeTypeReferenceOptions.GenericTypeParameter);
  965. prop.Attributes = MemberAttributes.Public;
  966. prop.Name = item.elementName;
  967. prop.Comments.Add(new CodeCommentStatement(item.comment));
  968. fields.Add(prop);
  969. }
  970. CodeTypeDeclaration wrapProxyClass = new CodeTypeDeclaration(headerInfo.scriptName);
  971. wrapProxyClass.TypeAttributes = System.Reflection.TypeAttributes.Public;
  972. wrapProxyClass.IsStruct = true;
  973. var destription = string.IsNullOrEmpty(headerInfo.description) ? "结构体" : headerInfo.description;
  974. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  975. wrapProxyClass.Comments.Add(new CodeCommentStatement(destription, true));
  976. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  977. foreach (var field in fields)
  978. {
  979. wrapProxyClass.Members.Add(field);
  980. }
  981. CodeNamespace nameSpace = new CodeNamespace(headerInfo.nameSpace);
  982. nameSpace.Types.Add(wrapProxyClass);
  983. nameSpace.Imports.AddRange(imports.ConvertAll<CodeNamespaceImport>(x => new CodeNamespaceImport(x)).ToArray());
  984. return nameSpace;
  985. }
  986. private string DrawNameSpace(Rect rect, string dataItem)
  987. {
  988. var rect1 = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
  989. return EditorGUI.TextField(rect1, dataItem);
  990. }
  991. public override void OnBodyGUI()
  992. {
  993. nameSpaceList.DoLayoutList();
  994. reorderableList.DoLayoutList();
  995. }
  996. }
  997. #endregion
  998. #region Interface
  999. /// <summary>
  1000. /// 6.接口创建模板
  1001. /// </summary>
  1002. [Serializable]
  1003. public class InterfaceTempate : ScriptTemplate
  1004. {
  1005. [MenuItem("Assets/Create/C# TempScripts/Interface", priority = 5)]
  1006. static void CreateEnum()
  1007. {
  1008. TempScriptHelper.QuickCreateTemplate<InterfaceTempate>();
  1009. }
  1010. public override string Name
  1011. {
  1012. get
  1013. {
  1014. return "Interface";
  1015. }
  1016. }
  1017. [SerializeField]
  1018. private List<string> imports = new List<string>() {
  1019. "System",
  1020. "UnityEngine"
  1021. };
  1022. private ReorderableList nameSpaceList;
  1023. private ReorderableList reorderableList;
  1024. public InterfaceTempate()
  1025. {
  1026. reorderableList = new ReorderableList(propertys, typeof(string));
  1027. reorderableList.onAddCallback += (x) => { propertys.Add(new PropertyItem()); };
  1028. reorderableList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "属性"); };
  1029. reorderableList.elementHeightCallback = (x) => { return 2 * EditorGUIUtility.singleLineHeight; };
  1030. reorderableList.drawElementCallback += (x, y, z, w) =>
  1031. {
  1032. DrawPropertyItem(x, propertys[y]);
  1033. };
  1034. nameSpaceList = new ReorderableList(imports, typeof(string));
  1035. nameSpaceList.onAddCallback += (x) => { imports.Add(""); };
  1036. nameSpaceList.drawHeaderCallback += (x) => { EditorGUI.LabelField(x, "命名空间"); };
  1037. nameSpaceList.drawElementCallback += (x, y, z, w) =>
  1038. {
  1039. imports[y] = DrawNameSpace(x, imports[y]);
  1040. };
  1041. }
  1042. protected override CodeNamespace CreateNameSpace()
  1043. {
  1044. List<CodeMemberProperty> propertysMemper = new List<CodeMemberProperty>();
  1045. foreach (var item in propertys)
  1046. {
  1047. CodeMemberProperty prop = new CodeMemberProperty();
  1048. prop.Type = new CodeTypeReference(item.type, CodeTypeReferenceOptions.GenericTypeParameter);
  1049. prop.Attributes = MemberAttributes.Public;
  1050. prop.Name = item.elementName;
  1051. prop.HasGet = item.get;
  1052. prop.HasSet = item.set;
  1053. prop.Comments.Add(new CodeCommentStatement(item.comment));
  1054. propertysMemper.Add(prop);
  1055. }
  1056. CodeTypeDeclaration wrapProxyClass = new CodeTypeDeclaration(headerInfo.scriptName);
  1057. wrapProxyClass.TypeAttributes = System.Reflection.TypeAttributes.Public;
  1058. wrapProxyClass.IsInterface = true;
  1059. var destription = string.IsNullOrEmpty(headerInfo.description) ? "接口" : headerInfo.description;
  1060. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  1061. wrapProxyClass.Comments.Add(new CodeCommentStatement(destription, true));
  1062. wrapProxyClass.Comments.Add(new CodeCommentStatement("<summary>", true));
  1063. foreach (var prop in propertysMemper)
  1064. {
  1065. wrapProxyClass.Members.Add(prop);
  1066. }
  1067. CodeNamespace nameSpace = new CodeNamespace(headerInfo.nameSpace);
  1068. nameSpace.Types.Add(wrapProxyClass);
  1069. nameSpace.Imports.AddRange(imports.ConvertAll<CodeNamespaceImport>(x => new CodeNamespaceImport(x)).ToArray());
  1070. return nameSpace;
  1071. }
  1072. private string DrawNameSpace(Rect rect, string dataItem)
  1073. {
  1074. var rect1 = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
  1075. return EditorGUI.TextField(rect1, dataItem);
  1076. }
  1077. public override void OnBodyGUI()
  1078. {
  1079. nameSpaceList.DoLayoutList();
  1080. reorderableList.DoLayoutList();
  1081. }
  1082. }
  1083. #endregion
  1084. ///下面可以自定义你的代码生成模板
  1085. ///...
  1086. /// <summary>
  1087. /// UI模板
  1088. /// </summary>
  1089. //[Serializable]
  1090. //public class UIPanelTempate : ScriptTemplate
  1091. //{
  1092. // public override string Name
  1093. // {
  1094. // get
  1095. // {
  1096. // return "UIPanel";
  1097. // }
  1098. // }
  1099. // protected override CodeNamespace CreateNameSpace()
  1100. // {
  1101. // return null;
  1102. // }
  1103. // public override void OnBodyGUI()
  1104. // {
  1105. // }
  1106. //}
  1107. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/81968
推荐阅读
相关标签
  

闽ICP备14008679号