コード例 #1
0
 private void Start()
 {
     // Delegate listener. BackToMainMenu() on exitBtn.
     // exitBtn.onClick.AddListener(delegate { exitBtn.GetComponent<UIExitBtnScript>().BackToMainMenu(); });
     menuBtn.onClick.AddListener(delegate { FromBackToMainMenuToCloseMenu(); });
     cameraMovementScript = FindObjectOfType <CameraMovementGPS>();
 }
コード例 #2
0
ファイル: GPSScript.cs プロジェクト: KahVille/flagworld
    // Start is called before the first frame update
    IEnumerator Start()
    {
        activeColor    = triviaBtn.GetComponent <Image>().color;
        isInitializing = true;
        // debugText.text = "Start";
        infoAnim     = infoPanelObj.GetComponent <Animator>();
        gpsBtnScript = FindObjectOfType <GPSButtonsScript>();
        canOpenMenu  = false;

        // Set map to correct position. First the map is moved to origo (0,0,0), then moved right and up so
        // the bottom left corner of map is in origo, and the map extends right and up.
        Vector3       wantedPos = Vector3.zero;
        RectTransform rt        = mapImage.GetComponent <RectTransform>();

        wantedPos.x += rt.rect.width / 2;
        wantedPos.y += rt.rect.height / 2;
        rt.position  = wantedPos;

        // Set camera start position to center of map
        Vector3 cameraStartPos;

        cameraStartPos   = mapImage.transform.position;
        cameraStartPos.z = -10f;
        Camera.main.transform.position = cameraStartPos;

        cameraMovementScript = FindObjectOfType <CameraMovementGPS>();

        // Set correct desiredPosition in camera movement script
        cameraMovementScript.SetStartDesiredPosition(cameraStartPos);

        // Get map image corners's world positions to mapCorners array
        mapImage.GetComponent <RectTransform>().GetWorldCorners(mapCorners);

        // Android permissions
        #if UNITY_ANDROID || UNITY_IOS
        if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
        {
            Permission.RequestUserPermission(Permission.FineLocation);
        }
        #endif

        #if UNITY_ANDROID && !UNITY_EDITOR || UNITY_IOS
        yield return(new WaitForSeconds(0.1f));

        // Need to tell the user about everything!

        // First, check if user has location service enabled
        if (!Input.location.isEnabledByUser)
        {
            // debugText.text = "Not Enabled";
            optionsGPSImg.SetImg(false);
            yield break;
        }
        // Start service before querying location
        Input.location.Start();
        autologging = false;
        //autologImg.color = Color.red;

        // 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)
        {
            // debugText.text = "Timed out";
            optionsGPSImg.SetImg(false);
            yield break;
        }

        // Connection has failed
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            // debugText.text = "Unable to determine device location";
            optionsGPSImg.SetImg(false);
            yield break;
        }
        else
        {
            // Access granted and location value could be retrieved
            //// debugText.text = "Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp;
            // debugText.text = "Working, yay!";
            tracking     = true;
            lastDistance = double.MaxValue;
            optionsGPSImg.SetImg(true);
            updateLocationCO = StartCoroutine(UpdateLocation());
        }

        if (!File.Exists(Application.persistentDataPath + "/GPSDatas.txt"))
        {
            File.AppendAllText(Application.persistentDataPath + "/GPSDatas.txt", String.Empty);
        }
        #elif UNITY_EDITOR
        yield return(null);

        // debugText.text = "Working, yay!";
        tracking = true;
        optionsGPSImg.SetImg(false);
        lastDistance     = double.MaxValue;
        updateLocationCO = StartCoroutine(UpdateLocation());
        #endif

        // Add listeners to location buttons
        for (int i = 0; i < locations.Length; i++)
        {
            int tmp = i;
            locBtns.Add(locations[i].image.transform.GetComponent <Button>());
            locBtns[i].onClick.AddListener(delegate { PushLocBtn(tmp); });
        }
        isInitializing = false;
        yield return(new WaitForSeconds(0.01f));
    }