当前位置:网站首页>Unity - use of API related to Pico development input system ---c
Unity - use of API related to Pico development input system ---c
2022-06-28 08:09:00 【Star_ MengMeng】
Unity It's using 2020.2.25
Pico SDK The version used is v2.0.5
Go straight to the code ,C# piece :
using System.Collections;
using System.Collections.Generic;
using Unity.XR.PXR;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;
public class PicoInputTest : MonoBehaviour
{
// Handle controller
private InputDevice leftController;
private InputDevice rightController;
private InputDevice headController;
private bool isTriggerDown;
private Vector2 axis;
// Handle ray
XRRayInteractor leftInteractor;
XRRayInteractor rightInteractor;
// Handle ray
private XRInteractorLineVisual leftRayLine;
private XRInteractorLineVisual rightRayLine;
// ray
RaycastHit leftRayInfo;
public GameObject cube;
public GameObject sphere;
void Start()
{
InitDevice();
InitData();
SetRayLineVisual();
}
// Initialization equipment
void InitDevice()
{
// Examples of equipment , Other necessary items can also be tested
if (PXR_Input.IsControllerConnected(PXR_Input.Controller.LeftController))
{
leftController = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
rightController = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
headController = InputDevices.GetDeviceAtXRNode(XRNode.Head);
}
// Note the path root node !
leftInteractor = transform.Find("Camera Offset/LeftHand Controller").GetComponent<XRRayInteractor>();
rightInteractor = transform.Find("Camera Offset/RightHand Controller").GetComponent<XRRayInteractor>();
// Left and right hand rays
leftRayLine = transform.Find("Camera Offset/LeftHand Controller").GetComponent<XRInteractorLineVisual>();
rightRayLine = transform.Find("Camera Offset/LeftHand Controller").GetComponent<XRInteractorLineVisual>();
}
// Data initialization
void InitData()
{
isTriggerDown = false;
axis = Vector2.zero;
}
// Get a list of all devices
private List<InputDevice> GetAllDevs()
{
List<InputDevice> deviceList = new List<InputDevice>();
InputDevices.GetDevices(deviceList);
return deviceList;
}
// Update is called once per frame
void Update()
{
// Equipment input system detection
DevicesInputUpdate();
// Radiographic testing
RayCheck();
}
// Set attributes such as ray color
public void SetRayLineVisual()
{
Gradient g;
g = new Gradient();
GradientColorKey[] gck;
gck= new GradientColorKey[2];
GradientAlphaKey[] gak;
gak = new GradientAlphaKey[2];
gck[0].color=Color.blue;
gck[0].time = 0.0f;
gck[1].color=Color.red;
gck[1].time = 1.0f;
gak[0].alpha = 1.0f;
gak[0].time = 0.0f;
gak[1].alpha = 1.0f;
gak[1].time = 1.0f;
g.SetKeys(gck,gak);
leftRayLine.lineWidth = 0.2f;
leftRayLine.invalidColorGradient = g;// There is no interactive ray color in the default state
//leftRayLine.validColorGradient = g2;// Define another color g2、 That is, the ray color when interaction is generated ;
}
// Device button detection
private void DevicesInputUpdate()
{
// Trigger key detection , The same principle as other buttons , Look directly at API Just define it
if (leftController.TryGetFeatureValue(CommonUsages.triggerButton,out isTriggerDown)&&isTriggerDown)
{
//TODO:
cube.SetActive(false);
}
// Rocker detection , It can also be directly passed through 0——1 To judge
if (leftController.TryGetFeatureValue(CommonUsages.primary2DAxis,out axis)&&!axis.Equals(Vector2.zero))
{
//TODO:
float angle = VectorAngle(new Vector2(1, 0), axis);
// On
if (angle > 45 && angle < 135)
{
transform.Translate(Camera.main.transform.forward*5*Time.deltaTime);
}
// Next
else if (angle < -45 && angle > -135)
{
transform.Translate(Camera.main.transform.forward*-5*Time.deltaTime);
}
// Left
else if ((angle < 180 && angle > 135) || (angle < -135 && angle > -180))
{
transform.Rotate(Vector3.up*-30*Time.deltaTime);
}
// Right
else if ((angle > 0 && angle < 45) || (angle > -45 && angle < 0))
{
transform.Rotate(Vector3.up*30*Time.deltaTime);
}
}
}
//update Handle radiographic testing
// Right click XR-UICanvas establish Canvas Only here Canvas Under the UI Components can interact with handle rays normally
private void RayCheck()
{
if (leftInteractor.GetCurrentRaycastHit(out leftRayInfo))
{
if (leftRayInfo.collider != null && leftRayInfo.collider.CompareTag("Player"))
{
//TODO:
sphere.transform.localScale = Vector3.one * 5;
}
else
{
sphere.transform.localScale = Vector3.one * 1;
}
if (leftRayInfo.collider.gameObject.name == "look1Obj")
{
if (leftController.TryGetFeatureValue(CommonUsages.triggerButton, out isTriggerDown) && isTriggerDown)
{
sphere.GetComponent<MeshRenderer>().material.color = Color.red;
//Ui3DTest._onVrCLick.Invoke("look1Obj");
}
}
else
{
sphere.GetComponent<MeshRenderer>().material.color = Color.blue;
}
}
}
float VectorAngle(Vector2 from,Vector2 to)
{
float angle;
Vector3 cross = Vector3.Cross(from, to);
angle = Vector2.Angle(from, to);
return cross.z > 0 ? angle : -angle;
}
}Enclosed Pico API Official documents :
边栏推荐
- Airflow2 configuration windows azure SSO details based on oauth2 protocol
- Unity 获取当前物体正前方,一定角度、距离的坐标点
- Prometheus deployment alarm docking QQ mailbox
- B_QuRT_User_Guide(26)
- Es data export CSV file
- NLP sequence can completely simulate human brain intelligence
- Airflow2.1.1 ultra detailed installation document
- Activity implicit jump
- Airflow2.1.1 summary of the pits stepped on in actual combat!!
- 【学习笔记】搜索
猜你喜欢

匿名页的反向映射

App automated testing appium tutorial 2 - ADB command

Ambari (V) ---ambari integrated Azkaban (valid for personal test)

Airflow2.1.1 ultra detailed installation document

Prometheus deployment alarm docking QQ mailbox

SQL Master slave Replication Build

三角变换公式

2022巴黎时装周儿童单元6.19武汉站圆满落幕

js取整的小技巧

AI首席架构师8-AICA-高翔 《深入理解和实践飞桨2.0》
随机推荐
Eslint syntax monitoring off
Activity implicit jump
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
MySQL two table connection principle (understand join buf)
Localization SoC development plan
Doris学习笔记之介绍、编译安装与部署
js取整的小技巧
Cloud native: cloud computing technology is upgraded again to open an era of comprehensive cloud development
sql分析(查询截取分析做sql优化)
B_ QuRT_ User_ Guide(29)
Upgrade HDP spark to spark 2.4.8 without upgrading ambari
NLP sequence can completely simulate human brain intelligence
同花顺注册开户靠谱吗?安全吗?
LeetCode之三步问题
Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
Image translation /transformer:ittr: unpaired image to image translation with transformers
[shangpinhui] project notes
Kubernetes theoretical basis
Conversion between HJ integer and IP address
图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换