Unity使用陀螺仪检测屏幕方向
Posted DaLiangChen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity使用陀螺仪检测屏幕方向相关的知识,希望对你有一定的参考价值。
using UnityEngine;
/// <summary>检测设备方向</summary>
public class CheckDeviceOrientation : MonoBehaviour
private void Start()
initGyro();
/// <summary>初始化陀螺仪</summary>
private void initGyro()
Input.gyro.enabled = true;
Input.gyro.updateInterval = 0.1f;
private bool isBackCamera = true;
private void Update()
checkOrientation();
/// </summary>
void checkOrientation()
if (Mathf.Abs(Input.gyro.gravity.z) <= 0.9f)
DeviceOrientation curOrientation = DeviceOrientation.Unknown;
if (Mathf.Abs(Input.gyro.gravity.x) > Mathf.Abs(Input.gyro.gravity.y))
if (Input.gyro.gravity.x > 0f)
curOrientation = DeviceOrientation.LandscapeRight;
else
curOrientation = DeviceOrientation.LandscapeLeft;
else if (Input.gyro.gravity.y > 0f)
curOrientation = !this.isBackCamera ? DeviceOrientation.Portrait : DeviceOrientation.PortraitUpsideDown;
else
curOrientation = !this.isBackCamera ? DeviceOrientation.PortraitUpsideDown : DeviceOrientation.Portrait;
以上是关于Unity使用陀螺仪检测屏幕方向的主要内容,如果未能解决你的问题,请参考以下文章