LauncherView.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System.Collections;
  2. using FairyGUI;
  3. using UI.Launcher;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public class LauncherView
  8. {
  9. private static LauncherView m_Instance = null;
  10. /// <summary>
  11. /// 单例
  12. /// </summary>
  13. public static LauncherView Instance
  14. {
  15. get
  16. {
  17. if (m_Instance == null)
  18. {
  19. m_Instance = new LauncherView();
  20. }
  21. return m_Instance;
  22. }
  23. }
  24. private UI_LauncherUI _ui;
  25. private GameObject _gameObject;
  26. private GoWrapper _wrapper;
  27. private GameObject _gameObject1;
  28. private GoWrapper _wrapper1;
  29. private string[] resNames = { "11", "33" };
  30. /// <summary>
  31. /// 每1%耗时,单位秒
  32. /// </summary>
  33. private const float SPEED = 0.01f;
  34. /// <summary>
  35. /// 界面是否打开状态
  36. /// </summary>
  37. private bool isOpen = false;
  38. /// <summary>
  39. /// FairyGUI包名
  40. /// </summary>
  41. private string _packageName;
  42. #region private
  43. private void Dispose()
  44. {
  45. if (_gameObject != null)
  46. {
  47. GameObject.Destroy(_gameObject);
  48. _gameObject = null;
  49. _wrapper.Dispose();
  50. _wrapper = null;
  51. }
  52. if (_gameObject1 != null)
  53. {
  54. GameObject.Destroy(_gameObject1);
  55. _gameObject1 = null;
  56. _wrapper1.Dispose();
  57. _wrapper1 = null;
  58. }
  59. UIPackage.RemovePackage("UI/" + _packageName + "/" + _packageName);
  60. _ui.Dispose(true);
  61. _ui = null;
  62. }
  63. #endregion
  64. public LauncherView()
  65. {
  66. _packageName = UI_LauncherUI.PACKAGE_NAME;
  67. UIPackage.AddPackage("UI/" + _packageName + "/" + _packageName);
  68. _ui = UI_LauncherUI.Create();
  69. _ui.target.MakeFullScreen();
  70. _ui.target.AddRelation(GRoot.inst, RelationType.Size);
  71. _ui.m_txtVersion.text = Application.version;
  72. // 启动异步加载协程
  73. CoroutineHelper.Instance.StartCoroutine(LoadResourcesAsync());
  74. }
  75. private IEnumerator LoadResourcesAsync()
  76. {
  77. string resPath = "Effect/ui_login/Game_Open_jindut_T";
  78. string resPath1 = "Effect/ui_login/Game_Open_jindut";
  79. // 异步加载第一个资源
  80. ResourceRequest request = Resources.LoadAsync<GameObject>(resPath);
  81. yield return request;
  82. if (request.asset != null)
  83. {
  84. var prefab = request.asset as GameObject;
  85. _gameObject = GameObject.Instantiate(prefab);
  86. _gameObject.transform.localScale = new Vector3(100, 100, 100);
  87. _wrapper = new GoWrapper(_gameObject);
  88. _ui.m_progressBar1.m_holderStar.SetNativeObject(_wrapper);
  89. }
  90. else
  91. {
  92. Debug.LogError("Failed to load resource: " + resPath);
  93. }
  94. // 异步加载第二个资源
  95. ResourceRequest request1 = Resources.LoadAsync<GameObject>(resPath1);
  96. yield return request1;
  97. if (request1.asset != null)
  98. {
  99. var prefab1 = request1.asset as GameObject;
  100. _gameObject1 = GameObject.Instantiate(prefab1);
  101. _gameObject1.transform.localScale = new Vector3(100, 100, 100);
  102. _wrapper1 = new GoWrapper(_gameObject1);
  103. _ui.m_progressBar1.m_bar1.m_holder.SetNativeObject(_wrapper1);
  104. }
  105. else
  106. {
  107. Debug.LogError("Failed to load resource: " + resPath1);
  108. }
  109. _ui.m_groupBar.visible = false;
  110. }
  111. /// <summary>
  112. /// 设置版本号文字
  113. /// </summary>
  114. /// <param name="version"></param>
  115. public void SetVersion(string version)
  116. {
  117. if (!isOpen)
  118. {
  119. return;
  120. }
  121. _ui.m_txtVersion.text = version;
  122. }
  123. /// <summary>
  124. /// 设置描述文字
  125. /// </summary>
  126. /// <param name="desc"></param>
  127. public void SetDesc(string desc, string descRight = "", bool showBar = false)
  128. {
  129. if (!isOpen)
  130. {
  131. return;
  132. }
  133. _ui.m_groupBar.visible = showBar;
  134. string str = string.Format("{0} {1}", desc, descRight); // + descRight;
  135. _ui.m_txtDescLeft.text = str;
  136. // _ui.m_txtDescLeft.text = desc ?? "";
  137. // _ui.m_txtDescRight.text = descRight ?? "";
  138. }
  139. /// <summary>
  140. /// 设置进度0-100
  141. /// </summary>
  142. /// <param name="progress"></param>
  143. /// <param name="callback"></param>
  144. public void SetProgress(long progress, GTweenCallback callback = null)
  145. {
  146. if (!isOpen)
  147. {
  148. return;
  149. }
  150. double oldValule;
  151. GTweener twener = GTween.GetTween(_ui.m_progressBar1, TweenPropType.Progress);
  152. if (twener != null)
  153. {
  154. oldValule = twener.value.d;
  155. }
  156. else
  157. {
  158. oldValule = _ui.m_progressBar1.target.value;
  159. }
  160. if (progress < oldValule)
  161. {
  162. _ui.m_progressBar1.target.value = progress;
  163. float posX = _ui.m_progressBar1.target.width * (progress / 100);
  164. // _ui.m_progressBar1.m_holder.x = Mathf.Min(posX, _ui.m_progressBar1.target.width);
  165. _ui.m_progressBar1.m_bar1.m_mask.width = posX;
  166. _ui.m_progressBar1.m_holderStar.x = posX;
  167. callback?.Invoke();
  168. }
  169. else
  170. {
  171. float duration = (float)(progress - oldValule) * SPEED;
  172. GTweener gtweener = _ui.m_progressBar1.target.TweenValue(progress, duration).OnUpdate((GTweener t) =>
  173. {
  174. float posX = _ui.m_progressBar1.target.width * (t.value.x / 100);
  175. // _ui.m_progressBar1.m_holder.x = Mathf.Min(posX, _ui.m_progressBar1.target.width);
  176. _ui.m_progressBar1.m_bar1.m_mask.width = posX;
  177. _ui.m_progressBar1.m_holderStar.x = posX;
  178. });
  179. if (callback != null)
  180. {
  181. gtweener.OnComplete(callback);
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// 打开界面
  187. /// </summary>
  188. public void Open()
  189. {
  190. if (isOpen)
  191. {
  192. return;
  193. }
  194. _ui.m_progressBar1.target.value = 0;
  195. _ui.m_txtDescLeft.text = "";
  196. _ui.m_txtDescRight.text = "";
  197. GRoot.inst.AddChild(_ui.target);
  198. isOpen = true;
  199. }
  200. /// <summary>
  201. /// 关闭界面
  202. /// </summary>
  203. /// <param name="toDestroy"></param>
  204. public void Close(bool toDestroy = true)
  205. {
  206. if (!isOpen)
  207. {
  208. return;
  209. }
  210. isOpen = false;
  211. _ui.target.RemoveFromParent();
  212. if (toDestroy)
  213. {
  214. Dispose();
  215. }
  216. }
  217. }
  218. }