1. 程式人生 > >Unity3D 使用陀螺儀 檢查手機方向 設定固定的旋轉角度

Unity3D 使用陀螺儀 檢查手機方向 設定固定的旋轉角度

namespace UnityEngine
{
    public enum ScreenOrientation
    {
        Unknown = 0,
        Portrait = 1,
        PortraitUpsideDown = 2,
        LandscapeLeft = 3,
        Landscape = 3,
        LandscapeRight = 4,
        AutoRotation = 5
    }
}
/// <summary>
/// private bool m_IsBackCamera;
/// private ScreenOrientation m_CurOrientation; /// </summary> void CheckOrientation() { if (Mathf.Abs(Input.gyro.gravity.z) <= 0.9f) { ScreenOrientation unknown = ScreenOrientation.Unknown; if (Mathf.Abs(Input.gyro.gravity.x) > Mathf.Abs
(Input.gyro.gravity.y)) { if (Input.gyro.gravity.x > 0f) { unknown = ScreenOrientation.LandscapeRight; } else { unknown = ScreenOrientation.LandscapeLeft
; } } else if (Input.gyro.gravity.y > 0f) { unknown = !this.m_IsBackCamera ? ScreenOrientation.Portrait : ScreenOrientation.PortraitUpsideDown; } else { unknown = !this.m_IsBackCamera ? ScreenOrientation.PortraitUpsideDown : ScreenOrientation.Portrait; } this.m_CurOrientation = unknown; } }
void SetupFixedRot()
        {
            switch (Screen.orientation)
            {
                case ScreenOrientation.Unknown:
                    this.m_FixedRot = Quaternion.Euler(90f, 180f, 0f);
                    break;

                case ScreenOrientation.Portrait:
                    this.m_FixedRot = Quaternion.Euler(90f, 180f, 0f);
                    break;

                case ScreenOrientation.PortraitUpsideDown:
                    this.m_FixedRot = Quaternion.Euler(90f, 0f, 0f);
                    break;

                case ScreenOrientation.LandscapeLeft:
                    this.m_FixedRot = Quaternion.Euler(90f, 90f, 0f);
                    break;

                case ScreenOrientation.LandscapeRight:
                    this.m_FixedRot = Quaternion.Euler(90f, 270f, 0f);
                    break;

                default:
                    this.m_FixedRot = Quaternion.Euler(90f, 180f, 0f);
                    break;
            }
        }