UIFormManager.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace LoyalSoftSDK
  5. {
  6. public class UIFormManager:MonoBehaviour
  7. {
  8. private static UIFormManager instance;
  9. public static UIFormManager GetInstance()
  10. {
  11. if(instance==null)
  12. {
  13. instance = new GameObject("UIFormManager").AddComponent<UIFormManager>();
  14. DontDestroyOnLoad(instance.transform);
  15. }
  16. return instance;
  17. }
  18. public int screenWidth = 0;
  19. public int screenHeight = 0;
  20. public string screenOrientation;
  21. public Transform canvasTransform;
  22. private Transform actionLayer;
  23. private Transform floatLayer;
  24. private Transform loadingLayer;
  25. //UI窗体的路径集合
  26. private Dictionary<string, string> uiFromPathDic;
  27. //所有已加载的UI集合
  28. private Dictionary<string, BaseUIForm> allUIFromDic;
  29. //支付界面实例,要接收支付结果的
  30. public PayPanel paypanl;
  31. public bool InitCanvas()
  32. {
  33. screenWidth = Screen.width;
  34. screenHeight = Screen.height;
  35. if (screenWidth > screenHeight)
  36. {
  37. screenOrientation = "landscape";
  38. }
  39. else
  40. {
  41. screenOrientation = "portrait";
  42. }
  43. if (screenOrientation.Equals("landscape"))
  44. {
  45. ResourLoadMananger.GetInstance().LoadAsset("LoyalSoftSDK/Canvas_landscape", false);
  46. }
  47. else
  48. {
  49. ResourLoadMananger.GetInstance().LoadAsset("LoyalSoftSDK/Canvas_portrait", false);
  50. }
  51. uiFromPathDic = new Dictionary<string, string>();
  52. allUIFromDic = new Dictionary<string, BaseUIForm>();
  53. canvasTransform = GameObject.FindGameObjectWithTag("SoftCanvas").transform;
  54. canvasTransform.SetAsLastSibling();
  55. DontDestroyOnLoad(canvasTransform);
  56. actionLayer = canvasTransform.Find("ActionLayer");
  57. floatLayer= canvasTransform.Find("FloatLayer");
  58. loadingLayer = canvasTransform.Find("LoadingLayer");
  59. //初始化UI窗体预设路径数据
  60. InitUIFormsPathsData();
  61. return true;
  62. }
  63. private void InitUIFormsPathsData()
  64. {
  65. uiFromPathDic.Add("AgreementPanel", "LoyalSoftSDK/UIPrefabs/AgreementPanel");
  66. uiFromPathDic.Add("AuthenticationPanel", "LoyalSoftSDK/UIPrefabs/AuthenticationPanel");
  67. uiFromPathDic.Add("CloseFloatPanel", "LoyalSoftSDK/UIPrefabs/CloseFloatPanel");
  68. uiFromPathDic.Add("ExitGamePanel", "LoyalSoftSDK/UIPrefabs/ExitGamePanel");
  69. uiFromPathDic.Add("GetPassPanel", "LoyalSoftSDK/UIPrefabs/GetPassPanel");
  70. uiFromPathDic.Add("LoginPanel", "LoyalSoftSDK/UIPrefabs/LoginPanel");
  71. uiFromPathDic.Add("LoginSueccesPanel", "LoyalSoftSDK/UIPrefabs/LoginSueccesPanel");
  72. uiFromPathDic.Add("PayPanel", "LoyalSoftSDK/UIPrefabs/PayPanel");
  73. uiFromPathDic.Add("PolicyPanel", "LoyalSoftSDK/UIPrefabs/PolicyPanel");
  74. uiFromPathDic.Add("RegisterPanel", "LoyalSoftSDK/UIPrefabs/RegisterPanel");
  75. uiFromPathDic.Add("ResetPassPanel", "LoyalSoftSDK/UIPrefabs/ResetPassPanel");
  76. uiFromPathDic.Add("InstallationWechatPanel", "LoyalSoftSDK/UIPrefabs/InstallationWechatPanel");
  77. uiFromPathDic.Add("PayInfoPanel", "LoyalSoftSDK/UIPrefabs/PayInfoPanel");
  78. uiFromPathDic.Add("FloatPanel", "LoyalSoftSDK/UIPrefabs/FloatPanel");
  79. uiFromPathDic.Add("LoadingPanel", "LoyalSoftSDK/UIPrefabs/LoadingPanel");
  80. uiFromPathDic.Add("MessagePanel", "LoyalSoftSDK/UIPrefabs/MessagePanel");
  81. uiFromPathDic.Add("TestPanel", "LoyalSoftSDK/UIPrefabs/TestPanel");
  82. }
  83. public void ShowUIForm(string uiname, BaseUIForm _parent, Dictionary<string, string> data)
  84. {
  85. // Debug.Log("showuif1");
  86. BaseUIForm baseUIForm = null;
  87. if (string.IsNullOrEmpty(name)) return;
  88. // Debug.Log("showuif2");
  89. baseUIForm = LoadFormsToAllUIFormsCache(uiname);
  90. if (baseUIForm == null) return;
  91. // Debug.Log("showuif3");
  92. if (uiname.Equals("PayPanel"))
  93. {
  94. // Debug.Log("showuif4");
  95. paypanl = baseUIForm as PayPanel;
  96. }
  97. // Debug.Log("showuif5");
  98. baseUIForm.Display(_parent,data);
  99. }
  100. public void CloseUIForm(string uiname)
  101. {
  102. BaseUIForm baseUIForm = null;
  103. if (string.IsNullOrEmpty(name)) return;
  104. baseUIForm = LoadFormsToAllUIFormsCache(uiname);
  105. if (baseUIForm == null) return;
  106. baseUIForm.Hiding();
  107. }
  108. public void OnEscape()
  109. {
  110. bool needOpenExit = true;
  111. foreach(BaseUIForm baseUIForm in allUIFromDic.Values)
  112. {
  113. if(baseUIForm.OnEscape())
  114. {
  115. needOpenExit = false;
  116. }
  117. }
  118. if(needOpenExit)
  119. {
  120. ShowUIForm("ExitGamePanel", null, null);
  121. }
  122. }
  123. public void OnEscapeTest()
  124. {
  125. bool needOpenExit = true;
  126. foreach (BaseUIForm baseUIForm in allUIFromDic.Values)
  127. {
  128. if (baseUIForm.OnEscape())
  129. {
  130. needOpenExit = false;
  131. }
  132. }
  133. if (needOpenExit)
  134. {
  135. Dictionary<string, string> data = new Dictionary<string, string>();
  136. data.Add("action", "ExitGame");
  137. data.Add("title", "退出游戏!");
  138. //UIFormManager.GetInstance().ShowUIForm("TestPanel", null, data);
  139. }
  140. }
  141. #region
  142. private BaseUIForm LoadFormsToAllUIFormsCache(string uiname)
  143. {
  144. BaseUIForm baseUIResult = null;
  145. allUIFromDic.TryGetValue(uiname, out baseUIResult);
  146. if (baseUIResult == null)
  147. {
  148. baseUIResult = LoadUIForm(uiname);
  149. }
  150. return baseUIResult;
  151. }
  152. private BaseUIForm LoadUIForm(string uiname)
  153. {
  154. string uiFormPath = null;
  155. GameObject uiFormPrefab = null;
  156. BaseUIForm baseUiForm = null;
  157. uiFromPathDic.TryGetValue(uiname, out uiFormPath);
  158. if (!string.IsNullOrEmpty(uiFormPath))
  159. {
  160. uiFormPrefab = ResourLoadMananger.GetInstance().LoadAsset(uiFormPath, false);
  161. }
  162. if (canvasTransform != null && uiFormPrefab != null)
  163. {
  164. baseUiForm = uiFormPrefab.GetComponent<BaseUIForm>();
  165. if (baseUiForm == null)
  166. {
  167. Debug.LogError("baseUiForm==null! ,请先确认窗体预设对象上是否加载了baseUIForm的子类脚本! 参数 uiFormName=" + uiname);
  168. return null;
  169. }
  170. switch (baseUiForm.curUIType)
  171. {
  172. case UIType.Normal:
  173. uiFormPrefab.transform.SetParent(actionLayer,false);
  174. break;
  175. case UIType.Float:
  176. uiFormPrefab.transform.SetParent(floatLayer, false);
  177. break;
  178. case UIType.Loading:
  179. uiFormPrefab.transform.SetParent(loadingLayer, false);
  180. break;
  181. default:
  182. break;
  183. }
  184. baseUiForm.Init();
  185. uiFormPrefab.SetActive(false);
  186. allUIFromDic.Add(uiname, baseUiForm);
  187. return baseUiForm;
  188. }
  189. else
  190. {
  191. Debug.LogError("_TraCanvasTransfrom==null Or goCloneUIPrefabs==null!! ,Plese Check!, 参数uiname=" + uiname);
  192. }
  193. Debug.Log("出现不可以预估的错误,请检查,参数 uiname=" + uiname);
  194. return null;
  195. }
  196. #endregion
  197. //void Update()
  198. //{
  199. // if (Input.GetKeyDown(KeyCode.Escape))
  200. // {
  201. // LoyalGameSDK.Instance.ExitGame(null);
  202. // }
  203. //}
  204. }
  205. }