在其他脚本中引用时,Bool返回不同的值

问题描述 投票:0回答:1

我正在使用以下脚本检查互联网连接:

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! :(");
    }
}

在这种情况下,如果我在线,我的控制台将会显示:

  • 其他脚本检查::离线! :(
  • MAIN Script Check ::在线! :)

如果我处于离线状态,它会为两个人说离线。

有什么建议?干杯!

c# unity3d
1个回答
0
投票

StartCoroutine(CheckConnection());coroutine开始Start() ......但是它会在收到MoveNext()电话后收到Update电话后才会被执行

来自Execution Order Documentation

Update函数返回后运行正常的协同程序更新。

Update在你的另一个剧本中检查Start中的bool时完成场景的所有Start调用后才被调用CheckConnection()甚至不会执行第一个yield


相反,你必须等到检查完成。

以下是使用IEnumerator Start如何做到这一点的一些方法 Unity允许这样做,以便延迟Start直到某些东西完成...只是你想做的事情:

  1. 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! :("); } } }
  2. Addistional标志 添加一个标志,例如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! :("); } } }
  3. 打回来 添加回调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! :("); } }); } }
© www.soinside.com 2019 - 2024. All rights reserved.