LauncherView.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using FairyGUI;
  2. using UI.Launcher;
  3. using UnityEngine;
  4. namespace GFGGame
  5. {
  6. public class LauncherView
  7. {
  8. private static LauncherView m_Instance = null;
  9. /// <summary>
  10. /// 单例
  11. /// </summary>
  12. public static LauncherView Instance
  13. {
  14. get
  15. {
  16. if (m_Instance == null)
  17. {
  18. m_Instance = new LauncherView();
  19. }
  20. return m_Instance;
  21. }
  22. }
  23. private UI_LauncherUI _ui;
  24. private GameObject _gameObject;
  25. private GoWrapper _wrapper;
  26. private string[] resNames = { "11", "33" };
  27. /// <summary>
  28. /// 每1%耗时,单位秒
  29. /// </summary>
  30. private const float SPEED = 0.01f;
  31. /// <summary>
  32. /// 界面是否打开状态
  33. /// </summary>
  34. private bool isOpen = false;
  35. /// <summary>
  36. /// FairyGUI包名
  37. /// </summary>
  38. private string _packageName;
  39. #region private
  40. private void Dispose()
  41. {
  42. if (_gameObject != null)
  43. {
  44. GameObject.Destroy(_gameObject);
  45. _gameObject = null;
  46. }
  47. if (_wrapper != null)
  48. {
  49. _wrapper.Dispose();
  50. _wrapper = null;
  51. }
  52. UIPackage.RemovePackage("UI/" + _packageName);
  53. _ui.Dispose(true);
  54. _ui = null;
  55. if (_gameObject != null)
  56. {
  57. GameObject.Destroy(_gameObject);
  58. }
  59. }
  60. #endregion
  61. public LauncherView()
  62. {
  63. _packageName = UI_LauncherUI.PACKAGE_NAME;
  64. UIPackage.AddPackage("UI/" + _packageName + "/" + _packageName);
  65. _ui = UI_LauncherUI.Create();
  66. _ui.target.MakeFullScreen();
  67. _ui.target.AddRelation(GRoot.inst, RelationType.Size);
  68. _ui.m_txtVersion.text = Application.version;
  69. string resPath = "Effect/ui_dljm/ui_dljm_jdt_tw";
  70. var prefab = Resources.Load<GameObject>(resPath);
  71. _gameObject = GameObject.Instantiate(prefab);
  72. var assetDisposer = _gameObject.AddComponent<AssetReleaser>();
  73. assetDisposer.resPath = resPath;
  74. _gameObject.transform.localScale = new Vector3(100, 100, 100);
  75. _wrapper = new GoWrapper(_gameObject);
  76. _ui.m_holder.SetNativeObject(_wrapper);
  77. }
  78. /// <summary>
  79. /// 设置版本号文字
  80. /// </summary>
  81. /// <param name="version"></param>
  82. public void SetVersion(string version)
  83. {
  84. if (!isOpen)
  85. {
  86. return;
  87. }
  88. _ui.m_txtVersion.text = version;
  89. }
  90. /// <summary>
  91. /// 设置描述文字
  92. /// </summary>
  93. /// <param name="desc"></param>
  94. public void SetDesc(string desc, string descRight = "")
  95. {
  96. if (!isOpen)
  97. {
  98. return;
  99. }
  100. string str = string.Format("{0} {1}", desc, descRight);// + descRight;
  101. _ui.m_txtDescLeft.text = str;
  102. // _ui.m_txtDescLeft.text = desc ?? "";
  103. // _ui.m_txtDescRight.text = descRight ?? "";
  104. }
  105. /// <summary>
  106. /// 设置进度0-100
  107. /// </summary>
  108. /// <param name="progress"></param>
  109. /// <param name="callback"></param>
  110. public void SetProgress(long progress, GTweenCallback callback = null)
  111. {
  112. if (!isOpen)
  113. {
  114. return;
  115. }
  116. double oldValule;
  117. GTweener twener = GTween.GetTween(_ui.m_progressBar1, TweenPropType.Progress);
  118. if (twener != null)
  119. {
  120. oldValule = twener.value.d;
  121. }
  122. else
  123. {
  124. oldValule = _ui.m_progressBar1.value;
  125. }
  126. if (progress < oldValule)
  127. {
  128. _ui.m_progressBar1.value = progress;
  129. float posX = _ui.m_progressBar1.width * (progress / 100) + _ui.m_progressBar1.x;
  130. _ui.m_imgAni.x = Mathf.Min(posX, _ui.m_progressBar1.width + _ui.m_progressBar1.x);
  131. callback?.Invoke();
  132. }
  133. else
  134. {
  135. float duration = (float)(progress - oldValule) * SPEED;
  136. GTweener gtweener = _ui.m_progressBar1.TweenValue(progress, duration).OnUpdate((GTweener t) =>
  137. {
  138. float posX = _ui.m_progressBar1.width * (t.value.x / 100) + _ui.m_progressBar1.x;
  139. _ui.m_imgAni.x = Mathf.Min(posX, _ui.m_progressBar1.width + _ui.m_progressBar1.x);
  140. });
  141. if (callback != null)
  142. {
  143. gtweener.OnComplete(callback);
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// 打开界面
  149. /// </summary>
  150. public void Open()
  151. {
  152. if (isOpen)
  153. {
  154. return;
  155. }
  156. _ui.m_progressBar1.value = 0;
  157. _ui.m_txtDescLeft.text = "";
  158. _ui.m_txtDescRight.text = "";
  159. GRoot.inst.AddChild(_ui.target);
  160. isOpen = true;
  161. }
  162. /// <summary>
  163. /// 关闭界面
  164. /// </summary>
  165. /// <param name="toDestroy"></param>
  166. public void Close(bool toDestroy = false)
  167. {
  168. if (!isOpen)
  169. {
  170. return;
  171. }
  172. isOpen = false;
  173. _ui.target.RemoveFromParent();
  174. if (toDestroy)
  175. {
  176. Dispose();
  177. }
  178. }
  179. }
  180. }