├── AutoGenerateCode.cs
└── README.md
/AutoGenerateCode.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------
2 | // This file write for Game Framework v3.x
3 | // Which Copyright © 2013-2018 Jiang Yin. All rights reserved.
4 | // Homepage: http://gameframework.cn/
5 | // Feedback: mailto:jiangyin@gameframework.cn
6 | // The code write by Ron Tang.
7 | //------------------------------------------------------------
8 |
9 | using UnityEditor;
10 | using UnityEngine;
11 | using System.IO;
12 | using System.Collections.Generic;
13 |
14 |
15 | namespace UnityGameFramework.Editor
16 | {
17 | ///
18 | /// 生成数据表行代码
19 | ///
20 | internal static class AutoGenerateCode
21 | {
22 | private static string tablePath = "\\GameMain\\DataTables\\";
23 | private static string codePath = "\\GameMain\\Scripts\\DataTable\\";
24 | private static string codeSpace = "StarForce";
25 | [MenuItem("Game Framework/AutoGenerateCode", false, 100)]
26 | private static void HandleAllDataTables()
27 | {
28 |
29 | //设置进度条
30 | //EditorUtility.DisplayProgressBar("设置AssetName名称", "正在设置AssetName名称中...", 0.50f);
31 | //EditorUtility.ClearProgressBar();
32 |
33 | //路径
34 | string fullPath = Application.dataPath + tablePath ;
35 |
36 | //获取指定路径下面的所有资源文件
37 | if (Directory.Exists(fullPath))
38 | {
39 | DirectoryInfo direction = new DirectoryInfo(fullPath);
40 | FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
41 |
42 | Debug.Log(files.Length);
43 |
44 | for (int i = 0; i < files.Length; i++)
45 | {
46 | if (files[i].Name.EndsWith(".meta"))
47 | {
48 | continue;
49 | }
50 | //Debug.Log("Name:" + files[i].Name);
51 | //Debug.Log( "FullName:" + files[i].FullName );
52 | //Debug.Log( "DirectoryName:" + files[i].DirectoryName );
53 | LoadFile(files[i].FullName);
54 | }
55 | }
56 | }
57 |
58 |
59 | static void LoadFile(string filePath)
60 | {
61 | using (StreamReader sr = File.OpenText(filePath))
62 | {
63 | string line;
64 | List lines = new List();
65 | int lineCount = 0;
66 | while ((line = sr.ReadLine()) != null && lineCount < 3)
67 | {
68 | //Debug.Log(line);
69 | lines.Add(line);
70 | lineCount++;
71 | }
72 | sr.Close();
73 | sr.Dispose();
74 | HandleData(lines);
75 | }
76 |
77 | }
78 | static void HandleData(List info)
79 | {
80 | string[] textOfTableNames = info[0].Split('\t');
81 | string textOfTableName = textOfTableNames[1];
82 |
83 | string[] textOfPropertyNames = info[1].Split('\t');
84 |
85 | string[] textOfPropertyTypeNames= info[2].Split('\t');
86 |
87 |
88 | StreamWriter sw;
89 | FileInfo t = new FileInfo(Application.dataPath+ codePath + textOfTableName + ".cs");
90 | sw = t.CreateText();
91 |
92 | WriteHeader(sw);
93 | WriteNameSpcace(sw, codeSpace);
94 | sw.WriteLine("{");
95 | sw.WriteLine(string.Format("public class {0} : IDataRow", textOfTableName));
96 | sw.WriteLine("{");
97 | WriteAllProperty(sw, textOfPropertyNames, textOfPropertyTypeNames);
98 | WriteParseDataRow(sw, textOfPropertyNames,textOfPropertyTypeNames);
99 | WriteAvoidJIT(sw, textOfTableName);
100 | sw.WriteLine("}");
101 | sw.WriteLine("}");
102 | sw.Flush();
103 | sw.Close();
104 | sw.Dispose();
105 |
106 | }
107 |
108 | static void WriteNameSpcace(StreamWriter sw, string name)
109 | {
110 | sw.WriteLine("namespace "+ name);
111 | }
112 |
113 | static void WriteAllProperty(StreamWriter sw, string[] textOfPropertyNames, string[] textOfPropertyTypeNames)
114 | {
115 | for (int i = 1; i < textOfPropertyNames.Length; i++)
116 | {
117 | if (string.IsNullOrEmpty(textOfPropertyTypeNames[i]))
118 | continue;
119 | if (string.IsNullOrEmpty(textOfPropertyNames[i]))
120 | continue;
121 | WriteProperty(sw, textOfPropertyTypeNames[i], textOfPropertyNames[i]);
122 |
123 | }
124 | }
125 |
126 | static void WriteHeader(StreamWriter sw)
127 | {
128 | sw.WriteLine("using GameFramework.DataTable;") ;
129 | sw.WriteLine("using System.Collections.Generic;");
130 | }
131 |
132 |
133 |
134 | static void WriteProperty(StreamWriter sw, string type, string name)
135 | {
136 | sw.WriteLine(" public"+" "+type+" "+ name);
137 | sw.WriteLine(" {");
138 | sw.WriteLine(" get;");
139 | sw.WriteLine(" protected set;");
140 | sw.WriteLine(" }");
141 | sw.WriteLine("");
142 | }
143 |
144 | static void WriteAvoidJIT(StreamWriter sw,string classTypeName)
145 | {
146 | sw.WriteLine(" private void AvoidJIT()");
147 | sw.WriteLine(" {");
148 | sw.WriteLine(" "+string.Format("new Dictionary ();", classTypeName));
149 | sw.WriteLine(" }");
150 |
151 | }
152 |
153 | static void WriteParseDataRow(StreamWriter sw,string[] names,string[] types)
154 | {
155 | sw.WriteLine(" public void ParseDataRow(string dataRowText)");
156 | sw.WriteLine(" {");
157 | sw.WriteLine(" string[] text = dataRowText.Split('\\t');");
158 | sw.WriteLine(" int index = 0;");
159 | sw.WriteLine(" index++;");
160 | sw.WriteLine(string.Format(" {0} = {1}.Parse(text[index++]);",names[1],types[1]));
161 | sw.WriteLine(" index++;");
162 | for (int i = 2; i < names.Length; i++)
163 | {
164 | if(string.IsNullOrEmpty(names[i])) continue;
165 |
166 | if(types[i]!="string")
167 | sw.WriteLine(string.Format(" {0} = {1}.Parse(text[index++]);", names[i],types[i]));
168 | else
169 | sw.WriteLine(string.Format(" {0} = text[index++];", names[i]));
170 | }
171 |
172 | sw.WriteLine(" }");
173 |
174 | }
175 |
176 |
177 | }
178 | }
179 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AutoGenerateDataTableCodeForGameframework
2 | 这是一段Editor代码,其用以生成Gameframework数据表对应的操作代码
3 |
--------------------------------------------------------------------------------