GlobalMgrUtil.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.U2D;
  6. namespace RayNeo.Native
  7. {
  8. /// <summary>
  9. /// 全局管理类
  10. /// </summary>
  11. public class GlobalMgrUtil : MonoSingleton<GlobalMgrUtil>
  12. {
  13. public string PREFIX = "[MercuryX2]";
  14. #region 轮询网络
  15. public Action<bool> IsInternetUnAvailable;
  16. private void InternetDetection()
  17. {
  18. bool CurrentInternetStatus = Application.internetReachability == NetworkReachability.NotReachable;
  19. IsInternetUnAvailable?.Invoke(CurrentInternetStatus);
  20. }
  21. /// <summary>
  22. /// 启动网络检测
  23. /// 每隔2秒检测一次
  24. /// </summary>
  25. public void StartInternetDetection()
  26. {
  27. InvokeRepeating("InternetDetection", 0, 2);
  28. }
  29. /// <summary>
  30. /// 关闭网络监测
  31. /// </summary>
  32. public void StopInternetDetection()
  33. {
  34. Debug.Log("[MercuryX2]:停止网络状态轮询");
  35. if (IsInvoking("InternetDetection"))
  36. {
  37. CancelInvoke("InternetDetection");
  38. }
  39. }
  40. #endregion
  41. #region 外部轮询
  42. private Action ExternalPoll;
  43. private void Polling()
  44. {
  45. ExternalPoll?.Invoke();
  46. }
  47. public void StartPoll(Action ExternalPoll, float Interval)
  48. {
  49. this.ExternalPoll = ExternalPoll;
  50. InvokeRepeating("Polling", 0, Interval);
  51. }
  52. public void StopPoll()
  53. {
  54. if (IsInvoking("Polling"))
  55. {
  56. CancelInvoke("Polling");
  57. }
  58. }
  59. #endregion
  60. #region 加载图集
  61. public SpriteAtlas LoadSpriteFromAssetBundle(string AssetPath, string AtlasName)
  62. {
  63. AssetBundle Bundle = AssetBundle.LoadFromFile(AssetPath);
  64. //从AssetBundle包中加载图集
  65. SpriteAtlas Atlas = Bundle.LoadAsset<SpriteAtlas>(AtlasName);
  66. return Atlas;
  67. }
  68. #endregion
  69. }
  70. }