[Unity AR] GPS에 따른 위치 이동

윤정민·2022년 10월 25일
0

1. 위도&경도 사용하기

1.1. 위도 경도 정보 가져오는 Script 생성

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.UI;

public class GPS : MonoBehaviour
{

    //텍스트 ui변수
    public static GPS instance;
    public Text latitude_text;
    public Text longitude_text;
    public float maxWaitTime = 10.0f;
    public float resendTime = 1.0f;

    //위도 경도 변경
    public float latitude = 0;
    public float longitude = 0;
    float waitTime = 0;

    public bool receiveGPS = false;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
    }
    void Start()
    {
        StartCoroutine(GPS_On());
    }

    //GPS처리 함수
    public IEnumerator GPS_On()
    {
        //만일,GPS사용 허가를 받지 못했다면, 권한 허가 팝업을 띄움
        if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
        {
            Permission.RequestUserPermission(Permission.FineLocation);
            while (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
            {
                yield return null;
            }
        }

        //만일 GPS 장치가 켜져 있지 않으면 위치 정보를 수신할 수 없다고 표시
        if (!Input.location.isEnabledByUser)
        {
            latitude_text.text = "GPS Off";
            longitude_text.text = "GPS Off";
            yield break;
        }

        //위치 데이터를 요청 -> 수신 대기
        Input.location.Start();

        //GPS 수신 상태가 초기 상태에서 일정 시간 동안 대기함
        while (Input.location.status == LocationServiceStatus.Initializing && waitTime < maxWaitTime)
        {
            yield return new WaitForSeconds(1.0f);
            waitTime++;
        }

        //수신 실패 시 수신이 실패됐다는 것을 출력
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            latitude_text.text = "위치 정보 수신 실패";
            longitude_text.text = "위치 정보 수신 실패";
        }

        //응답 대기 시간을 넘어가도록 수신이 없었다면 시간 초과됐음을 출력
        if (waitTime >= maxWaitTime)
        {
            latitude_text.text = "응답 대기 시간 초과";
            longitude_text.text = "응답 대기 시간 초과";
        }

        //수신된 GPS 데이터를 화면에 출력/
       
        LocationInfo li = Input.location.lastData;
         /*latitude = li.latitude;
        longitude = li.longitude;
        latitude_text.text = "위도 : " + latitude.ToString();
        longitude_text.text = "경도 : " + longitude.ToString();
        */
        //위치 정보 수신 시작 체크
        receiveGPS = true;

        //위치 데이터 수신 시작 이후 resendTime 경과마다 위치 정보를 갱신하고 출력
        while (receiveGPS)
        {
            li = Input.location.lastData;
            latitude = li.latitude;
            longitude = li.longitude;

            latitude_text.text = "위도 : " + latitude.ToString();
            longitude_text.text = "경도 : " + longitude.ToString();

            yield return new WaitForSeconds(resendTime);
        }
    }
}

1.2. GPS Manager Object 생성

  • [하이라키창] -> [Empty Object생성] -> [GPS Component 추가]
  • Empty Object의 이름 : GPSManager

1.3. 다른 스크립트에서 위도 정보 사용 예시

private GameObject GPSManager;
private float lat;
void start()
{
	GPSManager = GameObject.Find("GPSManager");
}
void GetLatitude()
{
	lat = GPSManager.GetComponent<GPS>().latitude;
}

2. 가져온 위도,경도 unity coordinate로 변경

2.1. GPS Converter 들고오기

https://github.com/MichaelTaylor3D/UnityGPSConverter/blob/master

2.2. 원하는 스케일이 있다면 위치 설정해주기

  • 기본은 latitude, longitude 모두 (0,0)이 기준
  • 원하는 스케일이 있다면 조정해서 사용

2.3. GPS.cs에서 converter 사용

  • converter : GPSEncoder.cs
...
public class GPS : MonoBehaviour
{

    ...

    //위도 경도 변경
    public float latitude = 0;
    public float longitude = 0;

    public Vector3 unityCoor; // unityCoor를 담을 변수
    float waitTime = 0;

    public bool receiveGPS = false;

    ...

    //GPS처리 함수
    public IEnumerator GPS_On()
    {
        ...

        //위치 데이터 수신 시작 이후 resendTime 경과마다 위치 정보를 갱신하고 출력
        while (receiveGPS)
        {
            li = Input.location.lastData;
            latitude = li.latitude;
            longitude = li.longitude;

            latitude_text.text = "위도 : " + latitude.ToString();
            longitude_text.text = "경도 : " + longitude.ToString();
            unityCoor = GPSEncoder.GPSToUCS(latitude, longitude); //GPSEncoder의 GPSToUCS 사용
            yield return new WaitForSeconds(resendTime);
        }
    }
}

2.4. 다른 스크립트에서 unityCoor 사용 예시

private GameObject GPSManager;
private Vector3 locationVec;
private float locationX;
void start()
{
	GPSManager = GameObject.Find("GPSManager");
}
void GetLatitude()
{
	locationVec = GPSManager.GetComponent<GPS>().unityCoor; //vector로 한방에 가져오기
    locationX = GPSManager.GetComponent<GPS>().unityCoor.x;
}
profile
그냥 하자

0개의 댓글