- public class PintTest : MonoBehaviour {
- public string ip = string.Empty;//IP地址
- Ping ping;//Ping命令
- string label;//延迟数据
- public UILabel pingLabel;//NGUI label
- void Start()
- {
- ip = "61.135.169.121"; // 百度
- SendPing();
- }
- private bool iswork = false;//是否有网络通信
- void Update()
- {
- if (Application.internetReachability == NetworkReachability.NotReachable)//没有网络
- {
- label = "500";
- SetColor(5000);
- pingLabel.text = label.ToString();
- iswork = true;
- }
- else if (iswork||(null != ping && ping.isDone))
- {
- iswork = false;
- label = ping.time.ToString();
- SetColor(ping.time);
- ping.DestroyPing();
- ping = null;
- Invoke("SendPing", 1);//每秒Ping一次
- pingLabel.text = label.ToString();
- }
- }
- void SendPing()
- {
- ping = new Ping(ip);
- }
- /// <summary>
- /// 延迟过高,颜色变化
- /// </summary>
- /// <param name="pingValue"></param>
- void SetColor(int pingValue)
- {
- if (pingValue < 100)
- {
- pingLabel.color = Color.black;
- }
- else if (pingValue < 200)
- {
- pingLabel.color = Color.yellow;
- }
- else
- {
- pingLabel.color = Color.red;
- }
- }
- }
Unity3d:UI显示网络延迟
最新推荐文章于 2022-07-27 14:14:19 发布