【问题标题】:How to update gps in unity?如何统一更新gps?
【发布时间】:2018-11-22 22:12:59
【问题描述】:

我目前正在通过 Unity 开发 ar 游戏。我正在使用 Vuforia sdk 制作游戏。 游戏内需要 GPS 坐标,但不会更新 GPS 坐标。 奇怪的是,在使用gps代码只在屏幕上显示坐标的单个应用程序中,gps坐标是正确更新的,但是如果我将相同的代码放入游戏中,gps坐标在开始时不再更新应用。 我不认为这是代码的问题。请帮忙。

公共类 GPSCheck : MonoBehaviour{

public static double first_Lat;
public static double first_Long;
public static double current_Lat;
public static double current_Long; 

private static WaitForSeconds second;

private static bool gpsStarted = false;

private static LocationInfo location;

private void Awake()
{
    second = new WaitForSeconds(1.0f);
}

IEnumerator Start()
{

    if (!Input.location.isEnabledByUser)
    {
        Debug.Log("GPS is not enabled");
        yield break;
    }


    Input.location.Start(5f, 10f);
    Debug.Log("Awaiting initialization");


    int maxWait = 20;
    while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
    {
        yield return second;
        maxWait -= 1;
    }

    if (maxWait < 1)
    {
        Debug.Log("Timed out");
        yield break;
    }


    if (Input.location.status == LocationServiceStatus.Failed)
    {
        Debug.Log("Unable to determine device location");
        yield break;

    }
    else
    {

        location = Input.location.lastData;
        first_Lat = location.latitude * 1.0d;
        first_Long = location.longitude * 1.0d;
        gpsStarted = true;


        while (gpsStarted)
        {
            location = Input.location.lastData;
            current_Lat = location.latitude * 1.0d;
            current_Long = location.longitude * 1.0d;
            yield return second;
        }
    }
}

public static void StopGPS()
{
    if (Input.location.isEnabledByUser)
    {
        gpsStarted = false;
        Input.location.Stop();
    }
}

}

【问题讨论】:

标签: c# unity3d gps vuforia


【解决方案1】:

首先:你没有启动你的协程。

第二:避免使用Unity方法作为协程。

试试这个:

public static double first_Lat;
public static double first_Long;
public static double current_Lat;
public static double current_Long; 

private static WaitForSeconds second;

private static bool gpsStarted = false;

private static LocationInfo location;

private void Awake()
{
    second = new WaitForSeconds(1.0f);
}

private void Start()
{
    StartCoroutine(GSP());
}

private IEnumarator GSP()
{
    if (!Input.location.isEnabledByUser)
    {
        Debug.Log("GPS is not enabled");
        yield break;
    }


    Input.location.Start(5f, 10f);
    Debug.Log("Awaiting initialization");


    int maxWait = 20;
    while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
    {
        yield return second;
        maxWait -= 1;
    }

    if (maxWait < 1)
    {
        Debug.Log("Timed out");
        yield break;
    }


    if (Input.location.status == LocationServiceStatus.Failed)
    {
        Debug.Log("Unable to determine device location");
        yield break;

    }
    else
    {

        location = Input.location.lastData;
        first_Lat = location.latitude * 1.0d;
        first_Long = location.longitude * 1.0d;
        gpsStarted = true;


        while (gpsStarted)
        {
            location = Input.location.lastData;
            current_Lat = location.latitude * 1.0d;
            current_Long = location.longitude * 1.0d;
            yield return second;
        }
    }
}

public static void StopGPS()
{
    if (Input.location.isEnabledByUser)
    {
        gpsStarted = false;
        Input.location.Stop();
    }
}

【讨论】:

  • 这段代码也不会更新我游戏中的gps坐标..T.T
  • 在gps测试应用中,运行良好的代码能不能回不去游戏?即使代码完全相同?
  • @Samuel Thauvin Apouchkal 在这种情况下不使用Start 作为IEnumerator 的原因是什么,如果它唯一要做的就是启动协程?如果Start 定义为IEnumerator,Unity 会自动将其作为协程而不是方法调用来启动。
  • 这甚至是他们使用的as example
  • 它是 IEnumerator Start() 而不是 void Start()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多