如何在不降低 fps 的情况下统一检查互联网连接
How to check internet connection in unity without fps drop
我正在 Unity 中从事 VR 项目。在这个项目中,我们有 websockets。对于某些互联网问题,我们想检查 websocket 是否仍然存在或出错,如果是这种情况,我们想检查互联网连接。如果互联网再次启动,则需要重新连接到 websocket 或重新初始化它。我们现在在更新功能中使用每秒触发的计时器来执行此操作,但每秒都会有一个 fps 下降以检查互联网。还有其他解决方法吗?
public float waitTime = 1f;
private float timer;
private void Update()
{
timer += Time.deltaTime;
if (timer > waitTime)
{
//check if websocket is alive or that it must re initialized
if (webSocket != null) //check if websocket was allready initialized
{
if (isSocketError && Application.internetReachability != NetworkReachability.NotReachable) // check if an error was given by the websocket and ethernet is available again
{
CreateWebsocketSession(sessionId);
}
else if ((!webSocket.IsAlive || !webSocket.IsConnected) && Application.internetReachability != NetworkReachability.NotReachable) // check if websocket is alive and internet is available.
{
CreateWebsocketSession(sessionId);
}
}
timer = 0f;
}
}
When you call a function, it runs to completion before returning. This
effectively means that any action taking place in a function must
happen within a single frame update
我通常会检查互联网连接 ping Google(Application.internetReachability
不是确定实际连接的好方法:docs)
IEnumerator checkInternetConnection(Action<bool> action){
WWW www = new WWW("http://google.com");
yield return www;//wait for execution of this row, executed at Time x
if (www.error != null) {//executed at time x+y, where y is the execution time(i think where you have drops)
action (false);
} else {
action (true);//got internet here
}
}
在您的情况下,使用带收益率的 Coroutine()
而不是 Update()
我正在 Unity 中从事 VR 项目。在这个项目中,我们有 websockets。对于某些互联网问题,我们想检查 websocket 是否仍然存在或出错,如果是这种情况,我们想检查互联网连接。如果互联网再次启动,则需要重新连接到 websocket 或重新初始化它。我们现在在更新功能中使用每秒触发的计时器来执行此操作,但每秒都会有一个 fps 下降以检查互联网。还有其他解决方法吗?
public float waitTime = 1f;
private float timer;
private void Update()
{
timer += Time.deltaTime;
if (timer > waitTime)
{
//check if websocket is alive or that it must re initialized
if (webSocket != null) //check if websocket was allready initialized
{
if (isSocketError && Application.internetReachability != NetworkReachability.NotReachable) // check if an error was given by the websocket and ethernet is available again
{
CreateWebsocketSession(sessionId);
}
else if ((!webSocket.IsAlive || !webSocket.IsConnected) && Application.internetReachability != NetworkReachability.NotReachable) // check if websocket is alive and internet is available.
{
CreateWebsocketSession(sessionId);
}
}
timer = 0f;
}
}
When you call a function, it runs to completion before returning. This effectively means that any action taking place in a function must happen within a single frame update
我通常会检查互联网连接 ping Google(Application.internetReachability
不是确定实际连接的好方法:docs)
IEnumerator checkInternetConnection(Action<bool> action){
WWW www = new WWW("http://google.com");
yield return www;//wait for execution of this row, executed at Time x
if (www.error != null) {//executed at time x+y, where y is the execution time(i think where you have drops)
action (false);
} else {
action (true);//got internet here
}
}
在您的情况下,使用带收益率的 Coroutine()
而不是 Update()