Monday, 28 March 2016

Unity: Scripting and Geocaching

I started familiarising myself with the Unity interface and tutorials. I particularly found the tutorial Unity Scripting extremely helpful in implementing the Geocaching for the AR.

using UnityEngine;
using System.Collections;
public class Gps : MonoBehaviour {
 // Use this for initialization the geolocation/location based information
 IEnumerator Start () {
   // First, check if user has location service enabled
   if (!Input.location.isEnabledByUser)
   {
     // remind user to enable GPS
     // As far as I know, there is no way to forward user to GPS setting menu in Unity
   }
   // Start service before querying location
   Input.location.Start();
   // Wait until service initializes
   int maxWait = 20;
   while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
     yield return new WaitForSeconds(1);
     maxWait--;
   }
   // Service didn't initialize in 20 seconds
   if (maxWait < 1) {
     print("Timed out");
   }
   // Connection has failed
   if (Input.location.status == LocationServiceStatus.Failed) {
     print("Unable to determine device location");
   }
   // Access granted and location value could be retrieved
   else
     print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " +    Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " +  Input.location.lastData.timestamp);
  // Stop service if there is no need to query location updates continuously
  Input.location.Stop();
 }
}

No comments:

Post a Comment