我目前正在通过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();
}
}
}
答案 0 :(得分:0)
首先:您不需要启动协程。
第二:避免将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();
}
}