UIExtension.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework.DataTable;
  8. using GameFramework.UI;
  9. using System.Collections;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. using UnityGameFramework.Runtime;
  13. namespace MetaClient
  14. {
  15. public static class UIExtension
  16. {
  17. public static IEnumerator FadeToAlpha(this CanvasGroup canvasGroup, float alpha, float duration)
  18. {
  19. float time = 0f;
  20. float originalAlpha = canvasGroup.alpha;
  21. while (time < duration)
  22. {
  23. time += Time.deltaTime;
  24. canvasGroup.alpha = Mathf.Lerp(originalAlpha, alpha, time / duration);
  25. yield return new WaitForEndOfFrame();
  26. }
  27. canvasGroup.alpha = alpha;
  28. }
  29. public static IEnumerator SmoothValue(this Slider slider, float value, float duration)
  30. {
  31. float time = 0f;
  32. float originalValue = slider.value;
  33. while (time < duration)
  34. {
  35. time += Time.deltaTime;
  36. slider.value = Mathf.Lerp(originalValue, value, time / duration);
  37. yield return new WaitForEndOfFrame();
  38. }
  39. slider.value = value;
  40. }
  41. public static bool HasUIForm(this UIComponent uiComponent, UIFormId uiFormId, string uiGroupName = null)
  42. {
  43. return uiComponent.HasUIForm((int)uiFormId, uiGroupName);
  44. }
  45. public static bool HasUIForm(this UIComponent uiComponent, int uiFormId, string uiGroupName = null)
  46. {
  47. IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
  48. DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
  49. if (drUIForm == null)
  50. {
  51. return false;
  52. }
  53. string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);
  54. if (string.IsNullOrEmpty(uiGroupName))
  55. {
  56. return uiComponent.HasUIForm(assetName);
  57. }
  58. IUIGroup uiGroup = uiComponent.GetUIGroup(uiGroupName);
  59. if (uiGroup == null)
  60. {
  61. return false;
  62. }
  63. return uiGroup.HasUIForm(assetName);
  64. }
  65. public static UGuiForm GetUIForm(this UIComponent uiComponent, UIFormId uiFormId, string uiGroupName = null)
  66. {
  67. return uiComponent.GetUIForm((int)uiFormId, uiGroupName);
  68. }
  69. public static UGuiForm GetUIForm(this UIComponent uiComponent, int uiFormId, string uiGroupName = null)
  70. {
  71. IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
  72. DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
  73. if (drUIForm == null)
  74. {
  75. return null;
  76. }
  77. string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);
  78. UIForm uiForm = null;
  79. if (string.IsNullOrEmpty(uiGroupName))
  80. {
  81. uiForm = uiComponent.GetUIForm(assetName);
  82. if (uiForm == null)
  83. {
  84. return null;
  85. }
  86. return (UGuiForm)uiForm.Logic;
  87. }
  88. IUIGroup uiGroup = uiComponent.GetUIGroup(uiGroupName);
  89. if (uiGroup == null)
  90. {
  91. return null;
  92. }
  93. uiForm = (UIForm)uiGroup.GetUIForm(assetName);
  94. if (uiForm == null)
  95. {
  96. return null;
  97. }
  98. return (UGuiForm)uiForm.Logic;
  99. }
  100. public static void CloseUIForm(this UIComponent uiComponent, UGuiForm uiForm)
  101. {
  102. uiComponent.CloseUIForm(uiForm.UIForm);
  103. }
  104. public static int? OpenUIForm(this UIComponent uiComponent, UIFormId uiFormId, object userData = null)
  105. {
  106. return uiComponent.OpenUIForm((int)uiFormId, userData);
  107. }
  108. public static int? OpenUIForm(this UIComponent uiComponent, int uiFormId, object userData = null)
  109. {
  110. IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
  111. DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
  112. if (drUIForm == null)
  113. {
  114. Log.Warning("Can not load UI form '{0}' from data table.", uiFormId.ToString());
  115. return null;
  116. }
  117. string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);
  118. if (!drUIForm.AllowMultiInstance)
  119. {
  120. if (uiComponent.IsLoadingUIForm(assetName))
  121. {
  122. return null;
  123. }
  124. if (uiComponent.HasUIForm(assetName))
  125. {
  126. return null;
  127. }
  128. }
  129. return uiComponent.OpenUIForm(assetName, drUIForm.UIGroupName, Constant.AssetPriority.UIFormAsset, drUIForm.PauseCoveredUIForm, userData);
  130. }
  131. public static void OpenDialog(this UIComponent uiComponent, DialogParams dialogParams)
  132. {
  133. if (((ProcedureBase)GameEntry.Procedure.CurrentProcedure).UseNativeDialog)
  134. {
  135. OpenNativeDialog(dialogParams);
  136. }
  137. else
  138. {
  139. uiComponent.OpenUIForm(UIFormId.DialogForm, dialogParams);
  140. }
  141. }
  142. private static void OpenNativeDialog(DialogParams dialogParams)
  143. {
  144. // TODO:这里应该弹出原生对话框,先简化实现为直接按确认按钮
  145. if (dialogParams.OnClickConfirm != null)
  146. {
  147. dialogParams.OnClickConfirm(dialogParams.UserData);
  148. }
  149. }
  150. }
  151. }