UIView.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using ET;
  2. using FairyGUI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace GFGGame
  7. {
  8. /// <summary>
  9. /// 视图基类,界面不可以直接继承
  10. /// </summary>
  11. public class UIView : IUIView
  12. {
  13. private string _descFilePath;
  14. private EnumViewAnimationType _viewAnimationType = EnumViewAnimationType.None;
  15. /// <summary>
  16. /// 打开视图传递的参数
  17. /// </summary>
  18. public object viewData { get; set; }
  19. public string viewName { get; set; }
  20. /// <summary>
  21. /// 设置视图组件
  22. /// </summary>
  23. public virtual GComponent viewCom { get; set; }
  24. /// <summary>
  25. /// 设置是否全屏自适应
  26. /// </summary>
  27. public bool isfullScreen { get; set; }
  28. /// <summary>
  29. /// 设置是否可以返回界面
  30. /// </summary>
  31. public bool isReturnView { get; set; }
  32. /// <summary>
  33. /// 设置是否可以返回窗口
  34. /// </summary>
  35. public bool isReturnWindow { get; set; }
  36. /// <summary>
  37. /// 设置返回界面是否刷新
  38. /// </summary>
  39. public bool backRefresh { get; set; }
  40. /// <summary>
  41. /// FairyGUI包名
  42. /// </summary>
  43. public string packageName
  44. {
  45. get
  46. {
  47. return _descFilePath;
  48. }
  49. set
  50. {
  51. _descFilePath = ResPathUtil.GetUIPackagePath(value);
  52. GFGUIPackage.AddPackage(_descFilePath);
  53. }
  54. }
  55. /// <summary>
  56. /// 界面关闭时间
  57. /// </summary>
  58. /// <value></value>
  59. public long closeTime { get; set; }
  60. /// <summary>
  61. /// 是否显示中
  62. /// </summary>
  63. public bool isShowing
  64. {
  65. get { return viewCom != null && viewCom.onStage; }
  66. }
  67. /// <summary>
  68. /// 设置视图显示和隐藏的动画类型
  69. /// </summary>
  70. protected EnumViewAnimationType viewAnimationType
  71. {
  72. set
  73. {
  74. _viewAnimationType = value;
  75. }
  76. }
  77. public UIView()
  78. {
  79. //
  80. Init();
  81. OnInit();
  82. if (viewCom != null)
  83. {
  84. viewCom.onAddedToStage.Add(__addedToStage);
  85. viewCom.onRemovedFromStage.Add(__removeFromStage);
  86. }
  87. }
  88. public virtual void Dispose()
  89. {
  90. if (viewCom != null)
  91. {
  92. viewCom.RemoveFromParent();
  93. viewCom.Dispose();
  94. viewCom = null;
  95. }
  96. if (_descFilePath != null)
  97. {
  98. if (packageName != ResPathUtil.GetUIPackagePath("CommonGame") && packageName != ResPathUtil.GetUIPackagePath("Common") && packageName != ResPathUtil.GetUIPackagePath("Main"))
  99. {
  100. GFGUIPackage.ReleasePackage(_descFilePath);
  101. } //这几个包不释放
  102. }
  103. _descFilePath = null;
  104. viewData = null;
  105. ViewManager.ClearUIView(viewName);
  106. }
  107. virtual protected void Init()
  108. {
  109. }
  110. virtual protected void OnInit()
  111. {
  112. }
  113. /// <summary>
  114. /// 界面打开状态刷新界面
  115. /// </summary>
  116. virtual public void Refresh()
  117. {
  118. object temp = viewData;
  119. this.OnHide();
  120. this.viewData = temp;
  121. this.BringToFront();
  122. this.OnShown();
  123. }
  124. public virtual void Show()
  125. {
  126. }
  127. virtual protected void BringToFront()
  128. {
  129. }
  130. public virtual void Hide()
  131. {
  132. // UIPackageManager.Instance.RemovePackageView(_descFilePath, viewName);
  133. // UIPackageManager.Instance.AddCloseTime(_descFilePath);
  134. closeTime = TimeHelper.ClientNowSeconds();
  135. DoHideAnimation();
  136. ViewManager.HideWin(this.viewName);
  137. EventAgent.DispatchEvent(ConstMessage.VIEW_CLOSED);
  138. }
  139. virtual protected void OnShown()
  140. {
  141. if (isfullScreen)
  142. {
  143. MakeFullScreen(viewCom);
  144. }
  145. AddEventListener();
  146. // if (GuideController.IsGuide())
  147. // {
  148. // Timers.inst.AddUpdate(UpdateToCheckGuide);
  149. // }
  150. }
  151. virtual protected void OnHide()
  152. {
  153. viewData = null;
  154. RemoveEventListener();
  155. // Timers.inst.Remove(UpdateToCheckGuide);
  156. TryCompleteGuide();
  157. }
  158. virtual protected void RemoveEventListener()
  159. {
  160. }
  161. virtual protected void AddEventListener()
  162. {
  163. }
  164. virtual protected void UpdateToCheckGuide(object param)
  165. {
  166. }
  167. virtual protected void TryCompleteGuide()
  168. {
  169. }
  170. protected virtual void DoShowAnimation()
  171. {
  172. OnShown();
  173. switch (_viewAnimationType)
  174. {
  175. case EnumViewAnimationType.ZOOM_CENTER:
  176. ViewAnimationFactory.ZoomInCenter(this.viewCom);
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. protected virtual void DoHideAnimation()
  183. {
  184. switch (_viewAnimationType)
  185. {
  186. case EnumViewAnimationType.ZOOM_CENTER:
  187. ViewAnimationFactory.ZoomOutCenter(this.viewCom, this.OnDoHideAnimationCompleted);
  188. break;
  189. default:
  190. this.OnDoHideAnimationCompleted();
  191. break;
  192. }
  193. }
  194. protected virtual void OnDoHideAnimationCompleted()
  195. {
  196. if (this.viewCom != null)
  197. {
  198. this.viewCom.RemoveFromParent();
  199. }
  200. }
  201. private void MakeFullScreen(GObject ui)
  202. {
  203. // 摘星界面特殊处理
  204. if (viewName == "LuckyBoxView")
  205. {
  206. ui.MakeFullScreen();
  207. }
  208. else
  209. {
  210. MakeUIFullScreen(ui);
  211. }
  212. ui.AddRelation(GRoot.inst, RelationType.Size);
  213. }
  214. private bool _adaptFinished = false;
  215. public void MakeUIFullScreen(GObject ui)
  216. {
  217. if (_adaptFinished)
  218. {
  219. return;
  220. }
  221. //这里做了最大宽度适配
  222. float targetWidth;
  223. float maxAspectRatio = 1080 * 1.0f / 1920;
  224. if (Screen.width * 1.0f / Screen.height > maxAspectRatio)
  225. {
  226. targetWidth = GRoot.inst.height * maxAspectRatio;
  227. ui.Center();
  228. }
  229. else
  230. {
  231. targetWidth = GRoot.inst.width;
  232. }
  233. GameObject uiObj = viewCom.displayObject.gameObject;
  234. List<GObject> listTopImg = new List<GObject>();
  235. float offsetY = ViewGlobal.GetRealTopOffset();
  236. // 处理全屏显示的背景图/遮罩
  237. List<Transform> list = ViewGlobal.FindAllGLoaderInTrans(uiObj.transform);
  238. for (int i = 0; i < list.Count; i++)
  239. {
  240. GameObject bg = list[i].gameObject;
  241. DisplayObjectInfo bgInfo = bg.GetComponent<DisplayObjectInfo>();
  242. GObject obj = GRoot.inst.DisplayObjectToGObject(bgInfo.displayObject);
  243. if (obj != null)
  244. {
  245. if (ViewGlobal.IsFullScreenPic(obj.name) && obj.height >= 1920)
  246. {
  247. obj.AddRelation(ui, RelationType.Center_Center);
  248. obj.SetSize(1080, 2400);
  249. obj.SetXY(obj.x, obj.y - offsetY / 2);
  250. }
  251. else if (obj.name.Contains("Top_img"))
  252. {
  253. listTopImg.Add(obj);
  254. }
  255. }
  256. }
  257. // 保存未适配时,不需要改变位置的UI的信息(适配部分存在拉伸效果的UI)
  258. ui.SetSize(targetWidth, GRoot.inst.height);
  259. List<float> heightList = new List<float>();
  260. for (int i = 0; i < listTopImg.Count; i++)
  261. {
  262. listTopImg[i].RemoveRelation(ui, RelationType.Center_Center);
  263. heightList.Add(listTopImg[i].height);
  264. }
  265. // UI整体高度缩放
  266. ui.SetSize(targetWidth, ViewGlobal.GetUIHeight());
  267. // UI整体下移动
  268. ui.SetXY(ui.x, offsetY);
  269. // 还原不需要适配的UI
  270. for (int i = 0; i < listTopImg.Count; i++)
  271. {
  272. listTopImg[i].SetSize(listTopImg[i].width, heightList[i] + offsetY * 2);
  273. listTopImg[i].SetXY(listTopImg[i].x, listTopImg[i].y - offsetY);
  274. }
  275. _adaptFinished = true;
  276. }
  277. void __addedToStage()
  278. {
  279. DoShowAnimation();
  280. }
  281. void __removeFromStage()
  282. {
  283. OnHide();
  284. if (_descFilePath == ResPathUtil.GetUIPackagePath("Login") || _descFilePath == ResPathUtil.GetUIPackagePath("Loading") || _descFilePath == ResPathUtil.GetUIPackagePath("CreateRole"))//这几个界面关闭后立即释放
  285. {
  286. Dispose();
  287. }
  288. }
  289. }
  290. }