赞
踩
#region 程序集 UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null // E:\program files\Unity\Editor\Data\Managed\UnityEngine.dll #endregion using UnityEngine.Scripting; namespace UnityEngine { // // 摘要: // Ping any given IP address (given in dot notation). //Ping任何给定的IP地址(用点符号表示)。 public sealed class Ping { // // 摘要: // Perform a ping to the supplied target IP address. // 对提供的目标IP地址执行ping。 // 参数: // address: ip地址 [GeneratedByOldBindingsGeneratorAttribute] public Ping(string address); ~Ping(); // // 摘要: // Has the ping function completed? // ping功能完成了吗? public bool isDone { get; } // // 摘要: // This property contains the ping time result after isDone returns true. // 此属性包含isDone返回true后的ping时间结果。 public int time { get; } // // 摘要: // The IP target of the ping. // ping的IP目标。 public string ip { get; } [GeneratedByOldBindingsGeneratorAttribute] [ThreadAndSerializationSafeAttribute] public void DestroyPing(); } }
using System.Collections; using System.Collections.Generic; using System.Reflection.Emit; using UnityEngine; public class UnityPing : MonoBehaviour { void Start() { StartCoroutine(CheckPing()); } IEnumerator CheckPing() { //Ping服务器 Ping ping = new Ping("服务器IP"); int time = 0; Debug.Log("开始尝试连接... : " + ping.isDone); while (!ping.isDone) { yield return new WaitForSeconds(0.1f); if (nTime > 20) //2秒 { Debug.Log("连接失败 ... " + ping.time); yield break; } time ++; } if (ping.isDone) { yield return ping.time; Debug.Log("连接成功... 就没成功过"); } } }
ps:原文的大括号是中文字符,如有使用建议直接复制下文代码。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class UnityPing : MonoBehaviour { private static string s_ip = ""; private static System.Action<int> s_callback = null; private static UnityPing s_unityPing = null; private static int s_timeout = 2; public static void CreatePing(string ip, System.Action<int> callback) { if (string.IsNullOrEmpty(ip)) return; if (callback == null) return; if (s_unityPing != null) return; s_ip = ip; s_callback = callback; GameObject go = new GameObject("UnityPing"); DontDestroyOnLoad(go); s_unityPing = go.AddComponent<UnityPing>(); } /// <summary> /// 超时时间(单位秒) /// </summary> public static int Timeout { set { if (value > 0) { s_timeout = value; } } get { return s_timeout; } } private void Start() { switch (Application.internetReachability) { case NetworkReachability.ReachableViaCarrierDataNetwork: // 3G/4G case NetworkReachability.ReachableViaLocalAreaNetwork: // WIFI { StopCoroutine(this.PingConnect()); StartCoroutine(this.PingConnect()); } break; case NetworkReachability.NotReachable: // 网络不可用 default: { if (s_callback != null) { s_callback(-1); Destroy(this.gameObject); } } break; } } private void OnDestroy() { s_ip = ""; s_timeout = 20; s_callback = null; if (s_unityPing != null) { s_unityPing = null; } } IEnumerator PingConnect() { // Ping网站 Ping ping = new Ping(s_ip); int addTime = 0; int requestCount = s_timeout * 10; // 0.1秒 请求 1 次,所以请求次数是 n秒 x 10 // 等待请求返回 while (!ping.isDone) { yield return new WaitForSeconds(0.1f); // 链接失败 if (addTime > requestCount) { addTime = 0; if (s_callback != null) { s_callback(ping.time); Destroy(this.gameObject); } yield break; } addTime++; } // 链接成功 if (ping.isDone) { if (s_callback != null) { s_callback(ping.time); Destroy(this.gameObject); } yield return null; } } }
using System.Text; using UnityEngine; using UnityEngine.Profiling; public class FPS : MonoBehaviour { private float w; private float h; private Rect rect; private string sUserMemory; private string s; public bool OnMemoryGUI; private long MonoUsedM; private long AllMemory; [Range(0f, 100f)] public int MaxMonoUsedM = 50; [Range(0f, 400f)] public int MaxAllMemory = 200; public long GfxDriver; public long ReservedMemory; private StringBuilder sb = new StringBuilder(); private float updateInterval = 0.5f; private float accum; private float frames; private float timeleft; private float fps; private string FPSAAA; [Range(0f, 150f)] public int MaxFPS; private GUIStyle style = new GUIStyle(); private void Start() { timeleft = updateInterval; style.fontSize = 20; h = (float)Screen.height / 720f; w = (float)Screen.width / 1280f; style.fontSize = (int)(30f * w); GUI.color = new Color(1f, 0f, 0f); rect = new Rect(10f, 70f, 200f * w, 60f * h); } private void Update() { UpdateUsed(); UpdateFPS(); } private void UpdateUsed() { sb.Clear(); MonoUsedM = Profiler.GetMonoUsedSizeLong() / 1000000; ReservedMemory = Profiler.GetTotalReservedMemoryLong() / 1000000; AllMemory = Profiler.GetTotalAllocatedMemoryLong() / 1000000; GfxDriver = Profiler.GetAllocatedMemoryForGraphicsDriver() / 1000000; sb.Append("MonoUsed:" + MonoUsedM + "M\n"); sb.Append("GfxDriver:" + GfxDriver + "M\n"); sb.Append("AllMemory:" + AllMemory + "M\n"); sb.Append("ReservedMemory:" + ReservedMemory + "M\n"); sb.Append("UnUsedReserved:" + Profiler.GetTotalUnusedReservedMemoryLong() / 1000000 + "M\n"); sb.Append("FPS:" + FPSAAA + "\n"); } private void UpdateFPS() { timeleft -= Time.deltaTime; accum += Time.timeScale / Time.deltaTime; frames += 1f; if ((double)timeleft <= 0.0) { fps = accum / frames; FPSAAA = fps.ToString("f2"); timeleft = updateInterval; accum = 0f; frames = 0f; } } private void OnGUI() { if (OnMemoryGUI) { GUI.Label(rect, sb.ToString(), style); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。