GenerateLuaTemple.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. if (uiNode.IsNewNode)
  709. {
  710. sb.Append(string.Format("\tlocal tmp = nil\r\n", name));
  711. sb.Append(string.Format("\tlocal tfm = Root:Find(\"{0}\")\r\n", name));
  712. sb.Append(string.Format("\tif tfm == nil then\r\n", name));
  713. sb.Append(string.Format("\t\treturn\r\n", name));
  714. sb.Append(string.Format("\telse\r\n", name));
  715. sb.Append(string.Format("\t\ttmp = tfm.gameObject\r\n", name));
  716. sb.Append(string.Format("\tend\r\n", name));
  717. }
  718. else
  719. {
  720. sb.Append(string.Format("\tlocal tmp = Root:Find(\"{0}\").gameObject\r\n", name));
  721. }
  722. if (uiNode && uiNode.enabled)
  723. {
  724. sb.Append(string.Format("\tif tolua.getpeer(tmp) == nil then\r\n"));
  725. sb.Append(string.Format("\t\ttolua.setpeer(tmp, {{}})\r\n"));
  726. sb.Append("\tend\r\n");
  727. int depNum = 0;
  728. if (uiNode.depObjs != null)
  729. {
  730. foreach (var depObj in uiNode.depObjs)
  731. {
  732. if (depObj)
  733. {
  734. depNum++;
  735. }
  736. }
  737. }
  738. //DepObjs
  739. if (depNum == 0)
  740. {
  741. CheckRepeadUIName(topObj, uiName, ErroName);
  742. if (trans.GetComponent<UIGridViewMark>() != null)
  743. {
  744. UIGridViewMark item = trans.GetComponent<UIGridViewMark>();
  745. string gridItemName = item.GridItemName;
  746. if (string.IsNullOrEmpty(item.GridItemName))
  747. {
  748. UnityEngine.Object parentObject = PrefabUtility.GetCorrespondingObjectFromSource(item.gameObject);
  749. gridItemName = parentObject != null ? parentObject.name : item.gameObject.name;
  750. }
  751. gridItemName = gridItemName.Substring(0, 1).ToUpper() + gridItemName.Substring(1);
  752. sb.Append(string.Format("\tself.{0} = CommonUtil.BindGridViewItem2LuaStatic(\"{1}\", tmp)\r\n", uiName, gridItemName));
  753. sb.Append(string.Format("\tself.{0}.prefabName = \"{1}\"\r\n", uiName, item.GridItemName));
  754. nullStr.Add(string.Format("\tif self.{0}.GenerateDestroy ~= nil then\r\n", uiName));
  755. nullStr.Add(string.Format("\t\tself.{0}:GenerateDestroy()\r\n", uiName));
  756. nullStr.Add(string.Format("\tend\r\n", uiName));
  757. }
  758. else
  759. {
  760. sb.Append(string.Format("\tself.{0} = tmp\r\n", uiName));
  761. }
  762. if (uiNode.activeType == ActiveType.Active)
  763. {
  764. sb.Append(string.Format("\tself.{0}:SetActive(true)\r\n", uiName));
  765. }
  766. else if (uiNode.activeType == ActiveType.Inactive)
  767. {
  768. sb.Append(string.Format("\tself.{0}:SetActive(false)\r\n", uiName));
  769. }
  770. nullStr.Add(string.Format("\tif tolua.getpeer(self.{0}) ~= nil then\r\n", uiName));
  771. nullStr.Add(string.Format("\t\ttolua.setpeer(self.{0}, nil)\r\n", uiName));
  772. nullStr.Add(string.Format("\tend\r\n", uiName));
  773. nullStr.Add(string.Format("\tself.{0} = nil\r\n", uiName));
  774. varName = string.Format("self.{0}", uiName);
  775. CreateNewClass(ref classes, className, uiName);
  776. tmpNodes.Add(uiName);
  777. }
  778. else
  779. {
  780. foreach (var depObj in uiNode.depObjs)
  781. {
  782. if (depObj)
  783. {
  784. List<string> paramStrs = new List<string>();
  785. Func<List<string>, UINode, string, bool> func = null;
  786. func = (list, dep, parmPath) =>
  787. {
  788. if (dep)
  789. {
  790. if (dep.gameObject == topObj)
  791. ;
  792. else
  793. {
  794. string s = trimName(dep.UIName);
  795. if (s == string.Empty)
  796. {
  797. s = getComponentArgNameWithName(trimName(dep.name));
  798. }
  799. parmPath = "." + s + parmPath;
  800. }
  801. bool end = false;
  802. if (dep.gameObject == topObj)
  803. end = true;
  804. else
  805. {
  806. if (dep.depObjs == null || dep.depObjs.Length == 0)
  807. end = true;
  808. }
  809. if (end)
  810. {
  811. list.Add(parmPath);
  812. }
  813. else
  814. {
  815. foreach (var value in dep.depObjs)
  816. {
  817. func(list, value, parmPath);
  818. }
  819. }
  820. }
  821. else
  822. {
  823. list.Add(parmPath);
  824. }
  825. return true;
  826. };
  827. func(paramStrs, depObj, string.Empty);
  828. foreach (var str in paramStrs)
  829. {
  830. //sb.Append(string.Format("\tself{0}.{1} = tmp\r\n", str, uiName));
  831. if (trans.GetComponent<UIGridViewMark>() != null)
  832. {
  833. UIGridViewMark item = trans.GetComponent<UIGridViewMark>();
  834. string gridItemName = item.GridItemName;
  835. if (string.IsNullOrEmpty(item.GridItemName))
  836. {
  837. UnityEngine.Object parentObject = PrefabUtility.GetCorrespondingObjectFromSource(item.gameObject);
  838. gridItemName = parentObject != null ? parentObject.name : item.gameObject.name;
  839. }
  840. gridItemName = gridItemName.Substring(0, 1).ToUpper() + gridItemName.Substring(1);
  841. sb.Append(string.Format("\tself{0}.{1} = CommonUtil.BindGridViewItem2LuaStatic(\"{2}\", tmp)\r\n", str, uiName, gridItemName));
  842. sb.Append(string.Format("\tself{0}.{1}.prefabName = \"{2}\"\r\n", str, uiName, gridItemName));
  843. 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);
  844. 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);
  845. nullPeerStr.Add(peer);
  846. nullPeerStr.Add(destroy);
  847. //nullPeerStr.Add(string.Format("\tif self{0}.{1}.GenerateDestroy ~= nil then\r\n", str, uiName));
  848. //nullPeerStr.Add(string.Format("\t\tself{0}.{1}:GenerateDestroy()\r\n", str, uiName));
  849. //nullPeerStr.Add("\tend\r\n");
  850. }
  851. else
  852. {
  853. sb.Append(string.Format("\tself{0}.{1} = tmp\r\n", str, uiName));
  854. 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);
  855. nullPeerStr.Add(peer);
  856. }
  857. if (uiNode.activeType == ActiveType.Active)
  858. {
  859. sb.Append(string.Format("\tself{0}.{1}:SetActive(true)\r\n", str, uiName));
  860. }
  861. else if (uiNode.activeType == ActiveType.Inactive)
  862. {
  863. sb.Append(string.Format("\tself{0}.{1}:SetActive(false)\r\n", str, uiName));
  864. }
  865. //nullPeerStr.Add(string.Format("\tif tolua.getpeer(self{0}.{1}) ~= nil then\r\n", str, uiName));
  866. //nullPeerStr.Add(string.Format("\t\ttolua.setpeer(self{0}.{1}, nil)\r\n", str, uiName));
  867. //nullPeerStr.Add("\tend\r\n");
  868. string s = string.Format("{0}.{1}", str, uiName);
  869. CreateNewClass(ref classes, className, s.Substring(1, s.Length - 1));
  870. tmpNodes.Add(s.Substring(1, s.Length - 1));
  871. if (varName == string.Empty)
  872. {
  873. varName = string.Format("self{0}.{1}", str, uiName);
  874. }
  875. }
  876. CheckRepeadUIName(depObj.gameObject, uiName, ErroName);
  877. }
  878. }
  879. }
  880. }
  881. }
  882. for (int i = 0; i < uiNode.keys.Count; i++)
  883. {
  884. string type = uiNode.keys[i];
  885. bool value = uiNode.values[i];
  886. if (!value)
  887. {
  888. continue;
  889. }
  890. if (bTop)
  891. {
  892. uiName = getComponentArgName(type);
  893. CheckRepeadUIName(topObj, uiName, ErroName);
  894. }
  895. //Button
  896. //Toggle
  897. //Slider
  898. //Scrollbar
  899. //Dropdown
  900. //InputField
  901. //RectTranform
  902. //Text
  903. //Image
  904. //RawImage
  905. //ScrollRect
  906. //CanvasGroup
  907. //Other
  908. CheckUnityTYpe_Default(varName, bTop, type, uiName, name, trans, sb, ref logic, className, ref tmpNodes, ref classes);
  909. }
  910. foreach (var typeName in functions.Keys)
  911. {
  912. bool needCode = false;
  913. for (int i = 0; i < uiNode.keys.Count; i++)
  914. {
  915. if (uiNode.keys[i] == typeName && !uiNode.values[i])
  916. {
  917. needCode = true;
  918. break;
  919. }
  920. }
  921. if (needCode)
  922. {
  923. sb.Append("\r\n");
  924. SetComponentCode(bTop, uiName, sb, typeName, className, ref tmpNodes, ref classes);
  925. }
  926. }
  927. }
  928. return name;
  929. }
  930. 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)
  931. {
  932. sb.Append("\r\n");
  933. SetComponentCode(bTop, uiName, sb, typeFullName, className, ref tmpNodes, ref classes);
  934. }
  935. private static string trimName(string name)
  936. {
  937. string trim = name.Replace(" ", "");
  938. trim = trim.Replace("\r", "");
  939. trim = trim.Replace("\n", "");
  940. trim = trim.Replace("\t", "");
  941. return trim;
  942. }
  943. public static string getComponentArgName(string typeFullName)
  944. {
  945. string name = typeFullName;
  946. int indexOf = name.LastIndexOf(".");
  947. if (indexOf != -1)
  948. {
  949. name = name.Substring(indexOf + 1, name.Length - indexOf - 1);
  950. }
  951. return getComponentArgNameWithName(name);
  952. }
  953. public static string getComponentArgNameWithName(string str)
  954. {
  955. return str.Substring(0, 1).ToLower() + str.Substring(1, str.Length - 1);
  956. }
  957. public static string getComponentTypeName(string fullName)
  958. {
  959. if (ComponentTypes.ContainsKey(fullName))
  960. {
  961. return "Enum.TypeInfo." + ComponentTypes[fullName];
  962. }
  963. else
  964. {
  965. string name = fullName;
  966. int indexOf = name.LastIndexOf(".");
  967. if (indexOf != -1)
  968. {
  969. name = name.Substring(indexOf + 1, name.Length - indexOf - 1);
  970. }
  971. return "Enum.TypeInfo." + name;
  972. }
  973. }
  974. private static void SetComponentCode(bool bTop, string uiName, StringBuilder sb, string fullName, string className, ref List<string> tmpNodes, ref Dictionary<string, Class> classes)
  975. {
  976. if (bTop)
  977. {
  978. string commonUI = getComponentArgName(fullName);
  979. sb.Append(string.Format("\tself.{0} = Root:GetComponent({1})\r\n", commonUI, getComponentTypeName(fullName)));
  980. if (getComponentTypeName(fullName).Equals("Enum.TypeInfo.Animator"))
  981. {
  982. sb.Append("\tself.animator.logWarnings = false\r\n");
  983. }
  984. AddClassField(ref classes, className, commonUI, fullName);
  985. if (functions.ContainsKey(fullName))
  986. {
  987. List<string> list = functions[fullName];
  988. foreach (var funcName in list)
  989. {
  990. sb.Append(string.Format("\tself.{0}:{1}()\r\n", commonUI, funcName));
  991. }
  992. }
  993. }
  994. else
  995. {
  996. string componentStr = string.Format("tmp.{0}", getComponentArgName(fullName));
  997. //在这里对textMeshProUGUI特殊处理,将textMeshProUGUI替换成text
  998. string newcomponentStr = componentStr.Replace(TextMeshProString, TextString);
  999. sb.Append(string.Format("\t{0} = tmp:GetComponent({1})\r\n", newcomponentStr, getComponentTypeName(fullName)));
  1000. if (getComponentTypeName(fullName).Equals("Enum.TypeInfo.Animator"))
  1001. {
  1002. sb.Append("\ttmp.animator.logWarnings = false\r\n");
  1003. }
  1004. string s = newcomponentStr.Substring(4, newcomponentStr.Length - 4);
  1005. foreach (var node in tmpNodes)
  1006. {
  1007. AddClassField(ref classes, className, string.Format("{0}.{1}", node, s), fullName);
  1008. }
  1009. if (functions.ContainsKey(fullName))
  1010. {
  1011. List<string> list = functions[fullName];
  1012. foreach (var funcName in list)
  1013. {
  1014. sb.Append(string.Format("\t{0}:{1}()\r\n", componentStr, funcName));
  1015. }
  1016. }
  1017. }
  1018. }
  1019. static Dictionary<string, Dictionary<string, string>> GetData(string content)
  1020. {
  1021. int linestart = 1;
  1022. int rowstart = 1;
  1023. string splitword = ",";
  1024. string[] lineArray;
  1025. string[] charArray;
  1026. string text = content.Replace("\r\n", "@");
  1027. lineArray = text.Split("@"[0]);
  1028. //DebugHelper.LogWarning("[TableName: {0}]lineArray {1}",binAsset.name, lineArray[0]);
  1029. string[] indexname = lineArray[1].Split(splitword[0]);
  1030. Dictionary<string, Dictionary<string, string>> Table = new Dictionary<string, Dictionary<string, string>>();
  1031. for (int i = linestart; i < lineArray.Length; ++i)
  1032. {
  1033. charArray = lineArray[i].Split(splitword[0]);
  1034. if (charArray.Length > 0 && charArray[0] == string.Empty)
  1035. {
  1036. DebugHelper.LogWarning("[ConfigMgr. GetData] Empty Index {0} {1}", "UIConfig", i);
  1037. continue;
  1038. }
  1039. Dictionary<string, string> Row = new Dictionary<string, string>();
  1040. for (int j = 0; j < charArray.Length; ++j)
  1041. {
  1042. if (indexname[j] == "" || indexname[j] == string.Empty)
  1043. {
  1044. DebugHelper.LogWarning("[ConfigMgr. GetData] Empty key {0} {1}", "UIConfig", j);
  1045. continue;
  1046. }
  1047. if (Row.ContainsKey(indexname[j]))
  1048. {
  1049. DebugHelper.LogWarning("[ConfigMgr. GetData] {0} {1}", "UIConfig", indexname[j]);
  1050. continue;
  1051. }
  1052. Row.Add(indexname[j], charArray[j]);
  1053. }
  1054. if (Table.ContainsKey(charArray[0]))
  1055. {
  1056. DebugHelper.LogWarning("[ConfigMgr. GetData] {0} {1} {2}", "UIConfig", i, lineArray[i - 1]);
  1057. continue;
  1058. }
  1059. Table.Add(charArray[0], Row);
  1060. }
  1061. return Table;
  1062. }
  1063. static UICfgData? GetUIConfig(string name)
  1064. {
  1065. try
  1066. {
  1067. if (UIConfigData.Count == 0)
  1068. {
  1069. if (Application.isPlaying)
  1070. {
  1071. try
  1072. {
  1073. LuaState luaState = LuaMgr.Instance.luaState;
  1074. LuaForEach<int, LuaTable>(luaState, c_UICfgPath, InitUIConfig);
  1075. foreach (var data in UIConfigData)
  1076. {
  1077. UICfgData data1 = data.Value;
  1078. if (data1.name.Equals(name))
  1079. {
  1080. return data1;
  1081. }
  1082. }
  1083. }
  1084. catch (Exception e)
  1085. {
  1086. Debug.LogException(e);
  1087. }
  1088. }
  1089. else
  1090. {
  1091. using (LuaState luaState = new LuaState())
  1092. {
  1093. try
  1094. {
  1095. luaState.Start();
  1096. // LuaForEach<string, LuaTable>(luaState, c_LanguagePackagePath, InitLanguagePackage);
  1097. LuaForEach<int, LuaTable>(luaState, c_UICfgPath, InitUIConfig);
  1098. foreach (var data in UIConfigData)
  1099. {
  1100. UICfgData data1 = data.Value;
  1101. if (data1.name.Equals(name))
  1102. {
  1103. return data1;
  1104. }
  1105. }
  1106. }
  1107. catch (Exception e)
  1108. {
  1109. Debug.LogException(e);
  1110. }
  1111. }
  1112. }
  1113. }
  1114. else
  1115. {
  1116. foreach (var data in UIConfigData)
  1117. {
  1118. UICfgData data1 = data.Value;
  1119. if (data1.name.Equals(name))
  1120. {
  1121. return data1;
  1122. }
  1123. }
  1124. }
  1125. }
  1126. catch (Exception e)
  1127. {
  1128. Debug.LogException(e);
  1129. }
  1130. return null;
  1131. }
  1132. private static void InitUIConfig(int key, LuaTable value)
  1133. {
  1134. if (UIConfigData.ContainsKey(key))
  1135. {
  1136. Debug.LogError(c_UICfgPath + "存在相同的Id :" + key);
  1137. return;
  1138. }
  1139. UICfgData roleCfgData = new UICfgData()
  1140. {
  1141. id = key,
  1142. name = value.RawGet<string, string>("name"),
  1143. res_path = value.RawGet<string, string>("res_path"),
  1144. };
  1145. UIConfigData.Add(key, roleCfgData);
  1146. }
  1147. private static void LuaForEach<K, V>(LuaState luaState, string luaFile, Action<K, V> foreachFun)
  1148. {
  1149. using (LuaTable luaTable = luaState.DoFile<LuaTable>(luaFile))
  1150. {
  1151. try
  1152. {
  1153. using (LuaDictTable<K, V> luaDictTable = luaTable.ToDictTable<K, V>())
  1154. {
  1155. try
  1156. {
  1157. foreach (var item in luaDictTable)
  1158. {
  1159. foreachFun(item.Key, item.Value);
  1160. }
  1161. }
  1162. catch (Exception e)
  1163. {
  1164. Debug.LogException(e);
  1165. }
  1166. }
  1167. }
  1168. catch (Exception e)
  1169. {
  1170. Debug.LogException(e);
  1171. }
  1172. }
  1173. }
  1174. static void PackageUINodeGOPath(UINode node, ref string path)
  1175. {
  1176. if (node.UIName == string.Empty)
  1177. {
  1178. node.UIName = getComponentArgNameWithName(trimName(node.gameObject.name));
  1179. }
  1180. path = "." + path;
  1181. path = node.UIName + path;
  1182. if (node.depObjs.Length > 0)
  1183. {
  1184. UINode node1 = node.depObjs[0];
  1185. PackageUINodeGOPath(node1, ref path);
  1186. }
  1187. else
  1188. {
  1189. path = path.Substring(0, path.Length - 1);
  1190. }
  1191. }
  1192. [MenuItem("GameObject/FindUINodeGOPath", false, 21)]
  1193. public static void FindUINodeGOPath()
  1194. {
  1195. GameObject[] gameObjects = Selection.gameObjects;
  1196. if (gameObjects.Length == 0) return;
  1197. GameObject go = gameObjects[0];
  1198. UINode node = go.GetComponent<UINode>();
  1199. if (node == null)
  1200. {
  1201. DebugHelper.LogError("该对象不存在UINode 组件,请添加上再操作!");
  1202. return;
  1203. }
  1204. string path = string.Empty;
  1205. node.FindUINodeGOPath(ref path);
  1206. string rootName = "";
  1207. Transform[] parents = go.GetComponentsInParent<Transform>();
  1208. if (parents != null)
  1209. {
  1210. foreach (var parent in parents)
  1211. {
  1212. if (parent.parent == null || parent.parent.name.Equals("UIRoot") || parent.parent.name.Equals("UIRoot (Environment)"))
  1213. {
  1214. rootName = parent.name;
  1215. break;
  1216. }
  1217. }
  1218. }
  1219. DebugHelper.LogError(rootName);
  1220. UICfgData? config = GetUIConfig(rootName);
  1221. if (config == null)
  1222. {
  1223. DebugHelper.LogError("[GenerateLua error] {0} {1} {2}", "UIConfig", rootName, "isnt exist");
  1224. return;
  1225. }
  1226. UICfgData data1 = config.Value;
  1227. int uiId = data1.id;
  1228. DebugHelper.LogError("所选对象 界面 路径 为: " + path + " uiname " + rootName + " uiId " + uiId);
  1229. }
  1230. }