UITemplate.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using UnityEngine.UI;
  5. using System;
  6. using System.Reflection;
  7. using System.IO;
  8. using System.Collections.Generic;
  9. #pragma warning disable
  10. public class UITemplate
  11. {
  12. //模板prafeb所在路径
  13. const string c_TemplateResPath = "EditorTemplateRes/";
  14. //接口名标志
  15. const string c_InterfaceName = "Interface";
  16. const string c_TemplateName = "Template";
  17. //所有模板名称
  18. List<string> m_allTemplateName = new List<string>();
  19. //当前显示的模板名
  20. string m_nowTemplateName = null;
  21. string m_newTemplateName = null;
  22. //模板实例
  23. GameObject m_UITemplate;
  24. //模板预设
  25. GameObject m_preUITemplate;
  26. //当前选中的gameObject
  27. GameObject m_SelectedNode;
  28. public void OnEnable()
  29. {
  30. EditorGUIStyleData.Init();
  31. FindAllUITemplate();
  32. }
  33. //当工程改变时
  34. public void OnProjectChange()
  35. {
  36. FindAllUITemplate();
  37. }
  38. #region GUI
  39. public void GUI()
  40. {
  41. CurrentChildGUI();
  42. CreatBlankTemple_GUI();
  43. TempleInfo_GUI();
  44. ShowAllTemple_GUI();
  45. if (GUILayout.Button("应用所有模板"))
  46. {
  47. ApplyAllUITemplate();
  48. }
  49. }
  50. #endregion
  51. #region 创建模板
  52. //隐藏创建部分
  53. bool b_isFoldCreatTemplate = false;
  54. public void CurrentChildGUI()
  55. {
  56. EditorGUI.indentLevel = 1;
  57. EditorGUILayout.Space();
  58. EditorGUILayout.ObjectField("当前节点:", m_SelectedNode, typeof(GameObject));
  59. EditorGUILayout.Space();
  60. }
  61. /// <summary>
  62. /// 创建部分UI显示
  63. /// </summary>
  64. public void CreatBlankTemple_GUI()
  65. {
  66. EditorGUI.indentLevel = 1;
  67. b_isFoldCreatTemplate = EditorGUILayout.Foldout((b_isFoldCreatTemplate), "创建模板:");
  68. if (b_isFoldCreatTemplate)
  69. {
  70. EditorGUI.indentLevel = 2;
  71. EditorGUILayout.LabelField("提示:模板名中不能含有下划线");
  72. m_newTemplateName = EditorGUILayout.TextField("模板名称:", m_newTemplateName);
  73. EditorGUILayout.BeginHorizontal();
  74. EditorGUILayout.Space();
  75. if (IsReasonable(m_newTemplateName))
  76. {
  77. if (HaveTheTemlate(m_newTemplateName))
  78. {
  79. if (GUILayout.Button("覆盖模板", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  80. {
  81. if (EditorUtility.DisplayDialog("警告", "该模板已存在,是否覆盖?", "是", "否"))
  82. {
  83. CreatUIBlankTemplate();
  84. m_newTemplateName = "";
  85. }
  86. }
  87. }
  88. else
  89. {
  90. if (GUILayout.Button("创建一个空白模板", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  91. {
  92. CreatUIBlankTemplate();
  93. m_newTemplateName = "";
  94. }
  95. }
  96. }
  97. EditorGUILayout.Space();
  98. EditorGUILayout.EndHorizontal();
  99. }
  100. }
  101. /// <summary>
  102. /// 判断是否符合规定
  103. /// </summary>
  104. bool IsReasonable(string TemplateName)
  105. {
  106. //不能含有下划线
  107. if (TemplateName != null
  108. && TemplateName != ""
  109. && !TemplateName.Contains("_"))
  110. {
  111. return true;
  112. }
  113. else
  114. {
  115. return false;
  116. }
  117. }
  118. /// <summary>
  119. /// 创建一个空白模板
  120. /// </summary>
  121. public void CreatUIBlankTemplate()
  122. {
  123. m_UITemplate = new GameObject(c_TemplateName + "_" + m_newTemplateName);
  124. m_preUITemplate = null;
  125. m_UITemplate.layer = LayerMask.NameToLayer("UI");
  126. RectTransform rt = m_UITemplate.AddComponent<RectTransform>();
  127. if(m_SelectedNode != null)
  128. {
  129. m_UITemplate.transform.SetParent(m_SelectedNode.transform);
  130. }
  131. m_UITemplate.transform.localScale = Vector3.one;
  132. rt.anchorMin = Vector2.zero;
  133. rt.anchorMax = Vector2.one;
  134. rt.anchoredPosition3D = Vector3.zero;
  135. rt.sizeDelta = Vector2.zero;
  136. rt.localScale = Vector3.one;
  137. SaveTemplate(m_UITemplate.name);
  138. b_isFoldCreatTemplate = false;
  139. b_isFoldTemplateInfo = true;
  140. }
  141. #endregion
  142. #region 模板当前信息
  143. //是否折叠
  144. bool b_isFoldTemplateInfo = true;
  145. List<GameObject> m_interfaceList = new List<GameObject>();
  146. public void TempleInfo_GUI()
  147. {
  148. SelectCurrentTemplate();
  149. if (m_UITemplate != null)
  150. {
  151. EditorGUI.indentLevel = 1;
  152. b_isFoldTemplateInfo = EditorGUILayout.Foldout((b_isFoldTemplateInfo), "当前模板信息:");
  153. if (b_isFoldTemplateInfo)
  154. {
  155. EditorGUI.indentLevel = 2;
  156. EditorGUILayout.LabelField("模板名称:" + m_nowTemplateName);
  157. EditorGUILayout.ObjectField("模板预设:", m_preUITemplate, typeof(GameObject));
  158. EditorGUILayout.ObjectField("模板实例:", m_UITemplate, typeof(GameObject));
  159. InterfaceGUI();
  160. EditorGUI.indentLevel = 2;
  161. EditorGUILayout.BeginHorizontal();
  162. EditorGUILayout.Space();
  163. if (GUILayout.Button("保存模板", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  164. {
  165. SaveTemplate(m_nowTemplateName);
  166. }
  167. //EditorGUILayout.Space();
  168. EditorGUILayout.EndHorizontal();
  169. EditorGUILayout.BeginHorizontal();
  170. EditorGUILayout.Space();
  171. if (GUILayout.Button("应用模板", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  172. {
  173. ApplyOneTemplate(m_nowTemplateName);
  174. }
  175. //EditorGUILayout.Space();
  176. EditorGUILayout.EndHorizontal();
  177. EditorGUILayout.BeginHorizontal();
  178. EditorGUILayout.Space();
  179. if (GUILayout.Button("删除模板", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  180. {
  181. if (EditorUtility.DisplayDialog("警告", "该操作不可逆,确定删除该模板?", "是", "否"))
  182. {
  183. DeleteTemlate(m_nowTemplateName);
  184. GameObject.DestroyImmediate(m_UITemplate);
  185. }
  186. }
  187. //EditorGUILayout.Space();
  188. EditorGUILayout.EndHorizontal();
  189. EditorGUILayout.Space();
  190. }
  191. }
  192. }
  193. public void SelectCurrentTemplate()
  194. {
  195. if (Selection.GetFiltered(typeof(GameObject), SelectionMode.DeepAssets).Length == 0)
  196. {
  197. return;
  198. }
  199. m_UITemplate = Selection.GetFiltered(typeof(GameObject), SelectionMode.DeepAssets)[0] as GameObject;
  200. m_UITemplate = GetTemlateParant(m_UITemplate);
  201. if (m_UITemplate != null)
  202. {
  203. m_nowTemplateName = GetTemplateName(m_UITemplate.name);
  204. m_preUITemplate = FindPrefabInTemplateRes(m_nowTemplateName);
  205. }
  206. }
  207. //保存模板
  208. void SaveTemplate(string l_templateName)
  209. {
  210. GameObject saveprefab = GetSavePerfab(m_UITemplate);
  211. m_preUITemplate = PrefabUtility.CreatePrefab("Assets/" + c_TemplateResPath + l_templateName + ".prefab", saveprefab, ReplacePrefabOptions.ConnectToPrefab);
  212. GameObject.DestroyImmediate(saveprefab);
  213. }
  214. GameObject GetSavePerfab(GameObject targetPerfab)
  215. {
  216. GameObject goTmp = GameObject.Instantiate(targetPerfab);
  217. goTmp.name = goTmp.name.Replace("(Clone)", "");
  218. //除去接口的子节点,以免重复保存
  219. RemoveInterface(goTmp.transform);
  220. return goTmp;
  221. }
  222. void RemoveInterface(Transform node)
  223. {
  224. foreach (Transform nodeTmp in node)
  225. {
  226. if (nodeTmp.name.Contains(c_InterfaceName))
  227. {
  228. foreach (Transform no in nodeTmp)
  229. {
  230. GameObject.DestroyImmediate(no.gameObject);
  231. }
  232. }
  233. else
  234. {
  235. RemoveInterface(nodeTmp);
  236. }
  237. }
  238. }
  239. //删除模板(包括数据)
  240. void DeleteTemlate(string l_TemplateName)
  241. {
  242. GameObject.DestroyImmediate(FindPrefabInTemplateRes(l_TemplateName), true);
  243. Debug.Log(Application.dataPath + "/" + c_TemplateResPath + l_TemplateName + ".prefab");
  244. File.Delete(Application.dataPath + "/" + c_TemplateResPath + l_TemplateName + ".prefab");
  245. File.Delete(Application.dataPath + "/" + c_TemplateResPath + l_TemplateName + ".prefab.meta");
  246. AssetDatabase.Refresh();
  247. }
  248. #region 接口
  249. bool b_isFoldInterface = false;
  250. bool b_isFoldCreateInterface = false;
  251. bool b_isFoldInterfaceList = false;
  252. public void InterfaceGUI()
  253. {
  254. EditorGUI.indentLevel = 2;
  255. b_isFoldInterface = EditorGUILayout.Foldout(b_isFoldInterface, "接口:");
  256. if (b_isFoldInterface)
  257. {
  258. m_interfaceList = GetInterfaceList(m_UITemplate);
  259. EditorGUI.indentLevel = 3;
  260. b_isFoldInterfaceList = EditorGUILayout.Foldout(b_isFoldInterfaceList, "接口列表:");
  261. if (b_isFoldInterfaceList)
  262. {
  263. EditorGUI.indentLevel = 4;
  264. InterfaceListGUI();
  265. }
  266. EditorGUI.indentLevel = 3;
  267. b_isFoldCreateInterface = EditorGUILayout.Foldout(b_isFoldCreateInterface, "创建接口:");
  268. if (b_isFoldCreateInterface)
  269. {
  270. EditorGUI.indentLevel = 4;
  271. newInterfaceName = EditorGUILayout.TextField("接口名:", newInterfaceName);
  272. EditorGUILayout.BeginHorizontal();
  273. EditorGUILayout.Space();
  274. if (IsReasonable(newInterfaceName)
  275. && !IsContainsInterfaceName(newInterfaceName))
  276. {
  277. if (GUILayout.Button("在当前节点添加一个接口", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  278. {
  279. CreatOneInterface();
  280. newInterfaceName = "";
  281. }
  282. EditorGUILayout.Space();
  283. }
  284. EditorGUILayout.EndHorizontal();
  285. }
  286. EditorGUILayout.Space();
  287. }
  288. }
  289. //创建一个接口
  290. void CreatOneInterface()
  291. {
  292. if (m_SelectedNode == null)
  293. {
  294. Debug.LogError("请先选中要添加接口的GameObject!");
  295. return;
  296. }
  297. GameObject go_newPort = new GameObject(c_InterfaceName + "_" + newInterfaceName);
  298. go_newPort.transform.parent = m_SelectedNode.transform;
  299. RectTransform rt = go_newPort.AddComponent<RectTransform>();
  300. rt.anchorMin = Vector2.zero;
  301. rt.anchorMax = Vector2.one;
  302. rt.anchoredPosition3D = Vector3.zero;
  303. rt.sizeDelta = Vector2.zero;
  304. rt.localScale = Vector3.one;
  305. }
  306. void InterfaceListGUI()
  307. {
  308. for (int i = 0; i < m_interfaceList.Count;i++ )
  309. {
  310. EditorGUILayout.BeginHorizontal();
  311. EditorGUILayout.LabelField(m_interfaceList[i].name.Split('_')[1] +":");
  312. EditorGUILayout.ObjectField(m_interfaceList[i],typeof(GameObject));
  313. EditorGUILayout.EndHorizontal();
  314. }
  315. EditorGUILayout.Space();
  316. }
  317. #endregion
  318. #endregion
  319. #region 模版列表
  320. //是否折叠
  321. bool b_isFoldUseTemple = false;
  322. bool b_isfoldDebugInfo = false;
  323. string newUIName = "";
  324. string newInterfaceName = "";
  325. Vector2 scrollPos = new Vector2();
  326. //显示所有模板
  327. public void ShowAllTemple_GUI()
  328. {
  329. if (Selection.GetFiltered(typeof(GameObject), SelectionMode.DeepAssets).Length > 0)
  330. {
  331. m_SelectedNode = Selection.GetFiltered(typeof(GameObject), SelectionMode.DeepAssets)[0] as GameObject;
  332. }
  333. else
  334. {
  335. m_SelectedNode = null;
  336. }
  337. EditorGUI.indentLevel = 1;
  338. b_isFoldUseTemple = EditorGUILayout.Foldout((b_isFoldUseTemple), "模板列表:");
  339. if (b_isFoldUseTemple)
  340. {
  341. scrollPos = EditorGUILayout.BeginScrollView(scrollPos,GUILayout.ExpandHeight(false));
  342. for (int i = 0; i < m_allTemplateName.Count; i++)
  343. {
  344. EditorGUI.indentLevel = 2;
  345. bool b_isFoldTmp = EditorGUILayout.Foldout(GetListIsFold(i), m_allTemplateName[i] + ":");
  346. SetListIsFold(i,b_isFoldTmp);
  347. if (GetListIsFold(i))
  348. {
  349. SingleTemplateInfo(m_allTemplateName[i]);
  350. }
  351. }
  352. EditorGUILayout.EndScrollView();
  353. EditorGUILayout.Space();
  354. }
  355. }
  356. int currentIndex = -1;
  357. bool GetListIsFold(int index)
  358. {
  359. return (currentIndex == index);
  360. }
  361. void SetListIsFold(int index,bool isFold)
  362. {
  363. if (isFold == true)
  364. {
  365. currentIndex = index;
  366. }
  367. if (isFold == false
  368. && index == currentIndex)
  369. {
  370. currentIndex = -1;
  371. }
  372. }
  373. void SingleTemplateInfo(string templateName)
  374. {
  375. EditorGUI.indentLevel = 3;
  376. EditorGUILayout.ObjectField("模板预设:",FindPrefabInTemplateRes(templateName), typeof(GameObject));
  377. GUILayout.BeginHorizontal();
  378. EditorGUILayout.Space();
  379. if (GUILayout.Button("创建此模板", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  380. {
  381. CreatUIByOneTemplate(templateName);
  382. }
  383. EditorGUILayout.Space();
  384. GUILayout.EndHorizontal();
  385. GUILayout.BeginHorizontal();
  386. EditorGUILayout.Space();
  387. if (GUILayout.Button("应用此模板", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  388. {
  389. ApplyOneTemplate(templateName);
  390. }
  391. EditorGUILayout.Space();
  392. GUILayout.EndHorizontal();
  393. GUILayout.BeginHorizontal();
  394. EditorGUILayout.Space();
  395. if (GUILayout.Button("删除此模板", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  396. {
  397. if (EditorUtility.DisplayDialog("警告", "该操作不可逆,确定删除该模板?", "是", "否"))
  398. {
  399. DeleteTemlate(templateName);
  400. }
  401. }
  402. EditorGUILayout.Space();
  403. GUILayout.EndHorizontal();
  404. }
  405. //当前选中的节点,将成为新UI的父节点
  406. GameObject go_SelectedObj;
  407. //用模板创建UI
  408. void CreatUIByOneTemplate(string templateName)
  409. {
  410. GameObject go_newUI = GameObject.Instantiate(FindPrefabInTemplateRes(templateName));
  411. if (m_SelectedNode != null)
  412. {
  413. go_newUI.transform.SetParent( m_SelectedNode.transform);
  414. }
  415. go_newUI.name = templateName;
  416. RectTransform rt = go_newUI.GetComponent<RectTransform>();
  417. rt.anchoredPosition3D = Vector3.zero;
  418. rt.sizeDelta = Vector2.zero;
  419. rt.localScale = Vector3.one;
  420. m_UITemplate = go_newUI;
  421. m_preUITemplate = FindPrefabInTemplateRes(templateName);
  422. }
  423. #endregion
  424. #region 模板替换部分
  425. public void ApplyAllUITemplate()
  426. {
  427. foreach (var oneTemplateName in m_allTemplateName)
  428. {
  429. ApplyOneTemplate(oneTemplateName);
  430. }
  431. }
  432. //应用一个模板到全局
  433. void ApplyOneTemplate(string templateName)
  434. {
  435. List<GameObject> allUsedUI = FindHadOneTemplateUI(templateName);
  436. foreach (var oneUI in allUsedUI)
  437. {
  438. Debug.Log(oneUI.name);
  439. ReplaceOneUI(oneUI, FindPrefabInTemplateRes(templateName));
  440. }
  441. }
  442. //替换单个UI
  443. void ReplaceOneUI(GameObject l_oldUIprefab, GameObject l_newTemplate)
  444. {
  445. GameObject UITmp = GameObject.Instantiate(l_oldUIprefab);
  446. RecursionNodeToReplaceTemplate(UITmp, l_newTemplate);
  447. PrefabUtility.ReplacePrefab(UITmp, l_oldUIprefab);
  448. GameObject.DestroyImmediate(UITmp, true);
  449. }
  450. //遍历所有子节点去替换模板
  451. void RecursionNodeToReplaceTemplate(GameObject l_oldNode, GameObject l_newTemplate)
  452. {
  453. foreach (Transform node in l_oldNode.transform)
  454. {
  455. RecursionNodeToReplaceTemplate(node.gameObject, l_newTemplate);
  456. if (node.name == l_newTemplate.name)
  457. {
  458. ReplaceTemplate(l_oldNode.transform, node.gameObject, l_newTemplate);
  459. }
  460. //else
  461. //{
  462. //}
  463. }
  464. }
  465. struct InterfaceReplaceStruct
  466. {
  467. public GameObject go;
  468. public int index;
  469. }
  470. Dictionary<string, InterfaceReplaceStruct> m_interfaceTmp;
  471. void ReplaceTemplate(Transform l_parent,GameObject l_oldTemplate, GameObject l_newTemplate)
  472. {
  473. m_interfaceTmp = new Dictionary<string, InterfaceReplaceStruct>();
  474. //先把旧UI下的接口移出来,存入表中
  475. RecursionNodeToReplaceInterfaceStepOne(l_oldTemplate.transform);
  476. //再把放入新模板的对应接口下
  477. //删除旧模板
  478. GameObject.DestroyImmediate(l_oldTemplate);
  479. //创建新模板
  480. GameObject l_templateTmp = GameObject.Instantiate(l_newTemplate);
  481. l_templateTmp.name = l_templateTmp.name.Replace("(Clone)","");
  482. l_templateTmp.transform.SetParent(l_parent);
  483. //最后换上接口
  484. RecursionNodeToReplaceInterfaceStepTwo(l_templateTmp.transform);
  485. }
  486. void RecursionNodeToReplaceInterfaceStepOne(Transform l_parent)
  487. {
  488. foreach (Transform node in l_parent)
  489. {
  490. if(node.name.Contains(c_InterfaceName))
  491. {
  492. if (!m_interfaceTmp.ContainsKey(node.name))
  493. {
  494. InterfaceReplaceStruct tmp = new InterfaceReplaceStruct();
  495. tmp.go = node.gameObject;
  496. tmp.index = node.transform.GetSiblingIndex();
  497. m_interfaceTmp.Add(node.name, tmp);
  498. }
  499. else
  500. {
  501. Debug.LogError(node.root.name + " : " + node.name + " 接口有重名!", node.gameObject);
  502. }
  503. node.SetParent(null);
  504. }
  505. else
  506. {
  507. RecursionNodeToReplaceInterfaceStepOne(node);
  508. }
  509. }
  510. }
  511. void RecursionNodeToReplaceInterfaceStepTwo(Transform l_parent)
  512. {
  513. foreach (Transform node in l_parent)
  514. {
  515. if (m_interfaceTmp.ContainsKey(node.name))
  516. {
  517. InterfaceReplaceStruct tmp = m_interfaceTmp[node.name];
  518. m_interfaceTmp.Remove(node.name);
  519. GameObject.DestroyImmediate(node.gameObject);
  520. tmp.go.transform.SetParent(l_parent);
  521. tmp.go.transform.SetSiblingIndex(tmp.index);
  522. }
  523. else
  524. {
  525. RecursionNodeToReplaceInterfaceStepTwo(node);
  526. }
  527. }
  528. }
  529. //获取应用某个模板的所有UI预设
  530. List<GameObject> FindHadOneTemplateUI(string templateName)
  531. {
  532. List<GameObject> findedUI = new List<GameObject>();
  533. string[] prefabNameSplit;
  534. foreach (var item in UIEditorWindow.allUIPrefab.Keys)
  535. {
  536. Debug.Log(item);
  537. if (UIEditorWindow.allUIPrefab[item] != null &&
  538. UIEditorWindow.allUIPrefab[item].name != "UIManager")
  539. {
  540. findedUI.Add(UIEditorWindow.allUIPrefab[item]);
  541. }
  542. }
  543. return findedUI;
  544. }
  545. #endregion
  546. #region 封装过的方法
  547. //获取Prefab
  548. GameObject FindPrefabInTemplateRes(string name)
  549. {
  550. name = "Assets/" + c_TemplateResPath + name;
  551. name += ".prefab";
  552. //Debug.Log(name);
  553. return AssetDatabase.LoadAssetAtPath(name, typeof(GameObject)) as GameObject;
  554. }
  555. //该模板是否已经存在
  556. bool HaveTheTemlate(string name)
  557. {
  558. return m_allTemplateName.Contains(name);
  559. }
  560. //获取一个选中的接口
  561. GameObject GetTemlateParant(GameObject l_node)
  562. {
  563. if (l_node.name.Contains(c_TemplateName))
  564. {
  565. return l_node;
  566. }
  567. else
  568. {
  569. if(l_node.transform.parent == null)
  570. {
  571. return null;
  572. }
  573. else
  574. {
  575. return GetTemlateParant(l_node.transform.parent.gameObject);
  576. }
  577. }
  578. }
  579. string GetTemplateName(string nodeName)
  580. {
  581. return nodeName;
  582. //return nodeName.Split('_')[1];
  583. }
  584. /// <summary>
  585. /// 获取接口列表
  586. /// </summary>
  587. /// <param name="l_node"></param>
  588. /// <returns></returns>
  589. List<GameObject> GetInterfaceList(GameObject l_node)
  590. {
  591. List<GameObject> result = new List<GameObject>();
  592. RecursionFindInterface(result, l_node.transform);
  593. return result;
  594. }
  595. void RecursionFindInterface(List<GameObject> interfaceList,Transform node)
  596. {
  597. foreach (Transform tr in node)
  598. {
  599. if (tr.name.Contains(c_InterfaceName))
  600. {
  601. interfaceList.Add(tr.gameObject);
  602. }
  603. RecursionFindInterface(interfaceList, tr);
  604. }
  605. }
  606. bool IsContainsInterfaceName(string newName)
  607. {
  608. foreach (GameObject tr in m_interfaceList)
  609. {
  610. if (tr.name == c_InterfaceName + "_" + newName)
  611. {
  612. return true;
  613. }
  614. }
  615. return false;
  616. }
  617. void FindAllUITemplate()
  618. {
  619. m_allTemplateName = new List<string>();
  620. FindTemplateName(Application.dataPath +"/"+ c_TemplateResPath);
  621. }
  622. public void FindTemplateName(string path)
  623. {
  624. if(Directory.Exists(path))
  625. {
  626. string[] allUIPrefabName = Directory.GetFiles(path);
  627. foreach (var item in allUIPrefabName)
  628. {
  629. if (item.EndsWith(".prefab"))
  630. {
  631. string configName = FileTool.RemoveExpandName(FileTool.GetFileNameByPath(item));
  632. m_allTemplateName.Add(configName);
  633. }
  634. }
  635. }
  636. }
  637. #endregion
  638. }