我正在使用以下脚本检查互联网连接:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckInternets : MonoBehaviour
{
private string url = "some photo on my google drive";
private string wwww;
public static bool InternetON = false;
// Start is called before the first frame update
void Start()
{
StartCoroutine(CheckConnection());
}
IEnumerator CheckConnection()
{
var www = new WWW(url);
yield return www;
if (www.isDone && www.bytesDownloaded > 0)
{
InternetON = true;
Debug.Log("MAIN Script Check :: Online! :)");
}
if (www.isDone && www.bytesDownloaded == 0)
{
InternetON = false;
Debug.LogWarning("MAIN Script Check :: Offline! :(");
}
}
}
在我的(Android)手机上试了这个,连接到移动数据(没有),它运行得很好。
一旦我在其他脚本中引用InternetON
bool,就会出现问题,即所述脚本总是将其读作false
(如果我将其设置为CheckInternets.cs中的true
),那么我的预感是我引用它错了,或者我我没有正确使用bool,但无法弄清楚如何。
其他脚本示例:
void Start()
{
if (CheckInternets.InternetON)
{
//do some stuff
Debug.Log("OTHER Script Check :: Online! :)");
}
else
{
//do some other stuff
Debug.Log("OTHER Script Check :: Offline! :(");
}
}
在这种情况下,如果我在线,我的控制台将会显示:
如果我处于离线状态,它会为两个人说离线。
有什么建议?干杯!
StartCoroutine(CheckConnection());
在coroutine开始Start()
......但是它会在收到MoveNext()
电话后收到Update
电话后才会被执行
来自Execution Order Documentation:
在
Update
函数返回后运行正常的协同程序更新。
Update
在你的另一个剧本中检查Start
中的bool
时完成场景的所有Start
调用后才被调用CheckConnection()
甚至不会执行第一个yield
。
相反,你必须等到检查完成。
以下是使用IEnumerator Start
如何做到这一点的一些方法
Unity允许这样做,以便延迟Start
直到某些东西完成...只是你想做的事情:
yield
CheckConnection
您可以简单地yield return
在协程中的任何其他IEnumerator
,以便等到另一个完成
public class CheckInternets : MonoBehaviour
{
private static string url = "some photo on my google drive";
private static string wwww;
public static bool InternetON = false;
public static IEnumerator CheckConnection()
{
var www = new WWW(url);
yield return www;
if (www.isDone && www.bytesDownloaded > 0)
{
InternetON = true;
Debug.Log("MAIN Script Check :: Online! :)");
}
if (www.isDone && www.bytesDownloaded == 0)
{
InternetON = false;
Debug.LogWarning("MAIN Script Check :: Offline! :(");
}
}
}
和
public class OtherScript : MonoBehaviour
{
IEnumerator Start()
{
// starts an internet check and waits until it finished
yield return CheckInternets.CheckConnection();
if (CheckInternets.InternetON)
{
//do some stuff
Debug.Log("OTHER Script Check :: Online! :)");
}
else
{
//do some other stuff
Debug.Log("OTHER Script Check :: Offline! :(");
}
}
}
checkFinished
并使用WaitUntil
public class CheckInternets : MonoBehaviour
{
private string url = "some photo on my google drive";
private wwww;
public static bool InternetON = false;
public static bool IsFinished = false;
// Start is called before the first frame update
private void Start()
{
StartCoroutine(CheckConnection());
}
public IEnumerator CheckConnection()
{
IsFinished = false;
var www = new WWW(url);
yield return www;
if (www.isDone && www.bytesDownloaded > 0)
{
InternetON = true;
Debug.Log("MAIN Script Check :: Online! :)");
}
if (www.isDone && www.bytesDownloaded == 0)
{
InternetON = false;
Debug.LogWarning("MAIN Script Check :: Offline! :(");
}
IsFinished = true;
}
}
并做
public class OtherScript : MonoBehaviour
{
private IEnumerator Start()
{
// starts an internet check and waits until it finished
yield return new WaitUntil(() => CheckInternets.IsFinished);
if (CheckInternets.InternetON)
{
//do some stuff
Debug.Log("OTHER Script Check :: Online! :)");
}
else
{
//do some other stuff
Debug.Log("OTHER Script Check :: Offline! :(");
}
}
}
Action
,这样您就可以在不使用IEnumerator
的情况下等待结果
public class CheckInternets : MonoBehaviour
{
private string url = "some photo on my google drive";
private string wwww;
// now you wouldn't need this anymore and could make it
// a local variable in the CheckConnectionRoutine
public static bool InternetON = false;
public void CheckConnection(Action<bool> callback)
{
StartCoroutine(CheckConnectionRoutine(callback));
}
private IEnumerator CheckConnectionRoutine(Action<bool> callback)
{
var www = new WWW(url);
yield return www;
if (www.isDone && www.bytesDownloaded > 0)
{
InternetON = true;
Debug.Log("MAIN Script Check :: Online! :)");
}
if (www.isDone && www.bytesDownloaded == 0)
{
InternetON = false;
Debug.LogWarning("MAIN Script Check :: Offline! :(");
}
callback?.Invoke(InternetON);
}
}
并使用方法使用它
public class OtherScript : MonoBehaviour
{
private void Start()
{
// somehow get a reference instead of using static
// that's always the cleaner way
var check = FindObjectOfType<CheckInternets>();
// starts an internet check
check?.CheckConnection(OnConnectionCheckFinished);
}
private void OnConnectionCheckFinished(bool isConnected)
{
if (isConnected)
{
//do some stuff
Debug.Log("OTHER Script Check :: Online! :)");
}
else
{
//do some other stuff
Debug.Log("OTHER Script Check :: Offline! :(");
}
}
}
或直接使用lambda表达式
public class OtherScript : MonoBehaviour
{
private void Start()
{
// somehow get a reference instead of using static
// that's always the cleaner way
var check = FindObjectOfType<CheckInternets>();
// starts an internet check
check?.CheckConnection((isConnected) =>
{
if (isConnected)
{
//do some stuff
Debug.Log("OTHER Script Check :: Online! :)");
}
else
{
//do some other stuff
Debug.Log("OTHER Script Check :: Offline! :(");
}
});
}
}