当前位置:网站首页>Unity3d_API_Gyroscope 陀螺仪的接口
Unity3d_API_Gyroscope 陀螺仪的接口
2022-08-09 13:02:00 【MOVCat】
Gyroscope
class in UnityEngine
// 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);
}
}
示例脚本演示如何使用陀螺仪类查看Android设备空间中的方向
[资源结构]
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);
}
}
案例源代码
边栏推荐
- 陈强教授《机器学习及R应用》课程 第十八章作业
- Draw a histogram with plot_hist_numeric()
- Microsoft 10/11 命令行打开系统设置页(WUAP,!WIN32)
- Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
- 为什么文字不贴合边
- [FPGA Tutorial Case 48] Image Case 8 - Realization of Converting RGB Image to HSV Image Based on FPGA, Assisted Verification by MATLAB
- 七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
- 面试攻略系列(二)-- 秒杀系统
- 【FPGA教程案例48】图像案例8——基于FPGA的RGB图像转化为HSV图像的实现,通过MATLAB进行辅助验证
- 周末看点回顾|我国IPv6网络全面建成;2022昇腾AI开发者创享日·南京站成功举办…
猜你喜欢
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
GIN a preliminary study, the environment is installed
[极客大挑战 2019]Upload
IDEA Gradle 常遇问题(二)(持续更新)
GIN中GET POST PUT DELETE请求
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
剑指offer,剪绳子2
某高校的R语言数据分析期末作业
Explanation of RTSP protocol
乐东消防救援大队应邀为干部开展消防安全培训
随机推荐
某高校的R语言数据分析期末作业
Professor Chen Qiang the machine learning and R application course chapter 18 assignments
Rmarkdown教程
Sandbox中的进程/线程相关-2
【FPGA教程案例48】图像案例8——基于FPGA的RGB图像转化为HSV图像的实现,通过MATLAB进行辅助验证
FPGA-近日工作总结
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
Unicom network management protocol block diagram
WSA toolkit installed app store tip doesn't work how to solve?
5G China unicom 一般性异常处理
IDEA Gradle 常遇问题(一)
ArcEngine(十)创建矢量图层
Draw a histogram with plot_hist_numeric()
#WeArePlay | 与更多开发者一起,探索新世界
R 语言 2010.1至2021.12艾滋病每月发病人数 时间序列分析
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
NFS pays special attention to the problem of permissions
面试题精选:神奇的斐波那契数列
剑指offer,剪绳子2
力扣解法汇总1413-逐步求和得到正数的最小值