본문 바로가기
NOTE/SaveSource

[Source Save] MobileSensorManager.cs

by DevAthena 2016. 11. 1.
using System.Collections.Generic;
using UnityEngine;
 
public enum Horizontal { Horizon, Right, Left };
public enum Vertical { Stand, Front, Back };
 
class MobileSensorManager : SingleTon<MobileSensorManager>
{
    private float _lat;
    private float _lon;
    private float _sensorX; // 좌우 기울기
    private float _sensorZ; // 앞뒤 기울기
 
    public Horizontal horizon;
    public Vertical vertical;
 
    private MobileSensorManager() { }
 
    public void SensorDetect()
    {
        Input.location.Start();
        CoroutineManager.Instance.Register(SensorUpdate());
    }
 
    private IEnumerator<CoroutinePhase> SensorUpdate()
    {
        while(true)
        { 
            UpdateLocation();
            UpdateTilt();
            yield return null;
        }
    }
 
    private void UpdateLocation()
    {
        _lat = Input.location.lastData.latitude;
        _lon = Input.location.lastData.longitude;
    }
 
    private void UpdateTilt()
    {
        _sensorX = Input.acceleration.x * 10;
        _sensorZ = Input.acceleration.z * 10;
 
        #region 좌우
        if (_sensorX >= 3)
        {
            horizon = Horizontal.Right;
        }
        else if (_sensorX <= -3)
        {
            horizon = Horizontal.Left;
        }
        else
        {
            horizon = Horizontal.Horizon;
        }
        #endregion
        
        #region 앞뒤
        if (_sensorZ >= 10)
        {
            vertical = Vertical.Front;
        }
        else if (_sensorZ <= -10)
        {
            vertical = Vertical.Back;
        }
        else
        {
            vertical = Vertical.Stand;
        }
        #endregion
    }
 
    #region property
    public float GetLatitude
    {
        get { return _lat; }
    }
    public float GetLongitue
    {
        get { return _lon; }
    }
    public Horizontal GetHorizontal
    {
        get { return horizon; }
    }
    public Vertical GetVertical
    {
        get { return vertical; }
    }
    #endregion
}
cs


'NOTE > SaveSource' 카테고리의 다른 글

[Source Save] GyhroCameraMove.cs  (0) 2016.11.09
[SaveSource] WebcamTexture  (0) 2016.11.02
[Source Save] TouchCameraMove.cs  (0) 2016.10.31
[Source Save] InputTouchManager.cs  (0) 2016.10.27
[SaveSource] Drag Script  (0) 2016.10.14