UIBase.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using UnityEngine.UI;
  6. public class UIBase : MonoBehaviour , UILifeCycleInterface
  7. {
  8. [HideInInspector]
  9. public Canvas m_canvas;
  10. #region 重载方法
  11. //当UI第一次打开时调用OnInit方法,调用时机在OnOpen之前
  12. public virtual void OnInit()
  13. {
  14. }
  15. protected virtual void OnUIDestroy()
  16. {
  17. }
  18. #endregion
  19. #region 继承方法
  20. private int m_UIID = -1;
  21. public int UIID
  22. {
  23. get { return m_UIID; }
  24. //set { m_UIID = value; }
  25. }
  26. public string UIEventKey
  27. {
  28. get { return UIName + "@" + m_UIID; }
  29. //set { m_UIID = value; }
  30. }
  31. string m_UIName = null;
  32. public string UIName
  33. {
  34. get
  35. {
  36. if (m_UIName == null)
  37. {
  38. m_UIName = name;
  39. }
  40. return m_UIName;
  41. }
  42. set
  43. {
  44. m_UIName = value;
  45. }
  46. }
  47. public void Init(string UIEventKey, int id)
  48. {
  49. if(UIEventKey != null)
  50. {
  51. UIName = null;
  52. UIName = UIEventKey + "_" + UIName;
  53. }
  54. m_UIID = id;
  55. m_canvas = GetComponent<Canvas>();
  56. CreateObjectTable();
  57. OnInit();
  58. }
  59. public void Dispose()
  60. {
  61. ClearGuideModel();
  62. RemoveAllListener();
  63. CleanItem();
  64. CleanModelShowCameraList();
  65. ClearLoadSprite();
  66. try
  67. {
  68. OnUIDestroy();
  69. }
  70. catch(Exception e)
  71. {
  72. Debug.LogError("UIBase Dispose Exception -> UIEventKey: " + UIEventKey + " Exception: " + e.ToString());
  73. }
  74. DisposeLifeComponent();
  75. }
  76. #region 获取对象
  77. public List<GameObject> m_objectList = new List<GameObject>();
  78. //生成对象表,便于快速获取对象,并忽略层级
  79. void CreateObjectTable()
  80. {
  81. if (m_objects == null)
  82. {
  83. m_objects = new Dictionary<string, GameObject>();
  84. }
  85. m_objects.Clear();
  86. m_images.Clear();
  87. m_Sprites.Clear();
  88. m_texts.Clear();
  89. m_textmeshs.Clear();
  90. m_buttons.Clear();
  91. m_scrollRects.Clear();
  92. m_reusingScrollRects.Clear();
  93. m_rawImages.Clear();
  94. m_rectTransforms.Clear();
  95. m_inputFields.Clear();
  96. m_Sliders.Clear();
  97. m_joySticks.Clear();
  98. m_joySticks_ro.Clear();
  99. m_longPressList.Clear();
  100. m_Canvas.Clear();
  101. for (int i = 0; i < m_objectList.Count; i++)
  102. {
  103. if (m_objectList[i] != null)
  104. {
  105. if (m_objects.ContainsKey(m_objectList[i].name))
  106. {
  107. Debug.LogError("CreateObjectTable ContainsKey ->" + m_objectList[i].name + "<-");
  108. }
  109. else
  110. {
  111. m_objects.Add(m_objectList[i].name, m_objectList[i]);
  112. }
  113. }
  114. else
  115. {
  116. Debug.LogWarning(name + " m_objectList[" + i + "] is Null !");
  117. }
  118. }
  119. }
  120. Dictionary<string, UIBase> m_uiBases = new Dictionary<string, UIBase>();
  121. Dictionary<string, GameObject> m_objects = null;
  122. Dictionary<string, Image> m_images = new Dictionary<string, Image>();
  123. Dictionary<string, Sprite> m_Sprites = new Dictionary<string, Sprite>();
  124. Dictionary<string, Text> m_texts = new Dictionary<string, Text>();
  125. Dictionary<string, TextMesh> m_textmeshs = new Dictionary<string, TextMesh>();
  126. Dictionary<string, Button> m_buttons = new Dictionary<string, Button>();
  127. Dictionary<string, ScrollRect> m_scrollRects = new Dictionary<string, ScrollRect>();
  128. Dictionary<string, ReusingScrollRect> m_reusingScrollRects = new Dictionary<string, ReusingScrollRect>();
  129. Dictionary<string, RawImage> m_rawImages = new Dictionary<string, RawImage>();
  130. Dictionary<string, RectTransform> m_rectTransforms = new Dictionary<string, RectTransform>();
  131. Dictionary<string, InputField> m_inputFields = new Dictionary<string, InputField>();
  132. Dictionary<string, Slider> m_Sliders = new Dictionary<string, Slider>();
  133. Dictionary<string, Canvas> m_Canvas = new Dictionary<string, Canvas>();
  134. Dictionary<string, Toggle> m_Toggle = new Dictionary<string, Toggle>();
  135. Dictionary<string, UGUIJoyStick> m_joySticks = new Dictionary<string, UGUIJoyStick>();
  136. Dictionary<string, UGUIJoyStickBase> m_joySticks_ro = new Dictionary<string, UGUIJoyStickBase>();
  137. Dictionary<string, LongPressAcceptor> m_longPressList = new Dictionary<string, LongPressAcceptor>();
  138. Dictionary<string, DragAcceptor> m_dragList = new Dictionary<string, DragAcceptor>();
  139. public bool HaveObject(string name)
  140. {
  141. bool has = false;
  142. has = m_objects.ContainsKey(name);
  143. return has;
  144. }
  145. public GameObject GetGameObject(string name)
  146. {
  147. if (m_objects == null)
  148. {
  149. CreateObjectTable();
  150. }
  151. if (m_objects.ContainsKey(name))
  152. {
  153. GameObject go = m_objects[name];
  154. if (go == null)
  155. {
  156. throw new Exception("UIWindowBase GetGameObject error: " + UIName + " m_objects[" + name + "] is null !!");
  157. }
  158. return go;
  159. }
  160. else
  161. {
  162. throw new Exception("UIWindowBase GetGameObject error: " + UIName + " dont find ->" + name + "<-");
  163. }
  164. }
  165. public RectTransform GetRectTransform(string name)
  166. {
  167. if (m_rectTransforms.ContainsKey(name))
  168. {
  169. return m_rectTransforms[name];
  170. }
  171. RectTransform tmp = GetGameObject(name).GetComponent<RectTransform>();
  172. if (tmp == null)
  173. {
  174. throw new Exception(m_EventNames + " GetRectTransform ->" + name + "<- is Null !");
  175. }
  176. m_rectTransforms.Add(name, tmp);
  177. return tmp;
  178. }
  179. public UIBase GetUIBase(string name)
  180. {
  181. if (m_uiBases.ContainsKey(name))
  182. {
  183. return m_uiBases[name];
  184. }
  185. UIBase tmp = GetGameObject(name).GetComponent<UIBase>();
  186. if (tmp == null)
  187. {
  188. throw new Exception(m_EventNames + " GetUIBase ->" + name + "<- is Null !");
  189. }
  190. m_uiBases.Add(name, tmp);
  191. return tmp;
  192. }
  193. public Sprite GetSprite(string name)
  194. {
  195. if (m_Sprites.ContainsKey(name))
  196. {
  197. return m_Sprites[name];
  198. }
  199. Sprite tmp = GetGameObject(name).GetComponent<Sprite>();
  200. if (tmp == null)
  201. {
  202. throw new Exception(m_EventNames + " GetImage ->" + name + "<- is Null !");
  203. }
  204. m_Sprites.Add(name, tmp);
  205. return tmp;
  206. }
  207. public Image GetImage(string name)
  208. {
  209. if (m_images.ContainsKey(name))
  210. {
  211. return m_images[name];
  212. }
  213. Image tmp = GetGameObject(name).GetComponent<Image>();
  214. if (tmp == null)
  215. {
  216. throw new Exception(m_EventNames + " GetImage ->" + name + "<- is Null !");
  217. }
  218. m_images.Add(name, tmp);
  219. return tmp;
  220. }
  221. public TextMesh GetTextMesh(string name)
  222. {
  223. if (m_textmeshs.ContainsKey(name))
  224. {
  225. return m_textmeshs[name];
  226. }
  227. TextMesh tmp = GetGameObject(name).GetComponent<TextMesh>();
  228. if (tmp == null)
  229. {
  230. throw new Exception(m_EventNames + " GetText ->" + name + "<- is Null !");
  231. }
  232. m_textmeshs.Add(name, tmp);
  233. return tmp;
  234. }
  235. public Text GetText(string name)
  236. {
  237. if (m_texts.ContainsKey(name))
  238. {
  239. return m_texts[name];
  240. }
  241. Text tmp = GetGameObject(name).GetComponent<Text>();
  242. if (tmp == null)
  243. {
  244. throw new Exception(m_EventNames + " GetText ->" + name + "<- is Null !");
  245. }
  246. m_texts.Add(name, tmp);
  247. return tmp;
  248. }
  249. public Toggle GetToggle(string name)
  250. {
  251. if (m_Toggle.ContainsKey(name))
  252. {
  253. return m_Toggle[name];
  254. }
  255. Toggle tmp = GetGameObject(name).GetComponent<Toggle>();
  256. if (tmp == null)
  257. {
  258. throw new Exception(m_EventNames + " GetText ->" + name + "<- is Null !");
  259. }
  260. m_Toggle.Add(name, tmp);
  261. return tmp;
  262. }
  263. public Button GetButton(string name)
  264. {
  265. if (m_buttons.ContainsKey(name))
  266. {
  267. return m_buttons[name];
  268. }
  269. Button tmp = GetGameObject(name).GetComponent<Button>();
  270. if (tmp == null)
  271. {
  272. throw new Exception(m_EventNames + " GetButton ->" + name + "<- is Null !");
  273. }
  274. m_buttons.Add(name, tmp);
  275. return tmp;
  276. }
  277. public InputField GetInputField(string name)
  278. {
  279. if (m_inputFields.ContainsKey(name))
  280. {
  281. return m_inputFields[name];
  282. }
  283. InputField tmp = GetGameObject(name).GetComponent<InputField>();
  284. if (tmp == null)
  285. {
  286. throw new Exception(m_EventNames + " GetInputField ->" + name + "<- is Null !");
  287. }
  288. m_inputFields.Add(name, tmp);
  289. return tmp;
  290. }
  291. public ScrollRect GetScrollRect(string name)
  292. {
  293. if (m_scrollRects.ContainsKey(name))
  294. {
  295. return m_scrollRects[name];
  296. }
  297. ScrollRect tmp = GetGameObject(name).GetComponent<ScrollRect>();
  298. if (tmp == null)
  299. {
  300. throw new Exception(m_EventNames + " GetScrollRect ->" + name + "<- is Null !");
  301. }
  302. m_scrollRects.Add(name, tmp);
  303. return tmp;
  304. }
  305. public RawImage GetRawImage(string name)
  306. {
  307. if (m_rawImages.ContainsKey(name))
  308. {
  309. return m_rawImages[name];
  310. }
  311. RawImage tmp = GetGameObject(name).GetComponent<RawImage>();
  312. if (tmp == null)
  313. {
  314. throw new Exception(m_EventNames + " GetRawImage ->" + name + "<- is Null !");
  315. }
  316. m_rawImages.Add(name, tmp);
  317. return tmp;
  318. }
  319. public Slider GetSlider(string name)
  320. {
  321. if (m_Sliders.ContainsKey(name))
  322. {
  323. return m_Sliders[name];
  324. }
  325. Slider tmp = GetGameObject(name).GetComponent<Slider>();
  326. if (tmp == null)
  327. {
  328. throw new Exception(m_EventNames + " GetSlider ->" + name + "<- is Null !");
  329. }
  330. m_Sliders.Add(name, tmp);
  331. return tmp;
  332. }
  333. public Canvas GetCanvas(string name)
  334. {
  335. if (m_Canvas.ContainsKey(name))
  336. {
  337. return m_Canvas[name];
  338. }
  339. Canvas tmp = GetGameObject(name).GetComponent<Canvas>();
  340. if (tmp == null)
  341. {
  342. throw new Exception(m_EventNames + " GetSlider ->" + name + "<- is Null !");
  343. }
  344. m_Canvas.Add(name, tmp);
  345. return tmp;
  346. }
  347. public Vector3 GetPosition(string name, bool islocal)
  348. {
  349. Vector3 tmp = Vector3.zero;
  350. GameObject go = GetGameObject(name);
  351. if (go != null)
  352. {
  353. if (islocal)
  354. tmp = GetGameObject(name).transform.localPosition;
  355. else
  356. tmp = GetGameObject(name).transform.position;
  357. }
  358. return tmp;
  359. }
  360. private RectTransform m_rectTransform;
  361. public RectTransform RectTransform
  362. {
  363. get
  364. {
  365. if (m_rectTransform == null)
  366. {
  367. m_rectTransform = GetComponent<RectTransform>();
  368. }
  369. return m_rectTransform;
  370. }
  371. set { m_rectTransform = value; }
  372. }
  373. public void SetSizeDelta(float w, float h)
  374. {
  375. RectTransform.sizeDelta = new Vector2(w, h);
  376. }
  377. #region 自定义组件
  378. public ReusingScrollRect GetReusingScrollRect(string name)
  379. {
  380. if (m_reusingScrollRects.ContainsKey(name))
  381. {
  382. return m_reusingScrollRects[name];
  383. }
  384. ReusingScrollRect tmp = GetGameObject(name).GetComponent<ReusingScrollRect>();
  385. if (tmp == null)
  386. {
  387. throw new Exception(m_EventNames + " GetReusingScrollRect ->" + name + "<- is Null !");
  388. }
  389. m_reusingScrollRects.Add(name, tmp);
  390. return tmp;
  391. }
  392. public UGUIJoyStick GetJoyStick(string name)
  393. {
  394. if (m_joySticks.ContainsKey(name))
  395. {
  396. return m_joySticks[name];
  397. }
  398. UGUIJoyStick tmp = GetGameObject(name).GetComponent<UGUIJoyStick>();
  399. if (tmp == null)
  400. {
  401. throw new Exception(m_EventNames + " GetJoyStick ->" + name + "<- is Null !");
  402. }
  403. m_joySticks.Add(name, tmp);
  404. return tmp;
  405. }
  406. public UGUIJoyStickBase GetJoyStick_ro(string name)
  407. {
  408. if (m_joySticks_ro.ContainsKey(name))
  409. {
  410. return m_joySticks_ro[name];
  411. }
  412. UGUIJoyStickBase tmp = GetGameObject(name).GetComponent<UGUIJoyStickBase>();
  413. if (tmp == null)
  414. {
  415. throw new Exception(m_EventNames + " GetJoyStick_ro ->" + name + "<- is Null !");
  416. }
  417. m_joySticks_ro.Add(name, tmp);
  418. return tmp;
  419. }
  420. public LongPressAcceptor GetLongPressComp(string name)
  421. {
  422. if (m_longPressList.ContainsKey(name))
  423. {
  424. return m_longPressList[name];
  425. }
  426. LongPressAcceptor tmp = GetGameObject(name).GetComponent<LongPressAcceptor>();
  427. if (tmp == null)
  428. {
  429. throw new Exception(m_EventNames + " GetLongPressComp ->" + name + "<- is Null !");
  430. }
  431. m_longPressList.Add(name, tmp);
  432. return tmp;
  433. }
  434. public DragAcceptor GetDragComp(string name)
  435. {
  436. if (m_dragList.ContainsKey(name))
  437. {
  438. return m_dragList[name];
  439. }
  440. DragAcceptor tmp = GetGameObject(name).GetComponent<DragAcceptor>();
  441. if (tmp == null)
  442. {
  443. throw new Exception(m_EventNames + " GetDragComp ->" + name + "<- is Null !");
  444. }
  445. m_dragList.Add(name, tmp);
  446. return tmp;
  447. }
  448. #endregion
  449. #endregion
  450. #region 注册监听
  451. protected List<Enum> m_EventNames = new List<Enum>();
  452. protected List<EventHandRegisterInfo> m_EventListeners = new List<EventHandRegisterInfo>();
  453. protected List<InputEventRegisterInfo> m_OnClickEvents = new List<InputEventRegisterInfo>();
  454. protected List<InputEventRegisterInfo> m_LongPressEvents = new List<InputEventRegisterInfo>();
  455. protected List<InputEventRegisterInfo> m_DragEvents = new List<InputEventRegisterInfo>();
  456. protected List<InputEventRegisterInfo> m_BeginDragEvents = new List<InputEventRegisterInfo>();
  457. protected List<InputEventRegisterInfo> m_EndDragEvents = new List<InputEventRegisterInfo>();
  458. public virtual void RemoveAllListener()
  459. {
  460. for (int i = 0; i < m_EventListeners.Count; i++)
  461. {
  462. m_EventListeners[i].RemoveListener();
  463. }
  464. m_EventListeners.Clear();
  465. for (int i = 0; i < m_OnClickEvents.Count; i++)
  466. {
  467. m_OnClickEvents[i].RemoveListener();
  468. }
  469. m_OnClickEvents.Clear();
  470. for (int i = 0; i < m_LongPressEvents.Count; i++)
  471. {
  472. m_LongPressEvents[i].RemoveListener();
  473. }
  474. m_LongPressEvents.Clear();
  475. #region 拖动事件
  476. for (int i = 0; i < m_DragEvents.Count; i++)
  477. {
  478. m_DragEvents[i].RemoveListener();
  479. }
  480. m_DragEvents.Clear();
  481. for (int i = 0; i < m_BeginDragEvents.Count; i++)
  482. {
  483. m_BeginDragEvents[i].RemoveListener();
  484. }
  485. m_BeginDragEvents.Clear();
  486. for (int i = 0; i < m_EndDragEvents.Count; i++)
  487. {
  488. m_EndDragEvents[i].RemoveListener();
  489. }
  490. m_EndDragEvents.Clear();
  491. #endregion
  492. }
  493. #region 添加监听
  494. bool GetRegister(List<InputEventRegisterInfo> list, string eventKey)
  495. {
  496. int registerCount = 0;
  497. for (int i = 0; i < list.Count; i++)
  498. {
  499. if (list[i].eventKey == eventKey)
  500. {
  501. registerCount++;
  502. }
  503. }
  504. return registerCount == 0;
  505. }
  506. public void AddOnClickListener(string buttonName, InputEventHandle<InputUIOnClickEvent> callback, string parm = null)
  507. {
  508. InputButtonClickRegisterInfo info = InputUIEventProxy.GetOnClickListener(GetButton(buttonName), UIEventKey, buttonName, parm, callback);
  509. info.AddListener();
  510. m_OnClickEvents.Add(info);
  511. }
  512. public void AddOnClickListenerByCreate(Button button, string compName, InputEventHandle<InputUIOnClickEvent> callback, string parm = null)
  513. {
  514. InputButtonClickRegisterInfo info = InputUIEventProxy.GetOnClickListener(button, UIEventKey, compName, parm, callback);
  515. info.AddListener();
  516. m_OnClickEvents.Add(info);
  517. }
  518. public void AddLongPressListener(string compName, InputEventHandle<InputUILongPressEvent> callback, string parm = null)
  519. {
  520. InputEventRegisterInfo<InputUILongPressEvent> info = InputUIEventProxy.GetLongPressListener(GetLongPressComp(compName), UIEventKey, compName, parm, callback);
  521. info.AddListener();
  522. m_LongPressEvents.Add(info);
  523. }
  524. public void AddBeginDragListener(string compName, InputEventHandle<InputUIOnBeginDragEvent> callback, string parm = null)
  525. {
  526. InputEventRegisterInfo<InputUIOnBeginDragEvent> info = InputUIEventProxy.GetOnBeginDragListener(GetDragComp(compName), UIEventKey, compName, parm, callback);
  527. info.AddListener();
  528. m_BeginDragEvents.Add(info);
  529. }
  530. public void AddEndDragListener(string compName, InputEventHandle<InputUIOnEndDragEvent> callback, string parm = null)
  531. {
  532. InputEventRegisterInfo<InputUIOnEndDragEvent> info = InputUIEventProxy.GetOnEndDragListener(GetDragComp(compName), UIEventKey, compName, parm, callback);
  533. info.AddListener();
  534. m_EndDragEvents.Add(info);
  535. }
  536. public void AddEventListener(Enum EventEnum, EventHandle handle)
  537. {
  538. EventHandRegisterInfo info = new EventHandRegisterInfo();
  539. info.m_EventKey = EventEnum;
  540. info.m_hande = handle;
  541. GlobalEvent.AddEvent(EventEnum, handle);
  542. m_EventListeners.Add(info);
  543. }
  544. #endregion
  545. #region 移除监听
  546. //TODO 逐步添加所有的移除监听方法
  547. public InputButtonClickRegisterInfo GetClickRegisterInfo(string buttonName, InputEventHandle<InputUIOnClickEvent> callback, string parm)
  548. {
  549. string eventKey = InputUIOnClickEvent.GetEventKey(UIEventKey, buttonName, parm);
  550. for (int i = 0; i < m_OnClickEvents.Count; i++)
  551. {
  552. InputButtonClickRegisterInfo info = (InputButtonClickRegisterInfo)m_OnClickEvents[i];
  553. if (info.eventKey == eventKey
  554. && info.callBack == callback)
  555. {
  556. return info;
  557. }
  558. }
  559. throw new Exception("GetClickRegisterInfo Exception not find RegisterInfo by " + buttonName + " parm " + parm);
  560. }
  561. public void RemoveOnClickListener(string buttonName, InputEventHandle<InputUIOnClickEvent> callback, string parm = null)
  562. {
  563. InputButtonClickRegisterInfo info = GetClickRegisterInfo(buttonName, callback, parm);
  564. m_OnClickEvents.Remove(info);
  565. info.RemoveListener();
  566. }
  567. public void RemoveLongPressListener(string compName, InputEventHandle<InputUILongPressEvent> callback, string parm = null)
  568. {
  569. InputEventRegisterInfo<InputUILongPressEvent> info = GetLongPressRegisterInfo(compName, callback, parm);
  570. m_LongPressEvents.Remove(info);
  571. info.RemoveListener();
  572. }
  573. public InputEventRegisterInfo<InputUILongPressEvent> GetLongPressRegisterInfo(string compName, InputEventHandle<InputUILongPressEvent> callback, string parm)
  574. {
  575. string eventKey = InputUILongPressEvent.GetEventKey(UIName, compName, parm);
  576. for (int i = 0; i < m_LongPressEvents.Count; i++)
  577. {
  578. InputEventRegisterInfo<InputUILongPressEvent> info = (InputEventRegisterInfo<InputUILongPressEvent>)m_LongPressEvents[i];
  579. if (info.eventKey == eventKey
  580. && info.callBack == callback)
  581. {
  582. return info;
  583. }
  584. }
  585. throw new Exception("GetLongPressRegisterInfo Exception not find RegisterInfo by " + compName + " parm " + parm);
  586. }
  587. #endregion
  588. #endregion
  589. #region 创建对象
  590. protected List<UIBase> m_ChildList = new List<UIBase>();
  591. int m_childUIIndex = 0;
  592. public UIBase CreateItem(string itemName, GameObject parent, bool isActive)
  593. {
  594. GameObject item = GameObjectManager.CreateGameObjectByPool(itemName, parent, isActive);
  595. return SetItem(item);
  596. }
  597. public UIBase CreateItem(string itemName, string prantName, bool isActive)
  598. {
  599. GameObject parent = GetGameObject(prantName);
  600. return CreateItem(itemName,parent,isActive);
  601. }
  602. public UIBase CreateItem(GameObject itemObj, GameObject parent, bool isActive)
  603. {
  604. GameObject item = GameObjectManager.CreateGameObjectByPool(itemObj, parent, isActive);
  605. return SetItem(item);
  606. }
  607. public UIBase CreateItem(string itemName, string prantName)
  608. {
  609. return CreateItem(itemName, prantName, true);
  610. }
  611. private UIBase SetItem(GameObject item)
  612. {
  613. item.transform.localScale = Vector3.one;
  614. item.transform.localPosition = Vector3.zero;
  615. UIBase UIItem = item.GetComponent<UIBase>();
  616. if (UIItem == null)
  617. {
  618. throw new Exception("CreateItem Error : ->" + item.name + "<- don't have UIBase Component!");
  619. }
  620. UIItem.Init(UIEventKey, m_childUIIndex++);
  621. UIItem.UIName = UIEventKey + "_" + UIItem.UIName;
  622. m_ChildList.Add(UIItem);
  623. return UIItem;
  624. }
  625. public void DestroyItem(UIBase item)
  626. {
  627. DestroyItem(item, true);
  628. }
  629. public void DestroyItem(UIBase item, bool isActive)
  630. {
  631. if (m_ChildList.Contains(item))
  632. {
  633. m_ChildList.Remove(item);
  634. item.Dispose();
  635. GameObjectManager.DestroyGameObjectByPool(item.gameObject, isActive);
  636. }
  637. }
  638. public void DestroyItem(UIBase item, float t)
  639. {
  640. if (m_ChildList.Contains(item))
  641. {
  642. m_ChildList.Remove(item);
  643. item.Dispose();
  644. GameObjectManager.DestroyGameObjectByPool(item.gameObject, t);
  645. }
  646. }
  647. public void CleanItem()
  648. {
  649. CleanItem(true);
  650. }
  651. public void CleanItem(bool isActive)
  652. {
  653. for (int i = 0; i < m_ChildList.Count; i++)
  654. {
  655. try
  656. {
  657. m_ChildList[i].Dispose();
  658. GameObjectManager.DestroyGameObjectByPool(m_ChildList[i].gameObject, isActive);
  659. }
  660. catch (Exception e)
  661. {
  662. Debug.LogError("CleanItem Error! UIName " + UIName + " Exception :" + e);
  663. }
  664. }
  665. m_ChildList.Clear();
  666. m_childUIIndex = 0;
  667. }
  668. public UIBase GetItemByIndex(string itemName, int index)
  669. {
  670. for (int i = 0; i < m_ChildList.Count; i++)
  671. {
  672. if (m_ChildList[i].name == itemName)
  673. {
  674. //Debug.Log("GetItemByIndex " + index, m_ChildList[i]);
  675. index--;
  676. if (index == 0)
  677. {
  678. return m_ChildList[i];
  679. }
  680. }
  681. }
  682. throw new Exception(UIName + " GetItem Exception Dont find Item: " + itemName);
  683. }
  684. public UIBase GetItemByKey(string uiEvenyKey)
  685. {
  686. for (int i = 0; i < m_ChildList.Count; i++)
  687. {
  688. if (m_ChildList[i].UIEventKey == uiEvenyKey)
  689. {
  690. return m_ChildList[i];
  691. }
  692. }
  693. throw new Exception(UIName + " GetItemByKey Exception Dont find Item: " + uiEvenyKey);
  694. }
  695. public bool GetItemIsExist(string itemName)
  696. {
  697. for (int i = 0; i < m_ChildList.Count; i++)
  698. {
  699. if (m_ChildList[i].name == itemName)
  700. {
  701. return true;
  702. }
  703. }
  704. return false;
  705. }
  706. #endregion
  707. #endregion
  708. #region 赋值方法
  709. public void SetText(string TextID, string content)
  710. {
  711. GetText(TextID).text = content.Replace("\\n", "\n");
  712. }
  713. public void SetImageColor(string ImageID, Color color)
  714. {
  715. GetImage(ImageID).color = color;
  716. }
  717. public void SetImageFillAmount(string ImageID, float value)
  718. {
  719. GetImage(ImageID).fillAmount = value;
  720. }
  721. public void SetTextColor(string TextID, Color color)
  722. {
  723. GetText(TextID).color = color;
  724. }
  725. public void SetImageAlpha(string ImageID, float alpha)
  726. {
  727. Color col = GetImage(ImageID).color;
  728. col.a = alpha;
  729. GetImage(ImageID).color = col;
  730. }
  731. public void SetInputText(string TextID, string content)
  732. {
  733. GetInputField(TextID).text = content;
  734. }
  735. /// <summary>
  736. /// 不再建议使用
  737. /// </summary>
  738. [Obsolete]
  739. public void SetTextByLangeage(string textID, string contentID, params object[] objs)
  740. {
  741. GetText(textID).text = LanguageManager.GetContent(LanguageManager.c_defaultModuleKey, contentID, objs);
  742. }
  743. void SetTextStyle(string textID, string work)
  744. {
  745. }
  746. public void SetTextByLangeage(string textID, string moduleName, string contentID, params object[] objs)
  747. {
  748. GetText(textID).text = LanguageManager.GetContent(moduleName, contentID, objs);
  749. }
  750. public void SetTextByLanguagePath(string textID, string languagePath, params object[] objs)
  751. {
  752. GetText(textID).text = LanguageManager.GetContentByKey(languagePath, objs);
  753. }
  754. public void SetSlider(string sliderID, float value)
  755. {
  756. GetSlider(sliderID).value = value;
  757. }
  758. public void SetActive(string gameObjectID, bool isShow)
  759. {
  760. GetGameObject(gameObjectID).SetActive(isShow);
  761. }
  762. /// <summary>
  763. /// Only Button
  764. /// </summary>
  765. public void SetEnabeled(string ID, bool enable)
  766. {
  767. GetButton(ID).enabled = enable;
  768. }
  769. /// <summary>
  770. /// Only Button
  771. /// </summary>
  772. public void SetButtonInteractable(string ID, bool enable)
  773. {
  774. GetButton(ID).interactable = enable;
  775. }
  776. public void SetRectWidth(string TextID, float value, float height)
  777. {
  778. GetRectTransform(TextID).sizeDelta = Vector2.right * -value * 2 + Vector2.up * height;
  779. }
  780. public void SetWidth(string TextID, float width, float height)
  781. {
  782. GetRectTransform(TextID).sizeDelta = Vector2.right * width + Vector2.up * height;
  783. }
  784. public void SetPosition(string TextID, float x, float y, float z, bool islocal)
  785. {
  786. if (islocal)
  787. GetRectTransform(TextID).localPosition = Vector3.right * x + Vector3.up * y + Vector3.forward * z;
  788. else
  789. GetRectTransform(TextID).position = Vector3.right * x + Vector3.up * y + Vector3.forward * z;
  790. }
  791. public void SetAnchoredPosition(string ID,float x, float y)
  792. {
  793. GetRectTransform(ID).anchoredPosition = Vector2.right * x + Vector2.up * y;
  794. }
  795. public void SetScale(string TextID, float x, float y, float z)
  796. {
  797. GetGameObject(TextID).transform.localScale = Vector3.right * x + Vector3.up * y + Vector3.forward * z;
  798. }
  799. public void SetMeshText(string TextID, string txt)
  800. {
  801. GetTextMesh(TextID).text = txt;
  802. }
  803. #endregion
  804. #region 动态加载Sprite赋值
  805. private Dictionary<string, int> loadSpriteNames = new Dictionary<string, int>();
  806. public void SetImageSprite(Image img, string name, bool is_nativesize = false)
  807. {
  808. if(ResourceManager.GetResourceIsExist(name))
  809. {
  810. UGUITool.SetImageSprite(img, name, is_nativesize);
  811. if (!loadSpriteNames.ContainsKey(name))
  812. loadSpriteNames.Add(name, 1);
  813. else
  814. loadSpriteNames[name]++;
  815. }
  816. else
  817. {
  818. Debug.LogError("SetImageSprite 资源不存在! ->" + name + "<-");
  819. }
  820. }
  821. private void ClearLoadSprite()
  822. {
  823. foreach (var item in loadSpriteNames)
  824. {
  825. int num = item.Value;
  826. //Debug.Log("UIBase 回收图片:" + item.Key + ":" + num);
  827. AssetsPoolManager.DestroyByPool(item.Key,num);
  828. }
  829. loadSpriteNames.Clear();
  830. }
  831. #endregion
  832. #region RawImageCamera
  833. List<UIModelShowTool.UIModelShowData> modelList = new List<UIModelShowTool.UIModelShowData>();
  834. public UIModelShowTool.UIModelShowData SetRawImageByModelShowCamera(string rawimageName, string modelName,
  835. string layerName = null,
  836. bool? orthographic = null,
  837. float? orthographicSize = null,
  838. Color? backgroundColor = null,
  839. Vector3? localPosition = null ,
  840. Vector3? localScale = null,
  841. Vector3? eulerAngles = null ,
  842. Vector3? texSize = null,
  843. float? nearClippingPlane = null,
  844. float? farClippingPlane = null)
  845. {
  846. var model = CreateModelShow(modelName, layerName, orthographic, orthographicSize, backgroundColor, localPosition, localScale, eulerAngles, texSize, nearClippingPlane, farClippingPlane);
  847. GetRawImage(rawimageName).texture = model.renderTexture;
  848. return model;
  849. }
  850. public void CleanModelShowCameraList()
  851. {
  852. for (int i = 0; i < modelList.Count; i++)
  853. {
  854. UIModelShowTool.DisposeModelShow(modelList[i]);
  855. }
  856. modelList.Clear();
  857. }
  858. public UIModelShowTool.UIModelShowData CreateModelShow(string modelName,
  859. string layerName = null,
  860. bool? orthographic = null,
  861. float? orthographicSize = null,
  862. Color? backgroundColor = null,
  863. Vector3? localPosition = null,
  864. Vector3? localScale = null,
  865. Vector3? eulerAngles = null,
  866. Vector3? texSize = null,
  867. float? nearClippingPlane = null,
  868. float? farClippingPlane = null)
  869. {
  870. var model = UIModelShowTool.CreateModelData(modelName, layerName, orthographic, orthographicSize, backgroundColor, localPosition, eulerAngles, localScale, texSize, nearClippingPlane, farClippingPlane);
  871. modelList.Add(model);
  872. return model;
  873. }
  874. public void RemoveModelShowCamera(UIModelShowTool.UIModelShowData data)
  875. {
  876. modelList.Remove(data);
  877. UIModelShowTool.DisposeModelShow(data);
  878. }
  879. #endregion
  880. #region 生命周期管理
  881. protected List<UILifeCycleInterface> m_lifeComponent = new List<UILifeCycleInterface>();
  882. public void AddLifeCycleComponent(UILifeCycleInterface comp)
  883. {
  884. comp.Init(UIEventKey, m_lifeComponent.Count);
  885. m_lifeComponent.Add(comp);
  886. }
  887. void DisposeLifeComponent()
  888. {
  889. for (int i = 0; i < m_lifeComponent.Count; i++)
  890. {
  891. try
  892. {
  893. m_lifeComponent[i].Dispose();
  894. }
  895. catch( Exception e)
  896. {
  897. Debug.LogError("UIBase DisposeLifeComponent Exception -> UIEventKey: " + UIEventKey + " Exception: " + e.ToString());
  898. }
  899. }
  900. m_lifeComponent.Clear();
  901. }
  902. #endregion
  903. #region 新手引导使用
  904. protected Dictionary<GameObject, GuideHeightLightComponent> m_CreateCanvasDict = new Dictionary<GameObject, GuideHeightLightComponent>(); //保存Canvas的创建状态
  905. public List<GameObject> GetHeightLightObjects()
  906. {
  907. return new List<GameObject>(m_CreateCanvasDict.Keys);
  908. }
  909. public void SetGuideMode(string objName, int order = 1)
  910. {
  911. SetGuideMode(GetGuideFixGameObject(objName), order);
  912. }
  913. /// <summary>
  914. /// 获取新手引导的固定GameObject(当找固定Item里子节点时使用格式 PetItem1.Use)
  915. /// </summary>
  916. /// <param name="objName"></param>
  917. /// <returns></returns>
  918. public GameObject GetGuideFixGameObject(string objName)
  919. {
  920. GameObject obj = null;
  921. if (objName.Contains("."))
  922. {
  923. string[] names = objName.Split('.');
  924. UIBase item = GetGameObject(names[0]).GetComponent<UIBase>();
  925. for (int i = 1; i < names.Length; i++)
  926. {
  927. string temp = names[i];
  928. GameObject tempObj = item.GetGameObject(temp);
  929. if (i == names.Length - 1)
  930. obj = tempObj;
  931. else
  932. item = tempObj.GetComponent<UIBase>();
  933. }
  934. }
  935. else
  936. {
  937. obj = GetGameObject(objName);
  938. }
  939. return obj;
  940. }
  941. /// <summary>
  942. /// 新手引导获得动态创建Item 格式为:PetItem[0].Use(PetItem的Item上挂有UIBase脚本, [0] 该名字的第几个Item,Use:拖到PetItem上的GameObject)
  943. /// </summary>
  944. /// <param name="itemName"></param>
  945. /// <returns></returns>
  946. public GameObject GetGuideDynamicCreateItem(string itemName)
  947. {
  948. if(string.IsNullOrEmpty(itemName))
  949. {
  950. Debug.LogError("GetGuideDynamicCreateItem itemName is Null!! ");
  951. return null;
  952. }
  953. string firstName = "";
  954. string[] strArr = itemName.Split('.');
  955. Debug.Log("GetGuideDynamicCreateItem.itemName :" + itemName);
  956. string childName = "";
  957. GameObject obj = null;
  958. if (strArr.Length > 0)
  959. {
  960. UIBase uIBase = null;
  961. firstName = strArr[0];
  962. int index = int.Parse(firstName.SplitExtend("[", "]")[0]);
  963. int tempIndex0 = firstName.IndexOf("[");
  964. firstName = firstName.Replace(firstName.Substring(tempIndex0), "");
  965. Debug.Log("UIBase : Index :" + index + " firstName :" + firstName + " m_ChildList:"+ m_ChildList.Count);
  966. int tempIndex = 0;
  967. for (int i = 0; i < m_ChildList.Count; i++)
  968. {
  969. UIBase cItem = m_ChildList[i];
  970. Debug.Log("Item:" + cItem);
  971. if (cItem.name == firstName)
  972. {
  973. if (index == tempIndex)
  974. {
  975. uIBase = cItem;
  976. obj = uIBase.gameObject;
  977. break;
  978. }
  979. tempIndex++;
  980. }
  981. }
  982. if (strArr.Length > 1)
  983. {
  984. childName = strArr[1];
  985. Debug.Log("childName:" + childName);
  986. if (childName.Contains("["))
  987. {
  988. childName = itemName.Replace(strArr[0] + ".", "");
  989. Debug.Log("childName:" + childName);
  990. obj = uIBase.GetGuideDynamicCreateItem(childName);
  991. }
  992. else
  993. {
  994. string afterNames= itemName.Replace(strArr[0] + ".", "");
  995. strArr = afterNames.Split('.');
  996. Debug.Log("afterNames :" + afterNames + " UIBase:" + GetType().Name);
  997. for (int i = 0; i < strArr.Length; i++)
  998. {
  999. string findName = strArr[i];
  1000. obj = uIBase.GetGameObject(findName);
  1001. if (i < strArr[i].Length - 1)
  1002. {
  1003. uIBase = obj.GetComponent<UIBase>();
  1004. }
  1005. }
  1006. }
  1007. }
  1008. }
  1009. if(obj == null)
  1010. {
  1011. Debug.LogError("GetGuideDynamicCreateItem error :UIEventKey " + UIEventKey + "itemName " + itemName);
  1012. }
  1013. return obj;
  1014. }
  1015. public void SetItemGuideMode(string itemName, int order = 1)
  1016. {
  1017. SetGuideMode(GetGuideDynamicCreateItem(itemName), order);
  1018. }
  1019. public void SetItemGuideModeByIndex(string itemName, int index, int order = 1)
  1020. {
  1021. SetGuideMode(GetItemByIndex(itemName, index).gameObject, order);
  1022. }
  1023. public void SetSelfGuideMode(int order = 1)
  1024. {
  1025. SetGuideMode(gameObject, order);
  1026. }
  1027. public GuideHeightLightComponent SetGuideMode(GameObject go, int order = 1)
  1028. {
  1029. GuideHeightLightComponent guideHeightLight = null;
  1030. if (!m_CreateCanvasDict.ContainsKey(go))
  1031. {
  1032. guideHeightLight = go.AddComponent<GuideHeightLightComponent>();
  1033. guideHeightLight.order = order;
  1034. m_CreateCanvasDict.Add(go, guideHeightLight);
  1035. }
  1036. return guideHeightLight;
  1037. }
  1038. public void CancelGuideModel(GameObject go)
  1039. {
  1040. if (go == null)
  1041. {
  1042. Debug.LogError("go is null");
  1043. return;
  1044. }
  1045. if (m_CreateCanvasDict.ContainsKey(go))
  1046. {
  1047. GuideHeightLightComponent guideHeightLight = m_CreateCanvasDict[go];
  1048. guideHeightLight.ClearGuide();
  1049. Destroy(guideHeightLight);
  1050. m_CreateCanvasDict.Remove(go);
  1051. //Debug.Log("ClearGuide______________");
  1052. }
  1053. }
  1054. public void ClearGuideModel()
  1055. {
  1056. List<GameObject> m_GuideList = new List<GameObject>(m_CreateCanvasDict.Keys);
  1057. for (int i = 0; i < m_GuideList.Count; i++)
  1058. {
  1059. CancelGuideModel(m_GuideList[i]);
  1060. }
  1061. for (int i = 0; i < m_ChildList.Count; i++)
  1062. {
  1063. m_ChildList[i].ClearGuideModel();
  1064. }
  1065. m_GuideList.Clear();
  1066. m_CreateCanvasDict.Clear();
  1067. }
  1068. #endregion
  1069. #region 工具方法
  1070. [ContextMenu("ObjectList 去重")]
  1071. public void ClearObject()
  1072. {
  1073. List<GameObject> ls = new List<GameObject>();
  1074. int len = m_objectList.Count;
  1075. for (int i = 0; i < len; i++)
  1076. {
  1077. GameObject go = m_objectList[i];
  1078. if (go != null)
  1079. {
  1080. if (!ls.Contains(go)) ls.Add(go);
  1081. }
  1082. }
  1083. ls.Sort((a, b) =>
  1084. {
  1085. return a.name.CompareTo(b.name);
  1086. });
  1087. m_objectList = ls;
  1088. }
  1089. //将世界坐标转换为 UI 坐标系中的位置
  1090. public Vector3 WorldPosToUIPos(Vector3 worldPos, string cameraKey)
  1091. {
  1092. Vector3 scale = UIManager.UILayerManager.GetUICameraDataByKey(cameraKey).m_root.GetComponent<RectTransform>().localScale;
  1093. Vector3 UIPos = new Vector3(worldPos.x / scale.x, worldPos.y / scale.y, worldPos.z / scale.z);
  1094. return UIPos;
  1095. }
  1096. //将 UI 坐标系中的位置 转换为 世界坐标
  1097. public Vector3 UIPosToWorldPos(Vector3 UIPos, string cameraKey)
  1098. {
  1099. Vector3 scale = UIManager.UILayerManager.GetUICameraDataByKey(cameraKey).m_root.GetComponent<RectTransform>().localScale;
  1100. Vector3 worldPos = new Vector3(UIPos.x * scale.x, UIPos.y * scale.y, UIPos.z * scale.z);
  1101. return worldPos;
  1102. }
  1103. #endregion
  1104. }