GenerateLuaTemple.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. using LuaInterface;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnityEditor;
  9. using UnityEngine;
  10. public struct UICfgData
  11. {
  12. public int id;
  13. public string name;
  14. public string res_path;
  15. }
  16. public class GenerateLuaTemple
  17. {
  18. static Dictionary<GameObject, Dictionary<string, string>> uiNodes =
  19. new Dictionary<GameObject, Dictionary<string, string>>();
  20. static Dictionary<string, bool> Generated = new Dictionary<string, bool>();
  21. static Dictionary<string, GenerateData> needGenerate = new Dictionary<string, GenerateData>();
  22. static string currPrefabPath = string.Empty;
  23. private const string c_UICfgPath = "Assets/Lua/Config/UICfg.lua";
  24. static Dictionary<int, UICfgData> UIConfigData = new Dictionary<int, UICfgData>();
  25. private static List<string> nullStr = new List<string>();
  26. private static List<string> nullPeerStr = new List<string>();
  27. public static readonly string UIPrefabDirPath = "Assets/Content/Prefabs/UI/";
  28. const string LuaViewDirPath = "Assets/Lua/UI/{0}.lua";
  29. const string LuaGridViewDir = "GridViewItem/";
  30. const string TextMeshProString = "textMeshProUGUI";
  31. const string TextString = "text";
  32. static Dictionary<string, string> ComponentTypes = new Dictionary<string, string>
  33. {
  34. //{ typeof(RectTransform).FullName, "xxxx" },
  35. };
  36. public static Dictionary<string, List<string>> functions = new Dictionary<string, List<string>>
  37. {
  38. };
  39. struct Class
  40. {
  41. public string name;
  42. public string super;
  43. public Dictionary<string, ClassFiled> fileds;
  44. }
  45. struct ClassFiled
  46. {
  47. public ClassFiled(string name, string type)
  48. {
  49. this.name = name;
  50. this.type = type;
  51. }
  52. public string name;
  53. public string type;
  54. }
  55. struct GenerateData
  56. {
  57. public GameObject gameObject;
  58. public string className;
  59. public string filePath;
  60. public string luaPath;
  61. public string uitype;
  62. public bool bTouchCLose;
  63. public string id;
  64. public int type;
  65. }
  66. [MenuItem("Assets/GenerateLuaClass(生成UI代码)")]
  67. [MenuItem("Tools/GenerateLuaClass")]
  68. public static void GenerateLua()
  69. {
  70. List<GameObject> objs = new List<GameObject>();
  71. GameObject[] gameObjects = Selection.gameObjects;
  72. Transform[] activeTransforms = Selection.transforms;
  73. foreach (GameObject gameObject in gameObjects)
  74. {
  75. bool bPrfab = true;
  76. foreach (Transform activeTransform in activeTransforms)
  77. {
  78. if (activeTransform == gameObject.transform)
  79. {
  80. bPrfab = false;
  81. break;
  82. }
  83. }
  84. if (bPrfab)
  85. {
  86. objs.Add(gameObject);
  87. }
  88. }
  89. Generate(objs);
  90. //TransPanelTool.TransPanel();
  91. }
  92. //[MenuItem("Assets/GenerateAllUILuaClass(生成所有UI代码)")]
  93. [MenuItem("Tools/GenerateAllUILuaClass(生成所有UI代码)")]
  94. public static void GenerateAllUILua()
  95. {
  96. string[] allPath = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/Content/Prefabs/UI" });
  97. Debug.Log(allPath.Length);
  98. List<GameObject> objs = new List<GameObject>();
  99. for (int i = 0; i < allPath.Length; i++)
  100. {
  101. string path = AssetDatabase.GUIDToAssetPath(allPath[i]);
  102. var obj = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
  103. if (obj != null)
  104. {
  105. objs.Add(obj);
  106. }
  107. }
  108. Generate(objs);
  109. //TransPanelTool.TransPanel();
  110. }
  111. public static void Generate(List<GameObject> objs)
  112. {
  113. GenerateCode(objs);
  114. AssetDatabase.Refresh();
  115. }
  116. private static bool CheckDepObjs(GameObject obj)
  117. {
  118. Func<Transform, string> getPathName = (o) =>
  119. {
  120. string path = o.name;
  121. Transform parent = o.parent;
  122. while (parent != null)
  123. {
  124. path = parent.name + "/" + path;
  125. parent = parent.parent;
  126. }
  127. return path;
  128. };
  129. UINode[] nodes = obj.GetComponentsInChildren<UINode>();
  130. foreach (var node in nodes)
  131. {
  132. if (node.depObjs == null)
  133. {
  134. continue;
  135. }
  136. foreach (var depObj in node.depObjs)
  137. {
  138. if (depObj)
  139. {
  140. if (depObj.gameObject == node.gameObject)
  141. {
  142. Debug.LogError("uinode 不能 依赖 自身!!!!! " + getPathName(node.transform));
  143. return false;
  144. }
  145. Transform parent = node.transform.parent;
  146. bool bFind = false;
  147. while (parent != null)
  148. {
  149. UINode uiNode = parent.GetComponent<UINode>();
  150. if (uiNode == depObj)
  151. {
  152. bFind = true;
  153. break;
  154. }
  155. parent = parent.parent;
  156. }
  157. if (!bFind)
  158. {
  159. parent = node.transform.parent;
  160. while (parent != null)
  161. {
  162. parent = parent.parent;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. private static void GenerateCode(List<GameObject> objs)
  171. {
  172. foreach (var obj in objs)
  173. {
  174. if (!CheckDepObjs(obj))
  175. return;
  176. }
  177. Generated.Clear();
  178. needGenerate.Clear();
  179. UIConfigData.Clear();
  180. foreach (GameObject gameObject in objs)
  181. {
  182. string assetPath = AssetDatabase.GetAssetPath(gameObject);
  183. UIGridViewMark mark = gameObject.GetComponent<UIGridViewMark>();
  184. GenerateData data = new GenerateData();
  185. string filePath = "";
  186. string className = "";
  187. string luaPath = "";
  188. if (mark)
  189. {
  190. className = gameObject.name;
  191. filePath = string.Format(LuaViewDirPath, LuaGridViewDir + className);
  192. data.type = 1;
  193. }
  194. else
  195. {
  196. UICfgData? config = GetUIConfig(gameObject.name);
  197. if (config == null)
  198. {
  199. DebugHelper.LogError("[GenerateLua error] {0} {1} {2}", "UIConfig", gameObject.name, "isnt exist");
  200. continue;
  201. }
  202. UICfgData data1 = config.Value;
  203. className = data1.name;
  204. luaPath = data1.res_path;
  205. filePath = string.Format(LuaViewDirPath, luaPath + "View");
  206. data.type = 0;
  207. }
  208. data.gameObject = gameObject;
  209. data.className = className;
  210. data.filePath = filePath;
  211. data.luaPath = luaPath;
  212. if (!needGenerate.ContainsKey(className))
  213. {
  214. needGenerate[className] = data;
  215. }
  216. }
  217. while (needGenerate.Count > 0)
  218. {
  219. foreach (string key in needGenerate.Keys)
  220. {
  221. uiNodes.Clear();
  222. GenerateData data = needGenerate[key];
  223. string filePath = data.filePath;
  224. string uitype = data.uitype;
  225. string className = data.className;
  226. string luaPath = data.luaPath;
  227. GameObject gameObject = data.gameObject;
  228. string dirPath = filePath.Substring(0, filePath.LastIndexOf("/") + 1);
  229. if (!Directory.Exists(dirPath))
  230. {
  231. Directory.CreateDirectory(dirPath);
  232. }
  233. if (data.type == 0)
  234. {
  235. //UI类型
  236. GenerateView(gameObject, className, filePath, luaPath);
  237. string generatePath = filePath.Replace(".lua", "_Generate.lua");
  238. GenerateMap(gameObject, className, generatePath, luaPath, data.type);
  239. string ctrPath = filePath.Replace("View", "Ctr");
  240. GenerateCtr(gameObject, className, ctrPath, luaPath);
  241. }
  242. else
  243. {
  244. //GridItem类型
  245. string generatePath = filePath.Replace(".lua", "_Generate.lua");
  246. GenerateMap(gameObject, className, generatePath, luaPath, data.type);
  247. }
  248. Generated[key] = true;
  249. needGenerate.Remove(key);
  250. break;
  251. }
  252. }
  253. Debug.Log("Generate lua over ");
  254. }
  255. private static void Generate_Field(StringBuilder sb_logic, string className)
  256. {
  257. sb_logic.Append(string.Format("---@class {0}\r\n", className));
  258. sb_logic.Append(string.Format("---##################### 【{0} Generate Field】 Start #####################\r\n", className));
  259. sb_logic.Append(string.Format("---自动生成代码变量声明\r\n"));
  260. sb_logic.Append(string.Format("---%%%%%%%%%%%%%%%%%%%%% 【{0} Generate Field】 End %%%%%%%%%%%%%%%%%%%%%\r\n", className));
  261. sb_logic.Append(string.Format("---##################### 【{0} Custom Field】 Start #####################\r\n", className));
  262. sb_logic.Append(string.Format("---TODO 自定义变量声明在这里: ---@field [public|protected|private] field_name FIELD_TYPE[|OTHER_TYPE]\r\n"));
  263. sb_logic.Append(string.Format("---%%%%%%%%%%%%%%%%%%%%% 【{0} Custom Field】 End %%%%%%%%%%%%%%%%%%%%%\r\n", className));
  264. }
  265. private static void GenerateView(GameObject obj, string className, string path, string luaPath)
  266. {
  267. if (File.Exists(path)) return;
  268. StringBuilder sb_logic = new StringBuilder();
  269. int index = luaPath.IndexOf("/");
  270. string dir = luaPath.Substring(0, index);
  271. string viewName = className + "View";
  272. string ctrName = dir + "/" + className + "Ctr";
  273. string generateName = dir + "/" + viewName + "_Generate";
  274. sb_logic.Append(string.Format("local {0} = require(\"" + generateName + "\")\r\n", viewName, viewName));
  275. sb_logic.Append("\r\n");
  276. sb_logic.Append(string.Format("function {0}:OnAwake(data)\r\n", viewName));
  277. sb_logic.Append("\tself.controller = require(\"" + ctrName + "\"):new()\r\n");
  278. sb_logic.Append("\tself.controller:Init(self)\r\n");
  279. sb_logic.Append("\tself.controller:SetData(data)\r\n");
  280. sb_logic.Append("end\r\n");
  281. sb_logic.Append("\r\n");
  282. sb_logic.Append(string.Format("function {0}:AddEventListener()\r\n", viewName));
  283. sb_logic.Append("\tManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)\r\n");
  284. sb_logic.Append("\r\n");
  285. sb_logic.Append("end\r\n");
  286. sb_logic.Append("\r\n");
  287. sb_logic.Append(string.Format("function {0}:FillContent(data, uiBase)\r\n", viewName));
  288. sb_logic.Append("\tself.uiBase = uiBase\r\n");
  289. sb_logic.Append("\tlocal gameObject = self.uiBase:GetRoot()\r\n");
  290. sb_logic.Append("\tif gameObject ~= nil then\r\n");
  291. sb_logic.Append("\t\tself.gameObject = gameObject\r\n");
  292. sb_logic.Append("\t\tself.transform = gameObject.transform\r\n");
  293. sb_logic.Append("\tend\r\n");
  294. sb_logic.Append("\tself:InitGenerate(self.transform, data)\r\n");
  295. sb_logic.Append("\r\n");
  296. sb_logic.Append("\tself:Init()\r\n");
  297. sb_logic.Append("end\r\n");
  298. sb_logic.Append("\r\n");
  299. sb_logic.Append(string.Format("function {0}:Init()\r\n", viewName));
  300. sb_logic.Append("end\r\n");
  301. sb_logic.Append("\r\n");
  302. sb_logic.Append(string.Format("function {0}:RemoveEventListener()\r\n", viewName));
  303. sb_logic.Append("\tManagerContainer.LuaEventMgr:Unregister(self.uiData.name)\r\n");
  304. sb_logic.Append("\r\n");
  305. sb_logic.Append("end\r\n");
  306. sb_logic.Append("\r\n");
  307. sb_logic.Append(string.Format("function {0}:AddUIEventListener()\r\n", viewName));
  308. sb_logic.Append("\r\n");
  309. sb_logic.Append("end\r\n");
  310. sb_logic.Append("\r\n");
  311. sb_logic.Append(string.Format("function {0}:OnHide()\r\n", viewName));
  312. sb_logic.Append("\r\n");
  313. sb_logic.Append("end\r\n");
  314. sb_logic.Append("\r\n");
  315. sb_logic.Append(string.Format("function {0}:OnShow(data)\r\n", viewName));
  316. sb_logic.Append("\tself.controller:SetData(data)\r\n");
  317. sb_logic.Append("\r\n");
  318. sb_logic.Append("end\r\n");
  319. sb_logic.Append("\r\n");
  320. sb_logic.Append(string.Format("function {0}:OnClose()\r\n", viewName));
  321. sb_logic.Append("end\r\n");
  322. sb_logic.Append("\r\n");
  323. sb_logic.Append(string.Format("function {0}:OnDispose()\r\n", viewName));
  324. sb_logic.Append("\tself.controller:OnDispose()\r\n");
  325. sb_logic.Append("end\r\n");
  326. sb_logic.Append("\r\n");
  327. sb_logic.Append(string.Format("return {0}\r\n", viewName));
  328. sb_logic.Append("\r\n");
  329. using (StreamWriter textWriter = new StreamWriter(path, false, new UTF8Encoding(false)))
  330. {
  331. textWriter.Write(sb_logic.ToString());
  332. textWriter.Flush();
  333. textWriter.Close();
  334. }
  335. sb_logic.Clear();
  336. sb_logic = null;
  337. //string baseClassName = "UIBase";
  338. //GenerateContent(obj, className, path, generatePath, sb_logic, baseClassName);
  339. }
  340. private static void GenerateCtr(GameObject obj, string className, string path, string luaPath)
  341. {
  342. if (File.Exists(path)) return;
  343. StringBuilder sb_logic = new StringBuilder();
  344. string ctrName = className + "Ctr";
  345. string baseClassName = "UICtrBase";
  346. sb_logic.Append(string.Format("local {0} = class(\"{1}\", require(\"{2}\"))\r\n", ctrName, ctrName, baseClassName));
  347. sb_logic.Append("\r\n");
  348. sb_logic.Append(string.Format("function {0}:Init(view)\r\n", ctrName));
  349. sb_logic.Append("\tself.view = view\r\n");
  350. sb_logic.Append("end\r\n");
  351. sb_logic.Append("\r\n");
  352. sb_logic.Append(string.Format("function {0}:SetData(data)\r\n", ctrName));
  353. sb_logic.Append("\tself.asyncIdx = 0\r\n");
  354. sb_logic.Append("\tif data == nil then return end\r\n");
  355. sb_logic.Append("\tself.data = data\r\n");
  356. sb_logic.Append("end\r\n");
  357. sb_logic.Append("\r\n");
  358. sb_logic.Append(string.Format("function {0}:GetAsyncIdx()\r\n", ctrName));
  359. sb_logic.Append("\tself.asyncIdx = self.asyncIdx + 1\r\n");
  360. sb_logic.Append("\treturn self.asyncIdx\r\n");
  361. sb_logic.Append("end\r\n");
  362. sb_logic.Append("\r\n");
  363. sb_logic.Append(string.Format("function {0}:GetData()\r\n", ctrName));
  364. sb_logic.Append("\treturn self.data\r\n");
  365. sb_logic.Append("end\r\n");
  366. sb_logic.Append("\r\n");
  367. sb_logic.Append(string.Format("function {0}:OnDispose()\r\n", ctrName));
  368. sb_logic.Append("\tself.data = nil\r\n");
  369. sb_logic.Append("\tself.view = nil\r\n");
  370. sb_logic.Append("end\r\n");
  371. sb_logic.Append("\r\n");
  372. sb_logic.Append(string.Format("return {0}\r\n", ctrName));
  373. sb_logic.Append("\r\n");
  374. using (StreamWriter textWriter = new StreamWriter(path, false, new UTF8Encoding(false)))
  375. {
  376. textWriter.Write(sb_logic.ToString());
  377. textWriter.Flush();
  378. textWriter.Close();
  379. }
  380. sb_logic.Clear();
  381. sb_logic = null;
  382. //string baseClassName = "UIBase";
  383. //GenerateContent(obj, className, path, generatePath, sb_logic, baseClassName);
  384. }
  385. private static Class CreateNewClass(ref Dictionary<string, Class> classes, string cls, string var)
  386. {
  387. string clsName = cls + "__Generate";
  388. if (var.Length > 0)
  389. {
  390. if (var.IndexOf(".") > 0)
  391. {
  392. var var_ = var.Replace(".", "_");
  393. if (!var_.StartsWith("_"))
  394. {
  395. var_ = "_" + var_;
  396. }
  397. clsName = cls + "__Generate" + var_;
  398. }
  399. else
  400. {
  401. clsName = cls + "__Generate" + "_" + var;
  402. }
  403. }
  404. if (!classes.ContainsKey(clsName))
  405. {
  406. Class newClass = new Class();
  407. newClass.name = clsName;
  408. if (clsName == cls + "__Generate")
  409. newClass.super = string.Empty;
  410. else
  411. newClass.super = typeof(GameObject).FullName;
  412. newClass.fileds = new Dictionary<string, ClassFiled>();
  413. classes[clsName] = newClass;
  414. }
  415. if (var.Length > 0)
  416. {
  417. string var2 = string.Empty;
  418. string filedName = var;
  419. int indexOf = var.LastIndexOf(".");
  420. if (indexOf > 0)
  421. {
  422. var2 = var.Substring(0, indexOf);
  423. filedName = var.Substring(indexOf + 1, var.Length - indexOf - 1);
  424. }
  425. Class cls2 = CreateNewClass(ref classes, cls, var2);
  426. if (!cls2.fileds.ContainsKey(filedName))
  427. {
  428. ClassFiled classFiled = new ClassFiled();
  429. classFiled.name = filedName;
  430. classFiled.type = clsName;
  431. cls2.fileds[filedName] = classFiled;
  432. }
  433. }
  434. return classes[clsName];
  435. }
  436. private static void AddClassField(ref Dictionary<string, Class> classes, string cls, string filed, string type)
  437. {
  438. string var = string.Empty;
  439. string filedName = filed;
  440. int indexOf = filed.LastIndexOf(".");
  441. if (indexOf > 0)
  442. {
  443. var = filed.Substring(0, indexOf);
  444. filedName = filed.Substring(indexOf + 1, filed.Length - indexOf - 1);
  445. }
  446. Class newClass = CreateNewClass(ref classes, cls, var);
  447. if (!newClass.fileds.ContainsKey(filedName))
  448. {
  449. ClassFiled classFiled = new ClassFiled();
  450. classFiled.name = filedName;
  451. classFiled.type = type;
  452. newClass.fileds[filedName] = classFiled;
  453. }
  454. }
  455. private static StringBuilder classesToString(ref Dictionary<string, Class> classes, string clsName)
  456. {
  457. StringBuilder sb = new StringBuilder();
  458. clsName = clsName + "__Generate";
  459. foreach (var cls in classes.Values)
  460. {
  461. if (cls.fileds.Count == 0)
  462. {
  463. continue;
  464. }
  465. string str = string.Empty;
  466. if (cls.super.Length > 0)
  467. {
  468. if (cls.super == typeof(GameObject).FullName)
  469. {
  470. str += string.Format("---@class {0}\r\n", cls.name);
  471. str += string.Format("---@field public {0} {1}\r\n", "gameObject", typeof(GameObject).FullName);
  472. }
  473. else
  474. str += string.Format("---@class {0} : {1}\r\n", cls.name, cls.super);
  475. }
  476. else
  477. str += string.Format("---@class {0}\r\n", cls.name);
  478. foreach (var filed in cls.fileds.Values)
  479. {
  480. string type = filed.type;
  481. if (classes.ContainsKey(type) && classes[type].fileds.Count == 0)
  482. {
  483. type = classes[type].super;
  484. }
  485. string p = "public";
  486. if (cls.name == clsName)
  487. p = "private";
  488. str += string.Format("---@field {0} {1} {2}\r\n", p, filed.name, type);
  489. }
  490. if (cls.name == clsName)
  491. sb.Append(str);
  492. else
  493. {
  494. str += "\r\n";
  495. sb.Insert(0, str);
  496. }
  497. }
  498. return sb;
  499. }
  500. private static void GenerateMap(GameObject obj, string className, string path, string luaPath, int type = 0)
  501. {
  502. StringBuilder sb_logic = new StringBuilder();
  503. string baseClassName = "UIViewBase";
  504. string viewName = className + "View";
  505. string logic = string.Empty;
  506. Dictionary<string, Class> classes = new Dictionary<string, Class>();
  507. AddClassField(ref classes, className, "gameObject", typeof(GameObject).FullName);
  508. AddClassField(ref classes, className, "transform", typeof(Transform).FullName);
  509. nullStr.Clear();
  510. nullPeerStr.Clear();
  511. StringBuilder sb = new StringBuilder();
  512. if (type == 0)
  513. {
  514. sb.Append(string.Format("local {0} = class(\"{1}\", require(\"{2}\"))\r\n", viewName, viewName, baseClassName));
  515. }
  516. else
  517. {
  518. sb.Append(string.Format("local {0} = class(\"{1}\")\r\n", viewName, viewName));
  519. }
  520. sb.Append("\r\n");
  521. sb.Append(string.Format("function {0}:ctor()\r\n", viewName));
  522. sb.Append("end\r\n");
  523. sb.Append("\r\n");
  524. sb.Append("---@private\r\n");
  525. sb.Append(string.Format("function {0}:SetActive(result)\r\n", viewName));
  526. sb.Append("\tself.gameObject:SetActive(result)\r\n");
  527. sb.Append("end\r\n");
  528. sb.Append("---@private\r\n");
  529. sb.Append(string.Format("function {0}:InitGenerate(Root, data)\r\n", viewName));
  530. sb.Append("\tself.transform = Root\r\n");
  531. sb.Append("\tself.inited = true\r\n");
  532. sb.Append("\r\n");
  533. if (type == 0)
  534. {
  535. sb.Append("\tif self.super.Init then\r\n");
  536. sb.Append("\t\tself.super.Init(self)\r\n");
  537. sb.Append("\tend\r\n");
  538. }
  539. sb.Append("\tlocal tmp\r\n");
  540. sb.Append("\r\n");
  541. StringBuilder tmpsb = new StringBuilder();
  542. StringBuilder funcsb = new StringBuilder();
  543. int funcIndex = 0;
  544. Func<StringBuilder, bool> addFuncString = builder =>
  545. {
  546. if (builder.Length > 0)
  547. {
  548. ++funcIndex;
  549. funcsb.Append("---@private\r\n");
  550. funcsb.Append(string.Format("function {0}:InitGenerate__{1}(Root, data)\r\n", viewName, funcIndex));
  551. funcsb.Append(builder);
  552. funcsb.Append("end\r\n");
  553. funcsb.Append("\r\n");
  554. builder.Clear();
  555. }
  556. return true;
  557. };
  558. Func<string, Transform, bool> doInit = null;
  559. doInit = (parentName, trans) =>
  560. {
  561. bool bcontinue = true;
  562. foreach (Transform child in trans)
  563. {
  564. if (child.parent != obj.transform && child.parent.GetComponent<UIGridViewMark>() != null) continue;
  565. string name = CheckUnityTYpe(false, obj, path, parentName, child, tmpsb, ref logic, className, out bcontinue, ref classes);
  566. addFuncString(tmpsb);
  567. if (bcontinue)
  568. doInit(name, child);
  569. }
  570. return true;
  571. };
  572. bool ttt = true;
  573. CheckUnityTYpe(true, obj, path, string.Empty, obj.transform, tmpsb, ref logic, className, out ttt, ref classes);
  574. addFuncString(tmpsb);
  575. doInit(string.Empty, obj.transform);
  576. for (int i = 0; i < funcIndex; i++)
  577. {
  578. sb.Append(string.Format("\tself:InitGenerate__{0}(Root,data)\r\n", i + 1));
  579. }
  580. sb.Append("\r\n");
  581. sb.Append("\r\n");
  582. sb.Append("end\r\n");
  583. sb.Append("\r\n");
  584. sb.Append(funcsb);
  585. sb.Append("---@private\r\n");
  586. sb.Append(string.Format("function {0}:GenerateDestroy()\r\n", viewName));
  587. /*foreach (var str in nullPeerStr)
  588. {
  589. sb.Append(str);
  590. }*/
  591. for (int i = nullPeerStr.Count - 1; i >= 0;--i)
  592. {
  593. sb.Append(nullPeerStr[i]);
  594. }
  595. foreach (var str in nullStr)
  596. {
  597. sb.Append(str);
  598. }
  599. sb.Append("\tself.transform = nil\r\n");
  600. sb.Append("\tself.gameObject = nil\r\n");
  601. sb.Append("\tself.inited = false\r\n");
  602. sb.Append("end\r\n");
  603. sb.Append(string.Format("return {0}", viewName));
  604. sb.Insert(0, classesToString(ref classes, className));
  605. using (StreamWriter textWriter = new StreamWriter(path, false, new UTF8Encoding(false)))
  606. {
  607. string rtStr = string.Format("return {0}", className);
  608. logic = logic.Replace(rtStr, "");
  609. logic += "\r\n";
  610. logic += rtStr + "\r\n";
  611. int indexOf = logic.IndexOf("\r\n\r\n\r\n");
  612. while (indexOf != -1)
  613. {
  614. logic = logic.Replace("\r\n\r\n\r\n", "\r\n\r\n");
  615. indexOf = logic.IndexOf("\r\n\r\n\r\n");
  616. }
  617. StringBuilder GenerateFieldStr = new StringBuilder();
  618. GenerateFieldStr.Append(string.Format("---@class {0}\r\n", className));
  619. GenerateFieldStr.Append(string.Format("---##################### 【{0} Generate Field】 Start #####################\r\n", className));
  620. GenerateFieldStr.Append(string.Format("---自动生成代码变量声明\r\n"));
  621. GenerateFieldStr.Append(string.Format("---%%%%%%%%%%%%%%%%%%%%% 【{0} Generate Field】 End %%%%%%%%%%%%%%%%%%%%%\r\n", className));
  622. StringBuilder CustomFieldStr = new StringBuilder();
  623. CustomFieldStr.Append(string.Format("---##################### 【{0} Custom Field】 Start #####################\r\n", className));
  624. CustomFieldStr.Append(string.Format("---TODO 自定义变量声明在这里: ---@field [public|protected|private] field_name FIELD_TYPE[|OTHER_TYPE]\r\n"));
  625. CustomFieldStr.Append(string.Format("---%%%%%%%%%%%%%%%%%%%%% 【{0} Custom Field】 End %%%%%%%%%%%%%%%%%%%%%\r\n", className));
  626. string GenerateFieldEnd = string.Format("---%%%%%%%%%%%%%%%%%%%%% 【{0} Generate Field】 End %%%%%%%%%%%%%%%%%%%%%\r\n", className);
  627. string CustomFieldEnd = string.Format("---%%%%%%%%%%%%%%%%%%%%% 【{0} Custom Field】 End %%%%%%%%%%%%%%%%%%%%%\r\n", className);
  628. string s = string.Format("---@class {0} : {0}__Generate\r\n", className);
  629. int GenerateFieldEndIndex = logic.IndexOf(GenerateFieldEnd);
  630. int CustomFieldEndIndex = logic.IndexOf(CustomFieldEnd);
  631. if (CustomFieldEndIndex != -1)
  632. {
  633. //如果已经有了
  634. //需要更新Generate
  635. //TODO
  636. if (GenerateFieldEndIndex != -1)
  637. {
  638. logic = logic.Replace(GenerateFieldStr.ToString(), s);
  639. }
  640. }
  641. else
  642. {
  643. //如果没有
  644. logic = s + CustomFieldStr.ToString() + logic;
  645. }
  646. textWriter.Write(logic);
  647. textWriter.Flush();
  648. textWriter.Close();
  649. }
  650. using (StreamWriter textWriter = new StreamWriter(path, false, new UTF8Encoding(false)))
  651. {
  652. textWriter.Write(sb.ToString());
  653. textWriter.Flush();
  654. textWriter.Close();
  655. }
  656. }
  657. private static void CheckRepeadUIName(GameObject depObj, string uiName, string path)
  658. {
  659. if (!uiNodes.ContainsKey(depObj))
  660. {
  661. uiNodes[depObj] = new Dictionary<string, string>();
  662. }
  663. Dictionary<string, string> dictionary = uiNodes[depObj];
  664. if (dictionary.ContainsKey(uiName))
  665. {
  666. Debug.Log(string.Format("UI名不能重复, {0} 重复节点: {1} {2}", depObj.name, dictionary[uiName], path));
  667. UnityEditor.EditorUtility.DisplayDialog("提示", string.Format("UI名不能重复, {0} 重复节点: {1} {2}", depObj.name, dictionary[uiName], path), "OK");
  668. }
  669. else
  670. {
  671. dictionary[uiName] = path;
  672. }
  673. }
  674. private static string CheckUnityTYpe(bool bTop, GameObject topObj, string fPath, string path, Transform trans, StringBuilder sb, ref string logic, string className, out bool bcontinue, ref Dictionary<string, Class> classes)
  675. {
  676. string name = string.Empty;
  677. if (path != String.Empty)
  678. name = path + "/" + trans.name;
  679. else
  680. name = trans.name;
  681. string ErroName = name;
  682. if (bTop)
  683. {
  684. ErroName = name;
  685. name = "Root";
  686. }
  687. bcontinue = true;
  688. UINode uiNode = trans.GetComponent<UINode>();
  689. if (uiNode && uiNode.enabled)
  690. {
  691. List<string> tmpNodes = new List<string>();
  692. string uiName = string.Empty;
  693. if (uiNode && uiNode.enabled)
  694. {
  695. uiName = trimName(uiNode.UIName);
  696. }
  697. if (uiName == string.Empty)
  698. {
  699. //UnityEditor.EditorUtility.DisplayDialog("提示", string.Format("UI名不能为空, {0}", currPrefabPath), "OK");
  700. uiName = getComponentArgNameWithName(trimName(trans.name));
  701. }
  702. sb.Append("--[[\r\n");
  703. sb.Append(string.Format("\t{0}\r\n", name));
  704. sb.Append("--]]\r\n");
  705. string varName = string.Empty;
  706. if (!bTop)
  707. {
  708. sb.Append(string.Format("\tlocal tmp = Root:Find(\"{0}\").gameObject\r\n", name));
  709. if (uiNode && uiNode.enabled)
  710. {
  711. sb.Append(string.Format("\tif tolua.getpeer(tmp) == nil then\r\n"));
  712. sb.Append(string.Format("\t\ttolua.setpeer(tmp, {{}})\r\n"));
  713. sb.Append("\tend\r\n");
  714. int depNum = 0;
  715. if (uiNode.depObjs != null)
  716. {
  717. foreach (var depObj in uiNode.depObjs)
  718. {
  719. if (depObj)
  720. {
  721. depNum++;
  722. }
  723. }
  724. }
  725. //DepObjs
  726. if (depNum == 0)
  727. {
  728. CheckRepeadUIName(topObj, uiName, ErroName);
  729. if (trans.GetComponent<UIGridViewMark>() != null)
  730. {
  731. UIGridViewMark item = trans.GetComponent<UIGridViewMark>();
  732. string gridItemName = item.GridItemName;
  733. if (string.IsNullOrEmpty(item.GridItemName))
  734. {
  735. UnityEngine.Object parentObject = PrefabUtility.GetCorrespondingObjectFromSource(item.gameObject);
  736. gridItemName = parentObject != null ? parentObject.name : item.gameObject.name;
  737. }
  738. gridItemName = gridItemName.Substring(0, 1).ToUpper() + gridItemName.Substring(1);
  739. sb.Append(string.Format("\tself.{0} = CommonUtil.BindGridViewItem2LuaStatic(\"{1}\", tmp)\r\n", uiName, gridItemName));
  740. sb.Append(string.Format("\tself.{0}.prefabName = \"{1}\"\r\n", uiName, item.GridItemName));
  741. nullStr.Add(string.Format("\tif self.{0}.GenerateDestroy ~= nil then\r\n", uiName));
  742. nullStr.Add(string.Format("\t\tself.{0}:GenerateDestroy()\r\n", uiName));
  743. nullStr.Add(string.Format("\tend\r\n", uiName));
  744. }
  745. else
  746. {
  747. sb.Append(string.Format("\tself.{0} = tmp\r\n", uiName));
  748. }
  749. if (uiNode.activeType == ActiveType.Active)
  750. {
  751. sb.Append(string.Format("\tself.{0}:SetActive(true)\r\n", uiName));
  752. }
  753. else if (uiNode.activeType == ActiveType.Inactive)
  754. {
  755. sb.Append(string.Format("\tself.{0}:SetActive(false)\r\n", uiName));
  756. }
  757. nullStr.Add(string.Format("\tif tolua.getpeer(self.{0}) ~= nil then\r\n", uiName));
  758. nullStr.Add(string.Format("\t\ttolua.setpeer(self.{0}, nil)\r\n", uiName));
  759. nullStr.Add(string.Format("\tend\r\n", uiName));
  760. nullStr.Add(string.Format("\tself.{0} = nil\r\n", uiName));
  761. varName = string.Format("self.{0}", uiName);
  762. CreateNewClass(ref classes, className, uiName);
  763. tmpNodes.Add(uiName);
  764. }
  765. else
  766. {
  767. foreach (var depObj in uiNode.depObjs)
  768. {
  769. if (depObj)
  770. {
  771. List<string> paramStrs = new List<string>();
  772. Func<List<string>, UINode, string, bool> func = null;
  773. func = (list, dep, parmPath) =>
  774. {
  775. if (dep)
  776. {
  777. if (dep.gameObject == topObj)
  778. ;
  779. else
  780. {
  781. string s = trimName(dep.UIName);
  782. if (s == string.Empty)
  783. {
  784. s = getComponentArgNameWithName(trimName(dep.name));
  785. }
  786. parmPath = "." + s + parmPath;
  787. }
  788. bool end = false;
  789. if (dep.gameObject == topObj)
  790. end = true;
  791. else
  792. {
  793. if (dep.depObjs == null || dep.depObjs.Length == 0)
  794. end = true;
  795. }
  796. if (end)
  797. {
  798. list.Add(parmPath);
  799. }
  800. else
  801. {
  802. foreach (var value in dep.depObjs)
  803. {
  804. func(list, value, parmPath);
  805. }
  806. }
  807. }
  808. else
  809. {
  810. list.Add(parmPath);
  811. }
  812. return true;
  813. };
  814. func(paramStrs, depObj, string.Empty);
  815. foreach (var str in paramStrs)
  816. {
  817. //sb.Append(string.Format("\tself{0}.{1} = tmp\r\n", str, uiName));
  818. if (trans.GetComponent<UIGridViewMark>() != null)
  819. {
  820. UIGridViewMark item = trans.GetComponent<UIGridViewMark>();
  821. string gridItemName = item.GridItemName;
  822. if (string.IsNullOrEmpty(item.GridItemName))
  823. {
  824. UnityEngine.Object parentObject = PrefabUtility.GetCorrespondingObjectFromSource(item.gameObject);
  825. gridItemName = parentObject != null ? parentObject.name : item.gameObject.name;
  826. }
  827. gridItemName = gridItemName.Substring(0, 1).ToUpper() + gridItemName.Substring(1);
  828. sb.Append(string.Format("\tself{0}.{1} = CommonUtil.BindGridViewItem2LuaStatic(\"{2}\", tmp)\r\n", str, uiName, gridItemName));
  829. sb.Append(string.Format("\tself{0}.{1}.prefabName = \"{2}\"\r\n", str, uiName, gridItemName));
  830. string destroy = string.Format("\tif self{0}.{1}.GenerateDestroy ~= nil then\r\n\t\tself{0}.{1}:GenerateDestroy()\r\n\tend\r\n", str, uiName, str, uiName);
  831. string peer = string.Format("\tif tolua.getpeer(self{0}.{1}) ~= nil then\r\n\t\ttolua.setpeer(self{0}.{1}, nil)\r\n\tend\r\n", str, uiName, str, uiName);
  832. nullPeerStr.Add(peer);
  833. nullPeerStr.Add(destroy);
  834. //nullPeerStr.Add(string.Format("\tif self{0}.{1}.GenerateDestroy ~= nil then\r\n", str, uiName));
  835. //nullPeerStr.Add(string.Format("\t\tself{0}.{1}:GenerateDestroy()\r\n", str, uiName));
  836. //nullPeerStr.Add("\tend\r\n");
  837. }
  838. else
  839. {
  840. sb.Append(string.Format("\tself{0}.{1} = tmp\r\n", str, uiName));
  841. string peer = string.Format("\tif tolua.getpeer(self{0}.{1}) ~= nil then\r\n\t\ttolua.setpeer(self{0}.{1}, nil)\r\n\tend\r\n", str, uiName, str, uiName);
  842. nullPeerStr.Add(peer);
  843. }
  844. if (uiNode.activeType == ActiveType.Active)
  845. {
  846. sb.Append(string.Format("\tself{0}.{1}:SetActive(true)\r\n", str, uiName));
  847. }
  848. else if (uiNode.activeType == ActiveType.Inactive)
  849. {
  850. sb.Append(string.Format("\tself{0}.{1}:SetActive(false)\r\n", str, uiName));
  851. }
  852. //nullPeerStr.Add(string.Format("\tif tolua.getpeer(self{0}.{1}) ~= nil then\r\n", str, uiName));
  853. //nullPeerStr.Add(string.Format("\t\ttolua.setpeer(self{0}.{1}, nil)\r\n", str, uiName));
  854. //nullPeerStr.Add("\tend\r\n");
  855. string s = string.Format("{0}.{1}", str, uiName);
  856. CreateNewClass(ref classes, className, s.Substring(1, s.Length - 1));
  857. tmpNodes.Add(s.Substring(1, s.Length - 1));
  858. if (varName == string.Empty)
  859. {
  860. varName = string.Format("self{0}.{1}", str, uiName);
  861. }
  862. }
  863. CheckRepeadUIName(depObj.gameObject, uiName, ErroName);
  864. }
  865. }
  866. }
  867. }
  868. }
  869. for (int i = 0; i < uiNode.keys.Count; i++)
  870. {
  871. string type = uiNode.keys[i];
  872. bool value = uiNode.values[i];
  873. if (!value)
  874. {
  875. continue;
  876. }
  877. if (bTop)
  878. {
  879. uiName = getComponentArgName(type);
  880. CheckRepeadUIName(topObj, uiName, ErroName);
  881. }
  882. //Button
  883. //Toggle
  884. //Slider
  885. //Scrollbar
  886. //Dropdown
  887. //InputField
  888. //RectTranform
  889. //Text
  890. //Image
  891. //RawImage
  892. //ScrollRect
  893. //CanvasGroup
  894. //Other
  895. CheckUnityTYpe_Default(varName, bTop, type, uiName, name, trans, sb, ref logic, className, ref tmpNodes, ref classes);
  896. }
  897. foreach (var typeName in functions.Keys)
  898. {
  899. bool needCode = false;
  900. for (int i = 0; i < uiNode.keys.Count; i++)
  901. {
  902. if (uiNode.keys[i] == typeName && !uiNode.values[i])
  903. {
  904. needCode = true;
  905. break;
  906. }
  907. }
  908. if (needCode)
  909. {
  910. sb.Append("\r\n");
  911. SetComponentCode(bTop, uiName, sb, typeName, className, ref tmpNodes, ref classes);
  912. }
  913. }
  914. }
  915. return name;
  916. }
  917. private static void CheckUnityTYpe_Default(string varName, bool bTop, string typeFullName, string uiName, string name, Transform trans, StringBuilder sb, ref string logic, string className, ref List<string> tmpNodes, ref Dictionary<string, Class> classes)
  918. {
  919. sb.Append("\r\n");
  920. SetComponentCode(bTop, uiName, sb, typeFullName, className, ref tmpNodes, ref classes);
  921. }
  922. private static string trimName(string name)
  923. {
  924. string trim = name.Replace(" ", "");
  925. trim = trim.Replace("\r", "");
  926. trim = trim.Replace("\n", "");
  927. trim = trim.Replace("\t", "");
  928. return trim;
  929. }
  930. public static string getComponentArgName(string typeFullName)
  931. {
  932. string name = typeFullName;
  933. int indexOf = name.LastIndexOf(".");
  934. if (indexOf != -1)
  935. {
  936. name = name.Substring(indexOf + 1, name.Length - indexOf - 1);
  937. }
  938. return getComponentArgNameWithName(name);
  939. }
  940. public static string getComponentArgNameWithName(string str)
  941. {
  942. return str.Substring(0, 1).ToLower() + str.Substring(1, str.Length - 1);
  943. }
  944. public static string getComponentTypeName(string fullName)
  945. {
  946. if (ComponentTypes.ContainsKey(fullName))
  947. {
  948. return "Enum.TypeInfo." + ComponentTypes[fullName];
  949. }
  950. else
  951. {
  952. string name = fullName;
  953. int indexOf = name.LastIndexOf(".");
  954. if (indexOf != -1)
  955. {
  956. name = name.Substring(indexOf + 1, name.Length - indexOf - 1);
  957. }
  958. return "Enum.TypeInfo." + name;
  959. }
  960. }
  961. private static void SetComponentCode(bool bTop, string uiName, StringBuilder sb, string fullName, string className, ref List<string> tmpNodes, ref Dictionary<string, Class> classes)
  962. {
  963. if (bTop)
  964. {
  965. string commonUI = getComponentArgName(fullName);
  966. sb.Append(string.Format("\tself.{0} = Root:GetComponent({1})\r\n", commonUI, getComponentTypeName(fullName)));
  967. if (getComponentTypeName(fullName).Equals("Enum.TypeInfo.Animator"))
  968. {
  969. sb.Append("\tself.animator.logWarnings = false\r\n");
  970. }
  971. AddClassField(ref classes, className, commonUI, fullName);
  972. if (functions.ContainsKey(fullName))
  973. {
  974. List<string> list = functions[fullName];
  975. foreach (var funcName in list)
  976. {
  977. sb.Append(string.Format("\tself.{0}:{1}()\r\n", commonUI, funcName));
  978. }
  979. }
  980. }
  981. else
  982. {
  983. string componentStr = string.Format("tmp.{0}", getComponentArgName(fullName));
  984. //在这里对textMeshProUGUI特殊处理,将textMeshProUGUI替换成text
  985. string newcomponentStr = componentStr.Replace(TextMeshProString, TextString);
  986. sb.Append(string.Format("\t{0} = tmp:GetComponent({1})\r\n", newcomponentStr, getComponentTypeName(fullName)));
  987. if (getComponentTypeName(fullName).Equals("Enum.TypeInfo.Animator"))
  988. {
  989. sb.Append("\ttmp.animator.logWarnings = false\r\n");
  990. }
  991. string s = newcomponentStr.Substring(4, newcomponentStr.Length - 4);
  992. foreach (var node in tmpNodes)
  993. {
  994. AddClassField(ref classes, className, string.Format("{0}.{1}", node, s), fullName);
  995. }
  996. if (functions.ContainsKey(fullName))
  997. {
  998. List<string> list = functions[fullName];
  999. foreach (var funcName in list)
  1000. {
  1001. sb.Append(string.Format("\t{0}:{1}()\r\n", componentStr, funcName));
  1002. }
  1003. }
  1004. }
  1005. }
  1006. static Dictionary<string, Dictionary<string, string>> GetData(string content)
  1007. {
  1008. int linestart = 1;
  1009. int rowstart = 1;
  1010. string splitword = ",";
  1011. string[] lineArray;
  1012. string[] charArray;
  1013. string text = content.Replace("\r\n", "@");
  1014. lineArray = text.Split("@"[0]);
  1015. //DebugHelper.LogWarning("[TableName: {0}]lineArray {1}",binAsset.name, lineArray[0]);
  1016. string[] indexname = lineArray[1].Split(splitword[0]);
  1017. Dictionary<string, Dictionary<string, string>> Table = new Dictionary<string, Dictionary<string, string>>();
  1018. for (int i = linestart; i < lineArray.Length; ++i)
  1019. {
  1020. charArray = lineArray[i].Split(splitword[0]);
  1021. if (charArray.Length > 0 && charArray[0] == string.Empty)
  1022. {
  1023. DebugHelper.LogWarning("[ConfigMgr. GetData] Empty Index {0} {1}", "UIConfig", i);
  1024. continue;
  1025. }
  1026. Dictionary<string, string> Row = new Dictionary<string, string>();
  1027. for (int j = 0; j < charArray.Length; ++j)
  1028. {
  1029. if (indexname[j] == "" || indexname[j] == string.Empty)
  1030. {
  1031. DebugHelper.LogWarning("[ConfigMgr. GetData] Empty key {0} {1}", "UIConfig", j);
  1032. continue;
  1033. }
  1034. if (Row.ContainsKey(indexname[j]))
  1035. {
  1036. DebugHelper.LogWarning("[ConfigMgr. GetData] {0} {1}", "UIConfig", indexname[j]);
  1037. continue;
  1038. }
  1039. Row.Add(indexname[j], charArray[j]);
  1040. }
  1041. if (Table.ContainsKey(charArray[0]))
  1042. {
  1043. DebugHelper.LogWarning("[ConfigMgr. GetData] {0} {1} {2}", "UIConfig", i, lineArray[i - 1]);
  1044. continue;
  1045. }
  1046. Table.Add(charArray[0], Row);
  1047. }
  1048. return Table;
  1049. }
  1050. static UICfgData? GetUIConfig(string name)
  1051. {
  1052. try
  1053. {
  1054. if (UIConfigData.Count == 0)
  1055. {
  1056. if (Application.isPlaying)
  1057. {
  1058. try
  1059. {
  1060. LuaState luaState = LuaMgr.Instance.luaState;
  1061. LuaForEach<int, LuaTable>(luaState, c_UICfgPath, InitUIConfig);
  1062. foreach (var data in UIConfigData)
  1063. {
  1064. UICfgData data1 = data.Value;
  1065. if (data1.name.Equals(name))
  1066. {
  1067. return data1;
  1068. }
  1069. }
  1070. }
  1071. catch (Exception e)
  1072. {
  1073. Debug.LogException(e);
  1074. }
  1075. }
  1076. else
  1077. {
  1078. using (LuaState luaState = new LuaState())
  1079. {
  1080. try
  1081. {
  1082. luaState.Start();
  1083. // LuaForEach<string, LuaTable>(luaState, c_LanguagePackagePath, InitLanguagePackage);
  1084. LuaForEach<int, LuaTable>(luaState, c_UICfgPath, InitUIConfig);
  1085. foreach (var data in UIConfigData)
  1086. {
  1087. UICfgData data1 = data.Value;
  1088. if (data1.name.Equals(name))
  1089. {
  1090. return data1;
  1091. }
  1092. }
  1093. }
  1094. catch (Exception e)
  1095. {
  1096. Debug.LogException(e);
  1097. }
  1098. }
  1099. }
  1100. }
  1101. else
  1102. {
  1103. foreach (var data in UIConfigData)
  1104. {
  1105. UICfgData data1 = data.Value;
  1106. if (data1.name.Equals(name))
  1107. {
  1108. return data1;
  1109. }
  1110. }
  1111. }
  1112. }
  1113. catch (Exception e)
  1114. {
  1115. Debug.LogException(e);
  1116. }
  1117. return null;
  1118. }
  1119. private static void InitUIConfig(int key, LuaTable value)
  1120. {
  1121. if (UIConfigData.ContainsKey(key))
  1122. {
  1123. Debug.LogError(c_UICfgPath + "存在相同的Id :" + key);
  1124. return;
  1125. }
  1126. UICfgData roleCfgData = new UICfgData()
  1127. {
  1128. id = key,
  1129. name = value.RawGet<string, string>("name"),
  1130. res_path = value.RawGet<string, string>("res_path"),
  1131. };
  1132. UIConfigData.Add(key, roleCfgData);
  1133. }
  1134. private static void LuaForEach<K, V>(LuaState luaState, string luaFile, Action<K, V> foreachFun)
  1135. {
  1136. using (LuaTable luaTable = luaState.DoFile<LuaTable>(luaFile))
  1137. {
  1138. try
  1139. {
  1140. using (LuaDictTable<K, V> luaDictTable = luaTable.ToDictTable<K, V>())
  1141. {
  1142. try
  1143. {
  1144. foreach (var item in luaDictTable)
  1145. {
  1146. foreachFun(item.Key, item.Value);
  1147. }
  1148. }
  1149. catch (Exception e)
  1150. {
  1151. Debug.LogException(e);
  1152. }
  1153. }
  1154. }
  1155. catch (Exception e)
  1156. {
  1157. Debug.LogException(e);
  1158. }
  1159. }
  1160. }
  1161. static void PackageUINodeGOPath(UINode node, ref string path)
  1162. {
  1163. if (node.UIName == string.Empty)
  1164. {
  1165. node.UIName = getComponentArgNameWithName(trimName(node.gameObject.name));
  1166. }
  1167. path = "." + path;
  1168. path = node.UIName + path;
  1169. if (node.depObjs.Length > 0)
  1170. {
  1171. UINode node1 = node.depObjs[0];
  1172. PackageUINodeGOPath(node1, ref path);
  1173. }
  1174. else
  1175. {
  1176. path = path.Substring(0, path.Length - 1);
  1177. }
  1178. }
  1179. [MenuItem("GameObject/FindUINodeGOPath", false, 21)]
  1180. public static void FindUINodeGOPath()
  1181. {
  1182. GameObject[] gameObjects = Selection.gameObjects;
  1183. if (gameObjects.Length == 0) return;
  1184. GameObject go = gameObjects[0];
  1185. UINode node = go.GetComponent<UINode>();
  1186. if (node == null)
  1187. {
  1188. DebugHelper.LogError("该对象不存在UINode 组件,请添加上再操作!");
  1189. return;
  1190. }
  1191. string path = string.Empty;
  1192. node.FindUINodeGOPath(ref path);
  1193. string rootName = "";
  1194. Transform[] parents = go.GetComponentsInParent<Transform>();
  1195. if (parents != null)
  1196. {
  1197. foreach (var parent in parents)
  1198. {
  1199. if (parent.parent == null || parent.parent.name.Equals("UIRoot") || parent.parent.name.Equals("UIRoot (Environment)"))
  1200. {
  1201. rootName = parent.name;
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. DebugHelper.LogError(rootName);
  1207. UICfgData? config = GetUIConfig(rootName);
  1208. if (config == null)
  1209. {
  1210. DebugHelper.LogError("[GenerateLua error] {0} {1} {2}", "UIConfig", rootName, "isnt exist");
  1211. return;
  1212. }
  1213. UICfgData data1 = config.Value;
  1214. int uiId = data1.id;
  1215. DebugHelper.LogError("所选对象 界面 路径 为: " + path + " uiname " + rootName + " uiId " + uiId);
  1216. }
  1217. }