NetStatus.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using ShadowStudio.UI;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Net.NetworkInformation;
  6. using System.Net.Sockets;
  7. using System.Runtime.CompilerServices;
  8. using UnityEngine;
  9. using UnityEngine.Events;
  10. using XRTool.Util;
  11. namespace LGS.Common_20200526
  12. {
  13. public class NetStatus : MonoBehaviour
  14. {
  15. private static NetStatus mInstant;
  16. public static NetStatus Instant {
  17. get {
  18. return mInstant;
  19. }
  20. }
  21. string m_ReachabilityText = "";
  22. string netStatus="";
  23. bool isOne = false;
  24. private event Action<NetworkReachability, NetworkReachability> NetWorkChangeCallBack;
  25. private NetworkReachability preNetWorkStatus = NetworkReachability.NotReachable;
  26. private void Awake()
  27. {
  28. if (mInstant != null) {
  29. DestroyImmediate(gameObject);
  30. return;
  31. }
  32. mInstant = this;
  33. DontDestroyOnLoad(gameObject);
  34. }
  35. private void Start()
  36. {
  37. //jo = new AndroidJavaObject("com.lgs.unitylibrary2.currentNetSpeed");
  38. //preNetWorkStatus = Application.internetReachability;
  39. //jo.Call("NetStart");
  40. SCGetNetStatus();
  41. StartCoroutine(GetNetSpeedAndstatus());
  42. }
  43. void Update()
  44. {
  45. if (isOne == false)
  46. {
  47. preNetWorkStatus = Application.internetReachability;
  48. // Debug.Log(GetType().ToString() + "赋值:" + preNetWorkStatus);
  49. isOne = true;
  50. }
  51. if (preNetWorkStatus != Application.internetReachability)
  52. {
  53. if (NetWorkChangeCallBack != null)
  54. {
  55. NetWorkChangeCallBack(preNetWorkStatus, Application.internetReachability);
  56. }
  57. preNetWorkStatus = Application.internetReachability;
  58. }
  59. }
  60. private void OnDestroy()
  61. {
  62. StopCoroutine(GetNetSpeedAndstatus());
  63. m_ReachabilityText = null;
  64. }
  65. /// <summary>
  66. /// 注册网络状态变化时触发事件,第一个参数为变化前的网络状态
  67. /// 第二个参数为变化后的网络状态
  68. /// </summary>
  69. /// <param name="callback"></param>
  70. public void RegisterNetWorkChangeCallBack(Action<NetworkReachability, NetworkReachability> callback)
  71. {
  72. NetWorkChangeCallBack += callback;
  73. }
  74. public string SCGetNetStatus()//获取网络状态
  75. {
  76. //Check if the device cannot reach the internet
  77. if (Application.internetReachability == NetworkReachability.NotReachable)//网络无法连接
  78. {
  79. //Change the Text
  80. m_ReachabilityText = "NoNetWork";
  81. }
  82. //Check if the device can reach the internet via a carrier data network
  83. else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)//网络可通过由运营商数据网络访问
  84. {
  85. m_ReachabilityText = "3G/4GNetWork";
  86. }
  87. //Check if the device can reach the internet via a LAN
  88. else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)//网络可通过WIFI或电缆访问
  89. {
  90. m_ReachabilityText = "WifiOrCableNetWork";
  91. }
  92. return m_ReachabilityText;
  93. }
  94. bool isPop;
  95. private IEnumerator GetNetSpeedAndstatus()//每经过1秒执行一次
  96. {
  97. while (true)
  98. {
  99. yield return new WaitForSecondsRealtime(1f);
  100. string status = SCGetNetStatus();
  101. switch (status)
  102. {
  103. case "NoNetWork":
  104. if(!isPop)
  105. {
  106. CommonMethod.ShowNetworkAnomalyPop(() => {
  107. isPop = false;
  108. }).GetComponent<BasePop>().OnClose(()=> {
  109. isPop = false;
  110. });
  111. }
  112. isPop = true;
  113. break;
  114. case "WifiOrCableNetWork":
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. }