版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Exclaiming/article/details/82877558
1.下载导入资源:https://pan.baidu.com/s/19JMpg6-_zVFXD6K1UKh_bQ
提取码:7d76
2.拖动Hologram下的Cursor到Hierarchy;
3.新建空物体,并命名为OrigamiCollection;
4.修改相机属性;
5.新建脚本WorldCursor,并添加到Cursor上;
using UnityEngine;
public class WorldCursor : MonoBehaviour
{
private MeshRenderer meshRenderer;
// Use this for initialization
void Start()
{
// Grab the mesh renderer that's on the same object as this script.
meshRenderer = this.gameObject.GetComponentInChildren<MeshRenderer>();
}
// Update is called once per frame
void Update()
{
// Do a raycast into the world based on the user's
// head position and orientation.
var headPosition = Camera.main.transform.position;
var gazeDirection = Camera.main.transform.forward;
RaycastHit hitInfo;
if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
{
// If the raycast hit a hologram...
// Display the cursor mesh.
meshRenderer.enabled = true;
// Move the cursor to the point where the raycast hit.
this.transform.position = hitInfo.point;
// Rotate the cursor to hug the surface of the hologram.
this.transform.rotation =
Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
else
{
// If the raycast did not hit a hologram, hide the cursor mesh.
meshRenderer.enabled = false;
}
}
}
6.新建脚本GazeGestureManager,并添加到OrigamiCollection上;
using UnityEngine;
using UnityEngine.XR.WSA.Input;
public class GazeGestureManager : MonoBehaviour
{
public static GazeGestureManager Instance { get; private set; }
// Represents the hologram that is currently being gazed at.
public GameObject FocusedObject { get; private set; }
GestureRecognizer recognizer;
// Use this for initialization
void Start()
{
Instance = this;
// Set up a GestureRecognizer to detect Select gestures.
//创建GestureRecognizer实例
recognizer = new GestureRecognizer();
//注册指定的手势类型,本例指定单击及双击手势
recognizer.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.DoubleTap);
//订阅手势事件
recognizer.TappedEvent += GestureRecognizer_TappedEvent;
//开始手势识别
recognizer.StartCapturingGestures();
}
private void OnTap()
{
if (FocusedObject != null)
{
FocusedObject.SendMessage("OnTap");
}
}
private void OnDoubleTap()
{
if (FocusedObject != null)
{
FocusedObject.SendMessage("OnDoubleTap");
}
}
private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
{
if (tapCount == 1)
{
OnTap();
}
else
{
OnDoubleTap();
}
}
void LateUpdate()
{
// Figure out which hologram is focused this frame.
GameObject oldFocusObject = FocusedObject;
// Do a raycast into the world based on the user's
// head position and orientation.
var headPosition = Camera.main.transform.position;
var gazeDirection = Camera.main.transform.forward;
RaycastHit hitInfo;
if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
{
// If the raycast hit a hologram, use that as the focused object.
FocusedObject = hitInfo.collider.gameObject;
}
else
{
// If the raycast did not hit a hologram, clear the focused object.
FocusedObject = null;
}
// If the focused object changed this frame,
// start detecting fresh gestures again.
if (FocusedObject != oldFocusObject)
{
recognizer.CancelGestures();
recognizer.StartCapturingGestures();
}
}
void OnDestroy()
{
recognizer.StopCapturingGestures();
recognizer.TappedEvent -= GestureRecognizer_TappedEvent;
}
}
7.新建脚本CubeScript,并添加到Cube上,实现单击为蓝色,双击为红色;
using UnityEngine;
using System.Collections;
public class CubeScript : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTap()
{
gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
}
private void OnDoubleTap()
{
gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
}
}
8.发布Hololens进行测试。
9.完整工程下载:https://download.csdn.net/download/exclaiming/10692685