UIBase.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LuaInterface;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public enum UI_ANIM {
  9. NO,
  10. MOVE_IN,
  11. MOVE_OUT,
  12. SCALE_IN,
  13. SCALE_OUT,
  14. }
  15. public abstract class UIBase : MonoBase {
  16. public delegate UIBase UIPageCreator ();
  17. public delegate void PointListener (GameObject point);
  18. public delegate void PointListenerNoParam ();
  19. public delegate void ButtonListener (Button btn);
  20. public delegate void ButtonListenerWithParam (Button btn, params object[] param);
  21. public delegate void ToggleListener (bool b);
  22. public delegate void ToggleListenerWithParams (Toggle tog, params object[] param);
  23. public delegate void DialogCallback (int btn);
  24. [HideInInspector]
  25. public int ParentId {
  26. set { m_parentId = value; }
  27. get { return m_parentId; }
  28. }
  29. [SerializeField]
  30. private int m_parentId;
  31. [HideInInspector]
  32. public bool NeedParentKeep {
  33. set { m_needParentKeep = value; }
  34. get { return m_needParentKeep; }
  35. }
  36. [SerializeField]
  37. private bool m_needParentKeep;
  38. private bool mIsBattleMainPage = false;
  39. [HideInInspector]
  40. public bool IsBattleMainPage
  41. {
  42. get { return mIsBattleMainPage; }
  43. set { mIsBattleMainPage = value; }
  44. }
  45. private bool mIsClosed = false;
  46. #region fields
  47. private static int s_sequence = 0;
  48. private bool bInited = false;
  49. public bool Inited
  50. {
  51. get { return bInited; }
  52. }
  53. Canvas[] mChildCanvasList = null;
  54. private Canvas mCanvas = null;
  55. protected CanvasGroup mCanvasGroup = null;
  56. private GameObject mRootPage = null;
  57. private Transform mPageBodyTrans = null;
  58. private GameObject mPageBody = null;
  59. private int mPageId = 0;
  60. public UIAnimationListener animListener;
  61. private UIAnimationEvent[] mAnimtionEvents;
  62. private Dictionary<string, GameObject> mChildGoes = null;
  63. public int PageId {
  64. get { return mPageId; }
  65. set { mPageId = value; }
  66. }
  67. private int mPageUniqueID = 0;
  68. public int PageUniqueID {
  69. get { return mPageUniqueID; }
  70. }
  71. private string mPageName;
  72. public string MPageName {
  73. get { return mPageName; }
  74. set { mPageName = value; }
  75. }
  76. private int mSourceUIID;
  77. public int MSourceUIID {
  78. get { return mSourceUIID; }
  79. set { mSourceUIID = value; }
  80. }
  81. private bool bShowTopBtn;
  82. public bool ShowTopBtn
  83. {
  84. get { return bShowTopBtn; }
  85. set { bShowTopBtn = value; }
  86. }
  87. private bool mOutList;
  88. public bool MOutList
  89. {
  90. get { return mOutList; }
  91. set { mOutList = value; }
  92. }
  93. public bool IsActive {
  94. get {
  95. if (mPageBody == null)
  96. return false;
  97. return mPageBody.activeSelf;
  98. }
  99. }
  100. protected object m_Data;
  101. public object Data {
  102. get { return m_Data; }
  103. }
  104. protected UI_ANIM mAnimInType = UI_ANIM.NO;
  105. protected UI_ANIM mAnimOutType = UI_ANIM.NO;
  106. private bool mIsPageAnimation = false;
  107. private bool mIsPageEnabled = false; //save enabled state when page is animation.
  108. protected bool mIsNotify = false;
  109. protected bool mIsTop = false;
  110. protected bool mIsEnabled = true;
  111. protected bool mIsDisposed = false;
  112. protected bool mIsOpened = false;
  113. protected bool mIsShowed = false;
  114. protected bool mIsHide = false;
  115. protected bool bNeedCache = true;
  116. public bool IsHide
  117. {
  118. get { return mIsHide; }
  119. }
  120. public bool NeedCache
  121. {
  122. get { return bNeedCache; }
  123. set { bNeedCache = value; }
  124. }
  125. //protected UIData mUIData;
  126. private bool mCanPlaySound = true;
  127. public bool CanPlaySound { get { return mCanPlaySound; } set { mCanPlaySound = value; } }
  128. private int mUIType = 0;
  129. public int MUIType {
  130. set { mUIType = value; }
  131. get { return mUIType; }
  132. }
  133. private bool mPersistentStatus = false;
  134. public bool MPersistentStatus {
  135. set { mPersistentStatus = value; }
  136. get { return mPersistentStatus; }
  137. }
  138. protected int mShowLoadingActionId = 0;
  139. protected struct CanvasStatus {
  140. public Canvas m_canvas;
  141. public bool m_bOriginalEnabled;
  142. public CanvasStatus (Canvas c, bool s) {
  143. m_canvas = c;
  144. m_bOriginalEnabled = s;
  145. }
  146. };
  147. protected List<CanvasStatus> m_ChildCanvasStatus = null;
  148. private EUIPageType mPageType = EUIPageType.Null;
  149. public EUIPageType PageType {
  150. get { return mPageType; }
  151. set { mPageType = value; }
  152. }
  153. public int initSortingOrder;
  154. public int SortingOrder {
  155. get {
  156. return mCanvas != null ? mCanvas.sortingOrder : 0;
  157. }
  158. set {
  159. if (mCanvas == null)
  160. return;
  161. mCanvas.sortingOrder = value;
  162. }
  163. }
  164. public GameObject PageBody {
  165. get { return mPageBody; }
  166. }
  167. public Transform PageTrans {
  168. get { return mPageBodyTrans; }
  169. }
  170. private string mAssetBundle;
  171. public string StrAssetBundle {
  172. get {
  173. return mAssetBundle;
  174. }
  175. set {
  176. if(mAssetBundle!=value)
  177. {
  178. mAssetBundle = value;
  179. }
  180. }
  181. }
  182. private string[] mChildAssetNames = null;
  183. private string mChildPaths;
  184. public string ChildPaths
  185. {
  186. get { return mChildPaths; }
  187. set
  188. {
  189. if(mChildPaths != value)
  190. {
  191. mChildPaths = value;
  192. if (!string.IsNullOrEmpty(mChildPaths))
  193. mChildAssetNames = mChildPaths.Split(';');
  194. else
  195. mChildAssetNames = null;
  196. }
  197. }
  198. }
  199. public bool IsEnabled {
  200. get { return mIsEnabled; }
  201. set { mIsEnabled = value; }
  202. }
  203. public bool IsDisposed {
  204. get { return mIsDisposed; }
  205. }
  206. public bool IsOpened {
  207. get { return mIsOpened; }
  208. }
  209. public UI_ANIM PageAnimInType {
  210. get { return mAnimInType; }
  211. set { mAnimInType = value; }
  212. }
  213. public UI_ANIM PageAnimOutType {
  214. get { return mAnimOutType; }
  215. set { mAnimOutType = value; }
  216. }
  217. public bool IsPageAnimation {
  218. get { return mIsPageAnimation; }
  219. }
  220. public bool CanvasEnabled {
  221. get { return mCanvas != null ? mCanvas.enabled : false; }
  222. set {
  223. if (mCanvas == null) return;
  224. if (mCanvas.enabled != value) {
  225. mCanvas.overrideSorting = true;
  226. mCanvas.enabled = value;
  227. if (mChildCanvasList != null) {
  228. for (int idx = 0; idx < mChildCanvasList.Length; idx++)
  229. {
  230. if (mChildCanvasList[idx])
  231. mChildCanvasList[idx].enabled = value;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. public bool IsShowed {
  238. get { return mIsShowed; }
  239. }
  240. public Canvas UICanvas {
  241. get { return mCanvas; }
  242. }
  243. List<Selectable> mAddListenerComList = new List<Selectable> ();
  244. Dictionary<Selectable, UIPlaySound> mBtnClickSoundHolderDict = new Dictionary<Selectable, UIPlaySound>();
  245. int interTime = 500;
  246. int timerSeq;
  247. bool UIEventStatus = false;
  248. #endregion
  249. private void Awake () {
  250. mPageUniqueID = ++s_sequence;
  251. }
  252. [NoToLua]
  253. public virtual void Open (UIData uidata, object param) {
  254. DisableGroupInteraction ();
  255. //mUIData = uidata;
  256. //mPageId = mUIData.PageID;
  257. m_Data = param;
  258. bInited = false;
  259. mIsShowed = true;
  260. mIsDisposed = false;
  261. mIsOpened = true;
  262. mIsHide = false;
  263. mIsClosed = false;
  264. OnAwake ();
  265. if (mPageBody == null)
  266. {
  267. LoadUIAssets ();
  268. }
  269. else
  270. {
  271. //!Move To Top Level
  272. mPageBodyTrans.SetAsLastSibling ();
  273. if (!mIsTop && UIMgr.Instance.MainFunPage != null) {
  274. UIMgr.Instance.MainFunPage.GetComponent<RectTransform> ().SetAsLastSibling ();
  275. }
  276. FillContent ();
  277. PageAnimationIn();
  278. }
  279. }
  280. [NoToLua]
  281. public void EnableGroupInteraction () {
  282. StartCoroutine (DelayCanvasGroupInteraction (0.0f));
  283. }
  284. [NoToLua]
  285. public void DisableGroupInteraction () {
  286. if (mCanvasGroup != null)
  287. mCanvasGroup.interactable = false;
  288. }
  289. IEnumerator DelayCanvasGroupInteraction (float time) {
  290. yield return new WaitForSeconds (time);
  291. if (mCanvasGroup != null) mCanvasGroup.interactable = true;
  292. }
  293. protected void CreatePageBody ()
  294. {
  295. if (string.IsNullOrEmpty (mPageName))
  296. mPageName = GetType ().Name;
  297. mPageBody = new GameObject (mPageName);
  298. mCanvas = mPageBody.AddComponent<Canvas> ();
  299. mPageBody.AddComponent<GraphicRaycaster> ();
  300. mCanvasGroup = mPageBody.AddComponent<CanvasGroup> ();
  301. DisableGroupInteraction ();
  302. SortingOrder = initSortingOrder;
  303. CanvasEnabled = true;
  304. animListener = mPageBody.AddComponent<UIAnimationListener>();
  305. animListener.ListenerPage = this;
  306. mPageBody.layer = LayerMask.NameToLayer ("UI");
  307. mPageBodyTrans = mPageBody.transform;
  308. mPageBodyTrans.SetParent (UIMgr.Instance.UIRootTrans);
  309. RectTransform rectTrans = mPageBody.GetComponent<RectTransform> ();
  310. if (rectTrans != null) {
  311. RectTransform rootRTrans = UIMgr.Instance.UIRootTrans.GetComponent<RectTransform> ();
  312. rectTrans.localPosition = Vector3.zero;
  313. rectTrans.localScale = Vector3.one;
  314. rectTrans.anchorMin = new Vector2 (0, 0);
  315. rectTrans.anchorMax = new Vector2 (1.0f, 1.0f);
  316. rectTrans.pivot = new Vector2 (0.5f, 0.5f);
  317. rectTrans.anchoredPosition = Vector2.zero;
  318. rectTrans.sizeDelta = Vector2.zero;
  319. }
  320. if (!mIsTop && UIMgr.Instance.MainFunPage != null) {
  321. UIMgr.Instance.MainFunPage.GetComponent<RectTransform> ().SetAsLastSibling ();
  322. }
  323. mCanvas.overrideSorting = true;
  324. //SortingOrder += UIMgr.Instance.GetNextPageOrderingSort (this);
  325. }
  326. private void ResetPageBody(GameObject bodyGO)
  327. {
  328. if (string.IsNullOrEmpty(mPageName))
  329. mPageName = GetType().Name;
  330. mPageBody = bodyGO;
  331. mCanvas = mPageBody.GetComponent<Canvas>();
  332. mCanvasGroup = mPageBody.GetComponent<CanvasGroup>();
  333. DisableGroupInteraction();
  334. SortingOrder = initSortingOrder;
  335. CanvasEnabled = true;
  336. animListener = mPageBody.GetComponent<UIAnimationListener>();
  337. if (animListener == null)
  338. {
  339. animListener = mPageBody.AddComponent<UIAnimationListener>();
  340. }
  341. animListener.ListenerPage = this;
  342. mPageBodyTrans = mPageBody.transform;
  343. if (!mIsTop && UIMgr.Instance.MainFunPage != null)
  344. {
  345. UIMgr.Instance.MainFunPage.GetComponent<RectTransform>().SetAsLastSibling();
  346. }
  347. mCanvas.overrideSorting = true;
  348. //SortingOrder += UIMgr.Instance.GetNextPageOrderingSort(this);
  349. mPageBody.SetActive(true);
  350. //CommonUtil.SetGameObjectLayer(mPageBody, "UI");
  351. }
  352. protected void LoadUIAssets ()
  353. {
  354. if (mAssetBundle != null && !string.IsNullOrEmpty(mAssetBundle))
  355. {
  356. GameObject pageGo = ResourceMgr.Instance.GetUIGoFromPool(Constants.UIPath, mAssetBundle + "_body");
  357. List<string> assetNames = new List<string>();
  358. assetNames.Add(mAssetBundle);
  359. if (mChildAssetNames != null)
  360. {
  361. for (int idx = 0; idx < mChildAssetNames.Length; idx++)
  362. assetNames.Add(mChildAssetNames[idx]);
  363. }
  364. if (pageGo == null)
  365. {
  366. CreatePageBody();
  367. ResourceMgr.Instance.GetGoesFromPool(Constants.UIPath, assetNames.ToArray(), OnLoadRes);
  368. }
  369. else
  370. {
  371. ResetPageBody(pageGo);
  372. mRootPage = pageGo.transform.Find("Root").gameObject;
  373. mAnimtionEvents = mPageBody.GetComponentsInChildren<UIAnimationEvent>(true);
  374. if (mAnimtionEvents != null)
  375. {
  376. foreach(var animationEvent in mAnimtionEvents)
  377. {
  378. animationEvent.BasePage = this;
  379. }
  380. }
  381. for (int idx = 1; idx < assetNames.Count; idx++)
  382. {
  383. if (mChildGoes == null)
  384. mChildGoes = new Dictionary<string, GameObject>();
  385. var subGo = pageGo.transform.Find("Part" + idx).gameObject;
  386. mChildGoes.Add(assetNames[idx], subGo);
  387. }
  388. StartInitialize();
  389. PageAnimationIn();
  390. }
  391. }
  392. else
  393. {
  394. EventMgr.DispatchEvent<int> (new CoreEvent<int> (ECoreEventType.EID_UI_LoadFail, mPageId));
  395. }
  396. }
  397. private void OnLoadRes (List<GameObject> uiGoes,string path,string[] assetNames)
  398. {
  399. if (uiGoes == null || uiGoes.Count == 0) {
  400. EventMgr.DispatchEvent<int> (new CoreEvent<int> (ECoreEventType.EID_UI_LoadFail, mPageId));
  401. Hide ();
  402. return;
  403. }
  404. mRootPage = uiGoes[0];
  405. mRootPage.name = "Root";
  406. mRootPage.SetActive(true);
  407. for(int idx = 0;idx < uiGoes.Count;idx++)
  408. {
  409. CommonUtil.SetGameObjectLayer(uiGoes[idx], "UI");
  410. RectTransform rectTrans = uiGoes[idx].GetComponent<RectTransform>();
  411. rectTrans.SetParent(mPageBodyTrans, false);
  412. rectTrans.localScale = Vector3.one;
  413. if(idx == 0)
  414. {
  415. rectTrans.anchorMin = new Vector2(0, 0);
  416. rectTrans.anchorMax = new Vector2(1.0f, 1.0f);
  417. rectTrans.pivot = new Vector2(0.5f, 0.5f);
  418. rectTrans.anchoredPosition3D = Vector3.zero;
  419. rectTrans.sizeDelta = Vector2.zero;
  420. }
  421. else
  422. {
  423. if (mChildGoes == null)
  424. mChildGoes = new Dictionary<string, GameObject>();
  425. mChildGoes.Add(assetNames[idx], uiGoes[idx]);
  426. uiGoes[idx].name = "Part" + idx;
  427. }
  428. }
  429. //已经关闭
  430. if (mIsDisposed || mIsClosed)
  431. {
  432. //ResourceMgr.Instance.RecycleGO(Constants.UIPath, mMainAssetBundle, mRootPage);
  433. for(int idx =0; idx < assetNames.Length;idx++)
  434. {
  435. ResourceMgr.Instance.RecycleGO(Constants.UIPath, assetNames[idx], uiGoes[idx]);
  436. }
  437. mRootPage = null;
  438. return;
  439. }
  440. mAnimtionEvents = mPageBody.GetComponentsInChildren<UIAnimationEvent>(true);
  441. if (mAnimtionEvents != null)
  442. {
  443. foreach (var animationEvent in mAnimtionEvents)
  444. {
  445. animationEvent.BasePage = this;
  446. }
  447. }
  448. StartInitialize ();
  449. PageAnimationIn();
  450. }
  451. public GameObject GetRoot ()
  452. {
  453. return mRootPage;
  454. }
  455. void StartInitialize ()
  456. {
  457. EnableGroupInteraction ();
  458. mChildCanvasList = mPageBody.GetComponentsInChildren<Canvas> ();
  459. if (mChildCanvasList != null)
  460. {
  461. for (int idx = 0; idx < mChildCanvasList.Length; idx++)
  462. mChildCanvasList[idx].enabled = true;
  463. }
  464. EventMgr.DispatchEvent<int>(new CoreEvent<int>(ECoreEventType.EID_UI_LoadSuccess, mPageId));
  465. InitializePage();
  466. }
  467. public GameObject FindElementGo (string name) {
  468. if (mPageBodyTrans == null) return null;
  469. GameObject go = null;
  470. Transform trans = mPageBodyTrans.Find (name);
  471. if (trans != null) go = trans.gameObject;
  472. return go;
  473. }
  474. public GameObject FindElementGo (GameObject go, string name) {
  475. if (go == null) return null;
  476. GameObject cgo = null;
  477. Transform trans = go.transform.Find (name);
  478. if (trans != null) cgo = trans.gameObject;
  479. return cgo;
  480. }
  481. public T FindElement<T> (string name) {
  482. return FindElement<T> (mPageBody, name);
  483. }
  484. public T FindElement<T> (GameObject go, string name) {
  485. if (go == null) return default (T);
  486. Transform trans = go.transform.Find (name);
  487. if (trans == null) return default (T);
  488. return trans.GetComponent<T> ();
  489. }
  490. #region 虚函数
  491. [NoToLua]
  492. public virtual void InitializePage () {
  493. bInited = true;
  494. FillContent ();
  495. AddEventListener ();
  496. AddUIEventListener ();
  497. }
  498. [NoToLua]
  499. public virtual void OnOpenAnimEnd () {
  500. mIsPageAnimation = false;
  501. IsEnabled = mIsPageEnabled;
  502. if (!IsOpened) {
  503. CanvasEnabled = false;
  504. }
  505. OnPageInEnd();
  506. EventMgr.DispatchEvent<int>(new CoreEvent<int>(ECoreEventType.EID_UI_SHOW, mPageId));
  507. }
  508. [NoToLua]
  509. public virtual void OnCloseAnimEnd()
  510. {
  511. mIsPageAnimation = false;
  512. IsEnabled = mIsPageEnabled;
  513. CanvasEnabled = false;
  514. OnPageOutEnd();
  515. EventMgr.DispatchEvent<int>(new CoreEvent<int>(ECoreEventType.EID_UI_CLOSE, mUIType));
  516. }
  517. public virtual void Show (object param = null, bool needOutAnim = true) {
  518. m_Data = param;
  519. EnableGroupInteraction ();
  520. if (!bInited)
  521. {
  522. return;
  523. }
  524. if (!mIsOpened) {
  525. mIsOpened = true;
  526. EventMgr.DispatchEvent<int> (new CoreEvent<int> (ECoreEventType.EID_UI_SHOW, mPageId));
  527. }
  528. if (!mIsShowed) {
  529. if (!UIMgr.Instance.PageInHideStack (this)) {
  530. //SortingOrder += UIMgr.Instance.GetNextPageOrderingSort(this);
  531. }
  532. CanvasEnabled = true;
  533. //mPageBody.SetActive (true);
  534. mIsShowed = true;
  535. mIsHide = false;
  536. PageAnimationIn (needOutAnim);
  537. }
  538. OnShow ();
  539. //EventMgr.DispatchEvent<int, bool> (new CoreEvent<int, bool> (ECoreEventType.EID_UI_VISIBLE, mPageId, true));
  540. }
  541. public virtual void Hide (bool needOutAnim = true) {
  542. if (!mIsShowed) return;
  543. OnHide();
  544. PageAnimationOut(needOutAnim);
  545. if (!IsPageAnimation) {
  546. //mPageBody.SetActive (false);
  547. CanvasEnabled = false;
  548. }
  549. mIsShowed = false;
  550. mIsHide = true;
  551. //EventMgr.DispatchEvent<int, bool> (new CoreEvent<int, bool> (ECoreEventType.EID_UI_VISIBLE, mPageId, false));
  552. }
  553. public virtual void Close (bool needOutAnim = true) {
  554. if (mIsClosed) return;
  555. RemoveEventListener();
  556. OnClose();
  557. PageAnimationOut(needOutAnim);
  558. mIsShowed = false;
  559. mIsOpened = false;
  560. mIsHide = false;
  561. mIsClosed = true;
  562. }
  563. public virtual void Dispose () {
  564. if (mIsDisposed) return;
  565. TimerManager.Instance.RemoveTimer(timerSeq);
  566. for (int idx = 0; idx < mAddListenerComList.Count; idx++)
  567. {
  568. Button btn = mAddListenerComList[idx] as Button;
  569. if (btn != null)
  570. {
  571. btn.onClick.RemoveAllListeners();
  572. continue;
  573. }
  574. Toggle tog = mAddListenerComList[idx] as Toggle;
  575. if (tog != null)
  576. {
  577. tog.onValueChanged.RemoveAllListeners();
  578. continue;
  579. }
  580. Slider slider = mAddListenerComList[idx] as Slider;
  581. if (slider != null)
  582. {
  583. slider.onValueChanged.RemoveAllListeners();
  584. continue;
  585. }
  586. InputField input = mAddListenerComList[idx] as InputField;
  587. if (input != null)
  588. {
  589. input.onValueChanged.RemoveAllListeners();
  590. continue;
  591. }
  592. TMP_InputField tInput = mAddListenerComList[idx] as TMP_InputField;
  593. if (tInput != null)
  594. {
  595. tInput.onValueChanged.RemoveAllListeners();
  596. continue;
  597. }
  598. Dropdown dropDown = mAddListenerComList[idx] as Dropdown;
  599. if (dropDown != null)
  600. {
  601. dropDown.onValueChanged.RemoveAllListeners();
  602. continue;
  603. }
  604. }
  605. mAddListenerComList.Clear();
  606. mBtnClickSoundHolderDict.Clear();
  607. Close();
  608. OnDispose ();
  609. CanvasEnabled = false;
  610. if(mRootPage!=null && bNeedCache)
  611. {
  612. if (mPageBody != null)
  613. {
  614. ResourceMgr.Instance.RecycleUIGO(Constants.UIPath, mAssetBundle + "_body", mPageBody);
  615. }
  616. }
  617. else
  618. {
  619. ResourceMgr.Instance.RemoveLoadedResource(Constants.UIPath, mAssetBundle);
  620. if(mChildAssetNames != null)
  621. {
  622. for(int idx =0; idx < mChildAssetNames.Length;idx++)
  623. {
  624. ResourceMgr.Instance.RemoveLoadedResource(Constants.UIPath, mChildAssetNames[idx]);
  625. }
  626. }
  627. GameObject.Destroy(mPageBody);
  628. }
  629. if (mAnimtionEvents != null)
  630. {
  631. foreach (var animationEvent in mAnimtionEvents)
  632. {
  633. animationEvent.BasePage = null;
  634. }
  635. }
  636. if(animListener != null)
  637. {
  638. animListener.ListenerPage = null;
  639. }
  640. mPageBody = null;
  641. mPageBodyTrans = null;
  642. mRootPage = null;
  643. mAnimtionEvents = null;
  644. mChildCanvasList = null;
  645. animListener = null;
  646. m_Data = null;
  647. mCanvas = null;
  648. mCanvasGroup = null;
  649. mIsDisposed = true;
  650. Destroy (this);
  651. }
  652. public virtual void BackIn () {
  653. OnBackIn ();
  654. }
  655. #endregion
  656. #region inner_methods
  657. void PageAnimationIn (bool needOutAnim = true) {
  658. if (!UIMgr.Instance) return;
  659. //已经关闭
  660. if (mIsDisposed || mIsClosed)
  661. {
  662. return;
  663. }
  664. if (mIsPageAnimation) {
  665. StartCoroutine (checkAnimationState ());
  666. return;
  667. }
  668. if (mRootPage != null)
  669. {
  670. mRootPage.SetActive(true);
  671. }
  672. mIsPageAnimation = false;
  673. if (needOutAnim)
  674. {
  675. Animator anim = null;
  676. Transform uiAnimator = mPageBody.transform.Find("Root/UIAnimator");
  677. if (uiAnimator != null)
  678. {
  679. if (mAnimInType != UI_ANIM.NO)
  680. {
  681. anim = uiAnimator.GetComponent<Animator>();
  682. }
  683. if (anim != null)
  684. {
  685. switch (mAnimInType)
  686. {
  687. case UI_ANIM.MOVE_IN:
  688. case UI_ANIM.MOVE_OUT:
  689. case UI_ANIM.SCALE_IN:
  690. case UI_ANIM.SCALE_OUT:
  691. {
  692. string name = GetAniName(mAnimInType);
  693. if (anim != null)
  694. {
  695. anim.Play(name, 0, 0);
  696. anim.updateMode = AnimatorUpdateMode.Normal;
  697. mIsPageAnimation = true;
  698. }
  699. }
  700. break;
  701. }
  702. }
  703. }
  704. }
  705. if (mIsPageAnimation)
  706. {
  707. mIsPageEnabled = IsEnabled;
  708. IsEnabled = false;
  709. }
  710. else
  711. {
  712. OnOpenAnimEnd();
  713. }
  714. }
  715. [NoToLua]
  716. public string GetAniName(UI_ANIM animType)
  717. {
  718. string animName = null;
  719. switch (animType)
  720. {
  721. case UI_ANIM.MOVE_IN:
  722. animName = "UIMoveInRight";
  723. break;
  724. case UI_ANIM.MOVE_OUT:
  725. animName = "UIMoveOutLeft";
  726. break;
  727. case UI_ANIM.SCALE_IN:
  728. animName = "UIWindowIn";
  729. break;
  730. case UI_ANIM.SCALE_OUT:
  731. animName = "UIWindowOut";
  732. break;
  733. }
  734. return animName;
  735. }
  736. void PageAnimationOut (bool needOutAnim = true) {
  737. if (!UIMgr.Instance) return;
  738. mIsPageAnimation = false;
  739. if (needOutAnim)
  740. {
  741. Animator anim = null;
  742. Transform uiAnimator = mPageBody.transform.Find("Root/UIAnimator");
  743. if (uiAnimator != null)
  744. {
  745. if (mAnimOutType != UI_ANIM.NO)
  746. {
  747. anim = uiAnimator.GetComponent<Animator>();
  748. }
  749. if (anim != null)
  750. {
  751. switch (mAnimOutType)
  752. {
  753. case UI_ANIM.MOVE_IN:
  754. case UI_ANIM.MOVE_OUT:
  755. case UI_ANIM.SCALE_IN:
  756. case UI_ANIM.SCALE_OUT:
  757. {
  758. string name = GetAniName(mAnimOutType);
  759. if (anim != null)
  760. {
  761. anim.Play(name, 0, 0);
  762. anim.updateMode = AnimatorUpdateMode.Normal;
  763. mIsPageAnimation = true;
  764. }
  765. }
  766. break;
  767. }
  768. }
  769. }
  770. }
  771. if (mIsPageAnimation) {
  772. mIsPageEnabled = IsEnabled;
  773. IsEnabled = false;
  774. } else {
  775. OnCloseAnimEnd();
  776. }
  777. StartCoroutine (DelayDisablePage ());
  778. }
  779. IEnumerator checkAnimationState () {
  780. while (mIsPageAnimation) yield return 0;
  781. PageAnimationIn ();
  782. }
  783. IEnumerator DelayDisablePage () {
  784. if (this.PageBody != null) {
  785. if (mCanvasGroup != null) {
  786. float time = 0;
  787. while (mCanvasGroup.alpha != 0) {
  788. yield return new WaitForEndOfFrame ();
  789. if (!PageBody)
  790. yield break;
  791. time += Time.deltaTime;
  792. if (time > 2) {
  793. yield break;
  794. }
  795. }
  796. if (mRootPage != null) {
  797. mRootPage.SetActive(false);
  798. }
  799. }
  800. }
  801. }
  802. #endregion
  803. #region 外部需要重构的 abstract 方法.
  804. protected abstract void OnAwake ();
  805. protected abstract void AddEventListener ();
  806. protected abstract void FillContent ();
  807. protected abstract void RemoveEventListener ();
  808. protected abstract void AddUIEventListener ();
  809. protected abstract void OnBaseHide();
  810. protected abstract void OnHide ();
  811. protected abstract void OnBaseShow();
  812. protected abstract void OnShow ();
  813. protected abstract void OnBaseClose();
  814. protected abstract void OnClose ();
  815. protected abstract void OnBaseDispose();
  816. protected abstract void OnDispose ();
  817. protected abstract void OnBackIn ();
  818. protected abstract void OnPageOutEnd();
  819. protected abstract void OnPageInEnd();
  820. public abstract void OnSubCloseAnimEnd(string path);
  821. #endregion
  822. #region events
  823. void ResetUIEventStatus (int seq) {
  824. UIEventStatus = false;
  825. timerSeq = -1;
  826. }
  827. public void AddButtonExitListener (Button btn, LuaTable table, LuaFunction listener, params object[] param) {
  828. if (btn != null) {
  829. mAddListenerComList.Add (btn);
  830. UIEventTriggerListener eventTrigger = UIEventTriggerListener.Get (btn.gameObject);
  831. eventTrigger.onPointerExit = new UnityEngine.Events.UnityAction (() => {
  832. listener.Call (table, btn, param);
  833. });
  834. }
  835. }
  836. public void AddButtonEventListener (Button btn, ButtonListener listener) {
  837. if (btn != null) {
  838. mAddListenerComList.Add (btn);
  839. btn.onClick.AddListener (new UnityEngine.Events.UnityAction (() => {
  840. if (BattleMgr.Instance.IsRotatingCam) return;
  841. if (UIEventStatus) return;
  842. UIEventStatus = true;
  843. timerSeq = TimerManager.Instance.AddTimer (interTime, 1, ResetUIEventStatus);
  844. ClickButton (btn);
  845. listener (btn);
  846. }));
  847. }
  848. }
  849. public void AddButtonEventListener (Button btn, LuaFunction listener) {
  850. if (btn != null) {
  851. mAddListenerComList.Add (btn);
  852. btn.onClick.AddListener (new UnityEngine.Events.UnityAction (() => {
  853. if (BattleMgr.Instance.IsRotatingCam) return;
  854. if (UIEventStatus) return;
  855. UIEventStatus = true;
  856. timerSeq = TimerManager.Instance.AddTimer (interTime, 1, ResetUIEventStatus);
  857. ClickButton (btn);
  858. listener.Call (btn);
  859. }));
  860. }
  861. }
  862. public void AddButtonEventListener (Button btn, LuaTable table, LuaFunction listener) {
  863. //Debug.Log($"------------------{btn.name}---");
  864. //if (btn.name == "BtnInfo")
  865. //{
  866. // Debug.Log($"------------------{btn.name}---");
  867. // btn.onClick.AddListener(() => { Debug.Log("点击BtnInfo"); });
  868. //}
  869. if (btn != null) {
  870. mAddListenerComList.Add (btn);
  871. btn.onClick.AddListener (new UnityEngine.Events.UnityAction (() => {
  872. if (BattleMgr.Instance.IsRotatingCam) return;
  873. if (UIEventStatus) return;
  874. UIEventStatus = true;
  875. timerSeq = TimerManager.Instance.AddTimer (interTime, 1, ResetUIEventStatus);
  876. ClickButton (btn);
  877. listener.Call (table, btn);
  878. }));
  879. }
  880. }
  881. public void AddButtonEventListener (Button btn, ButtonListenerWithParam listener, params object[] param) {
  882. if (btn != null) {
  883. mAddListenerComList.Add (btn);
  884. btn.onClick.AddListener (new UnityEngine.Events.UnityAction (() => {
  885. if (BattleMgr.Instance.IsRotatingCam) return;
  886. if (UIEventStatus) return;
  887. UIEventStatus = true;
  888. timerSeq = TimerManager.Instance.AddTimer (interTime, 1, ResetUIEventStatus);
  889. ClickButton (btn);
  890. listener (btn, param);
  891. }));
  892. }
  893. }
  894. public void AddButtonEventListener (Button btn, LuaTable table, LuaFunction listener, params object[] param) {
  895. if (btn != null) {
  896. mAddListenerComList.Add (btn);
  897. btn.onClick.AddListener (new UnityEngine.Events.UnityAction (() => {
  898. if (BattleMgr.Instance.IsRotatingCam) return;
  899. if (UIEventStatus) return;
  900. UIEventStatus = true;
  901. timerSeq = TimerManager.Instance.AddTimer (interTime, 1, ResetUIEventStatus);
  902. ClickButton (btn);
  903. listener.Call (table, btn, param);
  904. }));
  905. }
  906. }
  907. public void AddButtonUniqueEventListener (Button btn, LuaTable table, LuaFunction listener, params object[] param) {
  908. if (btn != null) {
  909. mAddListenerComList.Add (btn);
  910. btn.onClick.RemoveAllListeners ();
  911. btn.onClick.AddListener (new UnityEngine.Events.UnityAction (() => {
  912. if (BattleMgr.Instance.IsRotatingCam) return;
  913. ClickButton (btn);
  914. listener.Call (table, btn, param);
  915. }));
  916. }
  917. }
  918. public void AddToggleEventListener (Toggle tog, ToggleListener listener) {
  919. if (tog != null) {
  920. mAddListenerComList.Add (tog);
  921. tog.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<bool> ((b) => {
  922. if (BattleMgr.Instance.IsRotatingCam) return;
  923. if (b)
  924. ClickToggle (tog);
  925. listener (b);
  926. }));
  927. }
  928. }
  929. public void AddToggleEventListener (Toggle tog, LuaFunction listener) {
  930. if (tog != null) {
  931. mAddListenerComList.Add (tog);
  932. tog.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<bool> ((b) => {
  933. if (BattleMgr.Instance.IsRotatingCam) return;
  934. if (b && !tog.isOn)
  935. {
  936. ClickToggle(tog);
  937. }
  938. listener.Call (b);
  939. }));
  940. }
  941. }
  942. public void AddToggleEventListener (Toggle tog, ToggleListenerWithParams listener, params object[] param) {
  943. if (tog != null) {
  944. mAddListenerComList.Add (tog);
  945. tog.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<bool> ((b) => {
  946. if (BattleMgr.Instance.IsRotatingCam) return;
  947. if (b)
  948. ClickToggle (tog);
  949. listener (tog, param);
  950. }));
  951. }
  952. }
  953. public void AddToggleEventListener (Toggle tog, LuaTable luaTbl, LuaFunction listener, object param) {
  954. if (tog != null) {
  955. mAddListenerComList.Add (tog);
  956. tog.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<bool> ((b) => {
  957. if (BattleMgr.Instance.IsRotatingCam) return;
  958. if (b && !tog.isOn) {
  959. ClickToggle (tog);
  960. }
  961. listener.Call (luaTbl, tog, param, b);
  962. }));
  963. }
  964. }
  965. public void AddToggleUniqueEventListener(Toggle tog, LuaTable luaTbl, LuaFunction listener, params object[] param)
  966. {
  967. if (tog != null)
  968. {
  969. mAddListenerComList.Add(tog);
  970. tog.onValueChanged.RemoveAllListeners();
  971. tog.onValueChanged.AddListener(new UnityEngine.Events.UnityAction<bool>((b) => {
  972. if (BattleMgr.Instance.IsRotatingCam) return;
  973. if (b && !tog.isOn)
  974. {
  975. ClickToggle(tog);
  976. }
  977. listener.Call(luaTbl, tog, param, b);
  978. }));
  979. }
  980. }
  981. public void AddToggleEventListener (Toggle tog, LuaFunction listener, params object[] param) {
  982. if (tog != null) {
  983. mAddListenerComList.Add (tog);
  984. tog.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<bool> ((b) => {
  985. if (BattleMgr.Instance.IsRotatingCam) return;
  986. if (b && !tog.isOn)
  987. {
  988. ClickToggle(tog);
  989. }
  990. listener.Call (tog, param, b);
  991. }));
  992. }
  993. }
  994. public void AddSliderEventListener (Slider slider, LuaTable luaTbl, LuaFunction listener, params object[] param) {
  995. if (slider != null) {
  996. mAddListenerComList.Add (slider);
  997. slider.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<float> ((b) => {
  998. if (BattleMgr.Instance.IsRotatingCam) return;
  999. listener.Call (luaTbl, slider, b, param);
  1000. }));
  1001. }
  1002. }
  1003. public void AddSliderUniqueEventListener(Slider slider, LuaTable luaTbl, LuaFunction listener, params object[] param) {
  1004. if (slider != null) {
  1005. mAddListenerComList.Add (slider);
  1006. slider.onValueChanged.RemoveAllListeners();
  1007. slider.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<float> ((b) => {
  1008. if (BattleMgr.Instance.IsRotatingCam) return;
  1009. listener.Call (luaTbl, slider, b, param);
  1010. }));
  1011. }
  1012. }
  1013. public void AddInputFileEventListener (InputField inputField, LuaFunction listener) {
  1014. if (inputField != null) {
  1015. mAddListenerComList.Add (inputField);
  1016. inputField.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<string> ((str) => {
  1017. if (BattleMgr.Instance.IsRotatingCam) return;
  1018. listener.Call (inputField, str);
  1019. }));
  1020. }
  1021. }
  1022. public void AddTMPInputFileEventListener(TMP_InputField inputField, LuaFunction listener)
  1023. {
  1024. if (inputField != null)
  1025. {
  1026. mAddListenerComList.Add(inputField);
  1027. inputField.onValueChanged.AddListener(new UnityEngine.Events.UnityAction<string>((str) => {
  1028. listener.Call(inputField, str);
  1029. }));
  1030. }
  1031. }
  1032. public void AddDropdownEventListener (Dropdown dropdown, LuaFunction listener) {
  1033. if (dropdown != null) {
  1034. mAddListenerComList.Add (dropdown);
  1035. dropdown.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<int> ((value) => {
  1036. if (BattleMgr.Instance.IsRotatingCam) return;
  1037. listener.Call (dropdown, value);
  1038. }));
  1039. }
  1040. }
  1041. public void AddUIEventHandlerClickListener (UIEventHandler handler, LuaFunction listener) {
  1042. if (handler == null) return;
  1043. handler.onClick.AddListener (new UnityEngine.Events.UnityAction<GameObject> ((value) => {
  1044. if (BattleMgr.Instance.IsRotatingCam) return;
  1045. listener.Call (value);
  1046. }));
  1047. }
  1048. public void AddScrollRectValueChangeListener (ScrollRect scrollRect, LuaTable luaTbl, LuaFunction listener) {
  1049. if (scrollRect == null) return;
  1050. scrollRect.onValueChanged.AddListener (new UnityEngine.Events.UnityAction<Vector2> ((value) => {
  1051. if (BattleMgr.Instance.IsRotatingCam) return;
  1052. listener.Call (luaTbl, scrollRect, value);
  1053. }));
  1054. }
  1055. public void InvokeToggle (Toggle tog, bool isOn) {
  1056. if (tog != null) tog.onValueChanged.Invoke (isOn);
  1057. }
  1058. public GameObject FindChildGo(string childName)
  1059. {
  1060. if (mChildGoes == null) return null;
  1061. GameObject go;
  1062. mChildGoes.TryGetValue(childName, out go);
  1063. return go;
  1064. }
  1065. void ClickButton (Button btn) {
  1066. //处理点击button的通用行为
  1067. if (btn != null)
  1068. {
  1069. UIPlaySound holder = null;
  1070. if (!mBtnClickSoundHolderDict.TryGetValue(btn, out holder))
  1071. {
  1072. holder = btn.gameObject.GetComponent<UIPlaySound>();
  1073. mBtnClickSoundHolderDict.Add(btn, holder);
  1074. }
  1075. if (holder != null)
  1076. {
  1077. holder.OnClick();
  1078. }
  1079. else
  1080. {
  1081. MusicMgr.Instance.PlayUISound(UIPlaySound.CommonClickSound, false);
  1082. }
  1083. }
  1084. else
  1085. {
  1086. MusicMgr.Instance.PlayUISound(UIPlaySound.CommonClickSound, false);
  1087. }
  1088. }
  1089. void ClickToggle (Toggle tog)
  1090. {
  1091. //处理点击button的通用行为
  1092. if (tog != null)
  1093. {
  1094. UIPlaySound holder = null;
  1095. if (!mBtnClickSoundHolderDict.TryGetValue(tog, out holder))
  1096. {
  1097. holder = tog.gameObject.GetComponent<UIPlaySound>();
  1098. mBtnClickSoundHolderDict.Add(tog, holder);
  1099. }
  1100. if (holder != null)
  1101. {
  1102. holder.OnClick();
  1103. }
  1104. else
  1105. {
  1106. MusicMgr.Instance.PlayUISound(UIPlaySound.CommonClickSound, false);
  1107. }
  1108. }
  1109. else
  1110. {
  1111. MusicMgr.Instance.PlayUISound(UIPlaySound.CommonClickSound, false);
  1112. }
  1113. }
  1114. #endregion
  1115. }