访问陀螺仪的接口。
可以使用该类访问陀螺仪。下面的示例脚本演示了如何使用 Gyroscope 类查看设备在空间中的方向。
用于数据填充的基础传感器:
Android:重力、线性加速度、旋转矢量。更多信息。
iOS:陀螺仪、设备运动。更多信息。
// Create a cube with camera vector names on the faces. // Allow the device to show named faces as it is oriented.
using UnityEngine;
public class ExampleScript : MonoBehaviour { // Faces for 6 sides of the cube private GameObject[] quads = new GameObject[6];
// Textures for each quad, should be +X, +Y etc // with appropriate colors, red, green, blue, etc public Texture[] labels;
void Start() { // make camera solid colour and based at the origin GetComponent<Camera>().backgroundColor = new Color(49.0f / 255.0f, 77.0f / 255.0f, 121.0f / 255.0f); GetComponent<Camera>().transform.position = new Vector3(0, 0, 0); GetComponent<Camera>().clearFlags = CameraClearFlags.SolidColor;
// create the six quads forming the sides of a cube GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quads[0] = createQuad(quad, new Vector3(1, 0, 0), new Vector3(0, 90, 0), "plus x", new Color(0.90f, 0.10f, 0.10f, 1), labels[0]); quads[1] = createQuad(quad, new Vector3(0, 1, 0), new Vector3(-90, 0, 0), "plus y", new Color(0.10f, 0.90f, 0.10f, 1), labels[1]); quads[2] = createQuad(quad, new Vector3(0, 0, 1), new Vector3(0, 0, 0), "plus z", new Color(0.10f, 0.10f, 0.90f, 1), labels[2]); quads[3] = createQuad(quad, new Vector3(-1, 0, 0), new Vector3(0, -90, 0), "neg x", new Color(0.90f, 0.50f, 0.50f, 1), labels[3]); quads[4] = createQuad(quad, new Vector3(0, -1, 0), new Vector3(90, 0, 0), "neg y", new Color(0.50f, 0.90f, 0.50f, 1), labels[4]); quads[5] = createQuad(quad, new Vector3(0, 0, -1), new Vector3(0, 180, 0), "neg z", new Color(0.50f, 0.50f, 0.90f, 1), labels[5]);
GameObject.Destroy(quad); }
// make a quad for one side of the cube GameObject createQuad(GameObject quad, Vector3 pos, Vector3 rot, string name, Color col, Texture t) { Quaternion quat = Quaternion.Euler(rot); GameObject GO = Instantiate(quad, pos, quat); GO.name = name; GO.GetComponent<Renderer>().material.color = col; GO.GetComponent<Renderer>().material.mainTexture = t; GO.transform.localScale += new Vector3(0.25f, 0.25f, 0.25f); return GO; }
protected void Update() { GyroModifyCamera(); }
protected void OnGUI() { GUI.skin.label.fontSize = Screen.width / 40;
GUILayout.Label("Orientation: " + Screen.orientation); GUILayout.Label("input.gyro.attitude: " + Input.gyro.attitude); GUILayout.Label("iphone width/font: " + Screen.width + " : " + GUI.skin.label.fontSize); }
/********************************************/
// The Gyroscope is right-handed. Unity is left handed. // Make the necessary change to the camera. void GyroModifyCamera() { transform.rotation = GyroToUnity(Input.gyro.attitude); }
private static Quaternion GyroToUnity(Quaternion q) { return new Quaternion(q.x, q.y, -q.z, -q.w); } }
\
显示 +Z、+Y 和 -X 的 iOS 屏幕截图。
attitude | 返回设备的姿态(即在空间中的方向)。 |
enabled | 设置或检索该陀螺仪的启用状态。 |
gravity | 返回以设备参考系表示的重力加速度矢量。 |
rotationRate | 返回设备陀螺仪测量的旋转速率。 |
rotationRateUnbiased | 返回设备陀螺仪测量的无偏差旋转速率。 |
updateInterval | 设置或检索陀螺仪间隔时间(以秒为单位)。 |
userAcceleration | 返回用户给予设备的加速度。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.