using System;
using UnityEngine;
//相机陀螺仪
public class GyroCam : MonoBehaviour
{
//相机父物体
private GameObject camParent;
//是否Debug
public bool debug;
//陀螺仪
private Gyroscope gyro;
//是否调用陀螺仪
private bool gyroBool;
//是否有GPS
private bool hasGPS;
//朝向设置
public float northOffset;
//选择修正
private Quaternion rotFix;
//速度
public float speed;
//触摸旋转
// public FreeCam TouchRotate;
public void Awake()
{
//支持位置服务
this.hasGPS = SystemInfo.supportsLocationService;
Debug.Log(hasGPS);
//父物体的位置
Transform parent = base.transform.parent;
Debug.Log(parent);
GameObject obj2 = new GameObject("GyroCamParent")
{
transform = { position = base.transform.position }
};
Debug.Log(obj2);
base.transform.parent = obj2.transform;
GameObject obj3 = new GameObject("GyroCamGrandParent")
{
transform = { position = base.transform.position }
};
obj2.transform.parent = obj3.transform;
obj3.transform.parent = parent;
//支持陀螺仪
this.gyroBool = SystemInfo.supportsGyroscope;
if (this.gyroBool)
{
this.gyro = Input.gyro;
this.gyro.enabled = true;
if (Screen.orientation == ScreenOrientation.LandscapeLeft)
{
obj2.transform.eulerAngles = new Vector3(90f, 180f, 0f);
}
else if (Screen.orientation == ScreenOrientation.Portrait)
{
obj2.transform.eulerAngles = new Vector3(90f, 180f, 0f);
}
else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
{
obj2.transform.eulerAngles = new Vector3(90f, 180f, 0f);
}
else if (Screen.orientation == ScreenOrientation.LandscapeRight)
{
obj2.transform.eulerAngles = new Vector3(90f, 180f, 0f);
}
else
{
obj2.transform.eulerAngles = new Vector3(90f, 180f, 0f);
}
if (Screen.orientation == ScreenOrientation.LandscapeLeft)
{
this.rotFix = new Quaternion(0f, 0f, 1f, 0f);
}
else if (Screen.orientation == ScreenOrientation.Portrait)
{
this.rotFix = new Quaternion(0f, 0f, 1f, 0f);
}
else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
{
this.rotFix = new Quaternion(0f, 0f, 1f, 0f);
}
else if (Screen.orientation == ScreenOrientation.LandscapeRight)
{
this.rotFix = new Quaternion(0f, 0f, 1f, 0f);
}
else
{
this.rotFix = new Quaternion(0f, 0f, 1f, 0f);
}
}
else
{
//触摸旋转(没有陀螺仪,使用触摸)
// this.TouchRotate.enabled = true;
Debug.Log("No Gyroscope, using touch");
}
base.transform.parent.eulerAngles = new Vector3(base.transform.parent.eulerAngles.x, this.northOffset, base.transform.parent.eulerAngles.z);
}
private void OnDisable()
{
//如果还有GPS功能,定位状态没有关闭
if (this.hasGPS && (Input.location.status != LocationServiceStatus.Stopped))
{
//位置服务关闭
Input.location.Stop();
}
}
private void OnEnable()
{
//如果还有GPS功能,定位状态没有初始化和没有运行
if ((this.hasGPS && (Input.location.status != LocationServiceStatus.Initializing)) && (Input.location.status != LocationServiceStatus.Running))
{
//位置服务开启
Input.location.Start();
}
}
public void Update()
{
if (this.gyroBool)
{
Quaternion quaternion = new Quaternion(this.gyro.attitude.x, this.gyro.attitude.y, this.gyro.attitude.z, this.gyro.attitude.w);
base.transform.localRotation = Quaternion.Slerp(base.transform.localRotation, quaternion * this.rotFix, Time.deltaTime * this.speed);
}
}
}