UI_LoadingWindow.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System;
  5. /// <summary>
  6. /// 加载动画
  7. /// </summary>
  8. public class UI_LoadingWindow : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 窗体单例
  12. /// </summary>
  13. public static UI_LoadingWindow pInit = null;
  14. /// <summary>
  15. /// 根节点
  16. /// </summary>
  17. private GameObject mRoot = null;
  18. /// <summary>
  19. /// 加载场景的图片
  20. /// </summary>
  21. private GameObject mLoadScene = null;
  22. /// <summary>
  23. /// 加载场景的动圈
  24. /// </summary>
  25. private GameObject mLoadPanel = null;
  26. /// <summary>
  27. /// loading 圈圈Transform
  28. /// </summary>
  29. public Transform mCircularTran = null;
  30. /// <summary>
  31. /// loading 圈圈Transform
  32. /// </summary>
  33. public Transform mCircularTran2 = null;
  34. /// <summary>
  35. /// 进度条
  36. /// </summary>
  37. private GameObject mProgressObject = null;
  38. /// <summary>
  39. /// 进度条
  40. /// </summary>
  41. private Image mProgressSlider = null;
  42. /// <summary>
  43. /// 当前正在显示的背景
  44. /// </summary>
  45. private Image mCurtBg = null;
  46. /// <summary>
  47. /// 底部蓝光背景
  48. /// </summary>
  49. private Image mButtomImage = null;
  50. /// <summary>
  51. /// 当前正在显示的背景
  52. /// </summary>
  53. private GameObject mCurtBgObj = null;
  54. /// <summary>
  55. /// 当前进度条跟随点
  56. /// </summary>
  57. private GameObject mProgressSliderDot = null;
  58. /// <summary>
  59. /// 当前进度条进度文字
  60. /// </summary>
  61. private Text mProgressText = null;
  62. /// <summary>
  63. /// 场景模式
  64. /// </summary>
  65. private bool sceneMode = false;
  66. /// <summary>
  67. /// 界面
  68. /// </summary>
  69. private bool uiMode = false;
  70. /// <summary>
  71. /// 渐隐时间
  72. /// </summary>
  73. private float mFadeTime = 1;
  74. /// <summary>
  75. /// 起始位置
  76. /// </summary>
  77. private float mStart = -850;
  78. /// <summary>
  79. /// 终点位置
  80. /// </summary>
  81. private float mEnt = 870;
  82. /// <summary>
  83. /// 速度
  84. /// </summary>
  85. private float mSpeed = 0.1f;
  86. /// <summary>
  87. /// 目标进度
  88. /// </summary>
  89. private float prog = 0;
  90. /// <summary>
  91. /// 当前进度
  92. /// </summary>
  93. private float curt = 0;
  94. private int LoadingWinSmallTipsIndex = 0;
  95. /// <summary>
  96. /// 临时用的tips数组
  97. /// </summary>
  98. readonly string[] smalltips = new string[] { "小贴士:夜神月触发极限闪避后,大幅降低所有怪物的攻击与移动速度",
  99. "小贴士:言灵师品阶提升,可以提高各基本属性的成长值",
  100. "小贴士:更换更为精良的武器,可以提升言灵师的战斗力",
  101. "小贴士:战斗中灵活使用言灵师的闪避技,躲避敌人攻击",
  102. "小贴士:卡琳娜的必杀技可以为所有队员回复一定生命值",
  103. "小贴士:言灵分为左、右、中三派,分别代表攻击、防御和攻守兼备",
  104. };
  105. private string mTipContent = "加载场景";
  106. /// <summary>
  107. /// 显示tips的变量
  108. /// </summary>
  109. private float elips = 0f;
  110. /// <summary>
  111. ///
  112. /// </summary>
  113. private Text mTipsText = null;
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. private bool isInitFont = false;
  118. /// <summary>
  119. /// 获取单键
  120. /// </summary>
  121. /// <returns>单键实例</returns>
  122. public static UI_LoadingWindow Instance()
  123. {
  124. return pInit;
  125. }
  126. /// <summary>
  127. /// 初始化
  128. /// </summary>
  129. private void Awake()
  130. {
  131. pInit = this;
  132. // 初始化
  133. mRoot = transform.Find("Root").gameObject;
  134. mLoadScene = transform.Find("Root/LoadScene").gameObject;
  135. mLoadPanel = transform.Find("Root/LoadPanel").gameObject;
  136. mProgressObject = transform.Find("Root/LoadScene/ProgressSlider").gameObject;
  137. mProgressSliderDot = transform.Find("Root/LoadScene/ProgressSlider/ProgressText").gameObject;
  138. mProgressSliderDot.transform.localPosition = new Vector3(mStart, 86, 0);
  139. mCircularTran = transform.Find("Root/LoadPanel/cricle");
  140. mCircularTran2 = transform.Find("Root/LoadPanel/cricle (1)");
  141. mLoadScene.SetActive(false);
  142. mLoadPanel.SetActive(false);
  143. elips = 2.5f;
  144. DontDestroyOnLoad(gameObject);
  145. }
  146. /// <summary>
  147. /// 设置 UI 的字体
  148. /// </summary>
  149. /// <param name="uiObj"> UI对象 </param>
  150. public void SetFont(GameObject uiObj)
  151. {
  152. if (null == uiObj)
  153. {
  154. Debug.LogError("PanelHelper 设置字体,UI 为空,中断");
  155. return;
  156. }
  157. // 获取所有的 Text
  158. Text[] allText = uiObj.GetComponentsInChildren<Text>(true);
  159. // 设置字体
  160. for (int i = 0; i < allText.Length; i++)
  161. {
  162. allText[i].font = FontManager.Instance().CurFont;
  163. allText[i].fontStyle = FontStyle.Normal;
  164. }
  165. }
  166. /// <summary>
  167. /// 更新百分比
  168. /// </summary>
  169. /// <param name="progress"></param>
  170. /// <param name="speedCorrect">速度修正</param>
  171. public void SetProgress(int progress, string content = "")
  172. {
  173. if (!isInitFont)
  174. {
  175. SetFont(this.gameObject);
  176. }
  177. if (sceneMode)
  178. {
  179. this.prog = progress * 1.0f / 100;
  180. }
  181. }
  182. /// <summary>
  183. /// 叠加百分比
  184. /// </summary>
  185. /// <param name="progress"></param>
  186. /// <param name="speedCorrect">速度修正</param>
  187. public void SetAddProgress(int progress)
  188. {
  189. if (!isInitFont)
  190. {
  191. SetFont(this.gameObject);
  192. }
  193. if (sceneMode)
  194. {
  195. float newPrg = this.prog + progress * 1.0f / 100;
  196. if (newPrg >= 1)
  197. {
  198. this.prog = 1;
  199. }
  200. else
  201. {
  202. this.prog = newPrg;
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// 自动持续时间
  208. /// </summary>
  209. /// <param name="_continueTime"></param>
  210. public void SetAutoProgress(float _continueTime, Action ac)
  211. {
  212. Debug.LogError(Time.realtimeSinceStartup);
  213. if (_continueTime <= 0)
  214. {
  215. this.isAuto = false;
  216. this.curt = 1;
  217. this.prog = 1;
  218. }
  219. else
  220. {
  221. this.isAutoOverAction = ac;
  222. this.isAuto = true;
  223. this.curt = 0;
  224. this.prog = 1;
  225. this.mSpeed = 1 / (_continueTime / Time.deltaTime);
  226. }
  227. }
  228. private Action isAutoOverAction;
  229. private bool isAuto = false;
  230. /// <summary>
  231. /// 更新
  232. /// </summary>
  233. private void Update()
  234. {
  235. if (this.isAuto)
  236. {
  237. this.curt += this.mSpeed;
  238. Debug.Log(this.curt);
  239. if (this.curt >= 0.95)
  240. {
  241. this.curt = 1;
  242. this.isAuto = false;
  243. if (this.isAutoOverAction != null)
  244. {
  245. this.isAutoOverAction.Invoke();
  246. }
  247. }
  248. }
  249. // 场景模式(带Loading图)
  250. if (sceneMode)
  251. {
  252. this.curt = Mathf.Lerp(this.curt, this.prog, prog < curt || prog >= 1 ? 1 : mSpeed);
  253. mProgressSlider.fillAmount = this.curt;
  254. mProgressText.text = (int)(this.curt * 100) + "%";
  255. mProgressSliderDot.transform.localPosition = new Vector3(Mathf.Lerp(mStart, mEnt, this.curt), 86, 0);
  256. //mTipsText.text = this.mTipContent;
  257. elips += Time.deltaTime;
  258. if (elips > 2.5f)
  259. {
  260. // 每隔1秒更新tips
  261. elips = 0f;
  262. LoadingWinSmallTipsIndex++;
  263. if (LoadingWinSmallTipsIndex >= smalltips.Length)
  264. {
  265. LoadingWinSmallTipsIndex = 0;
  266. }
  267. mTipsText.text = smalltips[LoadingWinSmallTipsIndex];
  268. SetAddProgress(2);
  269. }
  270. }
  271. // UI模式(带小加载圈)
  272. if (uiMode)
  273. {
  274. mCircularTran.Rotate(-Vector3.forward * Time.deltaTime * 360);
  275. mCircularTran2.Rotate(Vector3.forward * Time.deltaTime * 360);
  276. }
  277. }
  278. /// <summary>
  279. /// 显示
  280. /// </summary>
  281. /// <param name="_type">0=透明背景动画(场景) 1=无背景转圈(窗体) 2=背景动画(战斗)</param>
  282. public void Show(int _type = 2)
  283. {
  284. switch (_type)
  285. {
  286. case 0:
  287. break;
  288. case 1:
  289. uiMode = true;
  290. this.mLoadPanel.SetActive(true);
  291. break;
  292. case 2:
  293. sceneMode = true;
  294. this.mProgressObject.SetActive(true);
  295. // 显示背景
  296. int mBgIndex = UnityEngine.Random.Range(0, 3);
  297. this.mCurtBgObj = transform.Find("Root/LoadScene/BgLst/Image" + mBgIndex).gameObject;
  298. this.mCurtBgObj.SetActive(true);
  299. this.mCurtBgObj.GetComponent<Image>().CrossFadeAlpha(1, mFadeTime, true);
  300. this.mLoadScene.SetActive(true);
  301. this.InitProgress();
  302. if (this.mTipsText == null)
  303. {
  304. this.mTipsText = transform.Find("Root/LoadScene/TextTip").gameObject.GetComponent<Text>();
  305. this.mTipsText.text = "0%";
  306. }
  307. this.mButtomImage = transform.Find("Root/LoadScene/ButtomImage").gameObject.GetComponent<Image>();
  308. this.mButtomImage.CrossFadeAlpha(1, mFadeTime, true);
  309. //mTipsText.text = smalltips[LocalSettings.LoadingWinSmallTipsIndex];
  310. break;
  311. }
  312. if (!isInitFont)
  313. {
  314. SetFont(this.gameObject);
  315. }
  316. }
  317. /// <summary>
  318. ///
  319. /// </summary>
  320. private void InitProgress()
  321. {
  322. if (this.mProgressSlider == null)
  323. {
  324. this.mProgressSlider = transform.Find("Root/LoadScene/ProgressSlider/ImageProgress").gameObject.GetComponent<Image>();
  325. }
  326. mProgressSlider.fillAmount = 0;
  327. if (this.mProgressText == null)
  328. {
  329. this.mProgressText = transform.Find("Root/LoadScene/ProgressSlider/ProgressText/Text").gameObject.GetComponent<Text>();
  330. }
  331. this.mProgressText.text = "0%";
  332. }
  333. /// <summary>
  334. /// 隐藏
  335. /// </summary>
  336. public void Hide(int type = 2)
  337. {
  338. switch (type)
  339. {
  340. case 1:
  341. uiMode = false;
  342. this.mLoadPanel.SetActive(false);
  343. break;
  344. case 2:
  345. if (this.mProgressSlider != null)
  346. {
  347. sceneMode = false;
  348. this.mLoadScene.SetActive(false);
  349. this.curt = 0;
  350. this.prog = 0;
  351. this.mProgressSlider.fillAmount = 0;
  352. this.mProgressText.text = "0%";
  353. this.mProgressSliderDot.transform.localPosition = new Vector3(mStart, 86, 0);
  354. this.mProgressObject.SetActive(false);
  355. this.mButtomImage.CrossFadeAlpha(0, mFadeTime, true);
  356. if (this.mCurtBgObj != null)
  357. {
  358. this.mCurtBgObj.GetComponent<Image>().CrossFadeAlpha(0, mFadeTime, true);
  359. }
  360. this.mTipsText.text = "0%";
  361. Invoke("ClosePanel", mFadeTime);
  362. }
  363. break;
  364. case 3:
  365. if (this.mProgressSlider != null)
  366. {
  367. this.curt = 0;
  368. this.prog = 0;
  369. this.mProgressSlider.fillAmount = 0;
  370. this.mProgressText.text = "0%";
  371. this.mProgressSliderDot.transform.localPosition = new Vector3(mStart, 86, 0);
  372. this.mProgressObject.SetActive(false);
  373. this.mButtomImage.CrossFadeAlpha(0, mFadeTime, true);
  374. if (this.mCurtBgObj != null)
  375. {
  376. this.mCurtBgObj.GetComponent<Image>().CrossFadeAlpha(0, mFadeTime, true);
  377. }
  378. this.mTipsText.text = "0%";
  379. Invoke("ClosePanel", 0.2f);
  380. }
  381. break;
  382. }
  383. }
  384. /// <summary>
  385. ///
  386. /// </summary>
  387. void ClosePanel()
  388. {
  389. this.mCurtBgObj.gameObject.SetActive(false);
  390. }
  391. }