欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity3d_API_Gyroscope 陀螺仪的接口

程序员文章站 2022-04-18 19:57:09
...

Gyroscope

class in UnityEngine


示例脚本演示如何使用陀螺仪类查看IOS设备空间中的方向
// iOS 陀螺仪范例

using UnityEngine;
using UnityEngine.iOS;

public class ExampleScript : MonoBehaviour
{
    // 创建六个Plane作为测试参照物
    private GameObject[] quads = new GameObject[6];

    // 有适当的颜色,如红色、绿色、蓝色等
    public Texture[] labels;

    void Start()
    {
        // 使相机的颜色为单色调
        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;

        // 创建六个Plane 作为测试参照物
        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);
    }

    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);
    }

    /********************************************/

    // 陀螺仪是惯用右手的。 统一是左撇子。
    // 对相机做必要的改动。
    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);
    }
}

Unity3d_API_Gyroscope 陀螺仪的接口

示例脚本演示如何使用陀螺仪类查看Android设备空间中的方向

[资源结构]

Unity3d_API_Gyroscope 陀螺仪的接口

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gyroscope : MonoBehaviour {

    private Quaternion gyroscopeQua;
    private Quaternion quatMult = new Quaternion(0, 0, 1, 0);
    private float speedH = 0.2f;    //差值
    
    protected void Start()
    {
        //**陀螺仪
        Input.gyro.enabled = true;
        //因安卓设备的陀螺仪四元数水平值为[0,0,0,0]水平向下,所以将相机初始位置修改与其对齐
        transform.eulerAngles = new Vector3(90, 90, 0);
    }

    protected void Update()
    {
        GyroModifyCamera();
    }

    protected void GyroModifyCamera()
    {
        gyroscopeQua = Input.gyro.attitude * quatMult;      //为球面运算做准备
        Camera.main.transform.localRotation = Quaternion.Slerp(Camera.main.transform.localRotation, gyroscopeQua, speedH);
    }
    protected void OnGUI()
    {
        GUI.skin.label.fontSize = Screen.width / 40;
        
        GUILayout.Label("Orientation: " + Screen.orientation);
        GUILayout.Label("input.gyro.eulerAngles: " + Input.gyro.attitude.eulerAngles);
        GUILayout.Label("now eulerAngles: " + Camera.main.transform.eulerAngles);
        GUILayout.Label("iphone width/font: " + Screen.width + " : " + GUI.skin.label.fontSize);
    }
}
Unity3d_API_Gyroscope 陀螺仪的接口
案例源代码
相关标签: Gyrocsope 陀螺仪