HoloLens 1代 简单开发

Hololens手势简单开发:

做一个简单的手势开发:用手势单击物体改变物体颜色:

其中MultiTarget是你想选中的目标物体,Sphere是一个光标。

需要的脚本(脚本文档后有附):

光标脚本(Cursor.cs):光标可视化,把这个脚本挂到一个你想让他成为光标的物体上。

视线脚本(GazeGesture.cs):

执行脚本:这个脚本(CubeScript.cs)需要放在你在手势点击之后想哪个物体产生效果,就放在哪个物体上。

主要步骤

  1. Manager:新建空物体Manager,将GazeGesture脚本挂到此物体上。
  2. 光标:新建任意物体(光标物体尺寸大小应该小),将光标脚本挂到这个物体上。
  3. 执行物体:用高通vuforia新建物体,将执行脚本挂到这个物体上。
  4. 之后将工程上传到Hloloens,进行操作。

详细步骤:

  1. 新建工程color1,点击File选择Bulid Setting ;点击Player Setting,选择XR Setting,选择VR和AR。

2.在Untitled面板右键点击选择Vuforia中的AR Camera,点击input,删除面板中的Main Camera。

3.点击右键,点击Create Empty创建空物体。重命名Manager。

  1. 创建光标cursor,创建3D Object-Sphere。

6.导入Vurforia打包物体:Assets-Import Package-Custom Package

7. 分别将脚本挂到相应物体上。

Hololens发布

1.Publishing Settings

勾选internetClient、WebCam、Microphone

2.添加场景、选择开发平台

3选择build按钮,生成发布APP文件夹

4VS打开程序

5.修改三个地方

<TargetDeviceFamily Name="Windows.Holographic" MinVersion="10.0.10586.0" MaxVersionTested="10.0.17134.0" />

 

 

脚本详细内容

GazeGesture.cs:

using UnityEngine;
//using UnityEngine.VR.WSA.Input;
using UnityEngine.XR.WSA.Input;

public class GazeGesture : MonoBehaviour
{
    public static GazeGesture Instance { get; private set; }

    // 保存当前凝视的物体;
    public GameObject FocusedObject { get; private set; }

    GestureRecognizer recognizer;

    // Use this for initialization
    void Start()
    {
        Instance = this;

        // 用来检测手势;
        recognizer = new GestureRecognizer();
        recognizer.TappedEvent += (source, tapCount, ray) =>
        {
            // 向凝视的物体和父物体发送OnSelect消息;
            if (FocusedObject != null)
            {
                FocusedObject.SendMessageUpwards("OnSelect");
            }
        };
        recognizer.StartCapturingGestures();
    }

    // Update is called once per frame
    void Update()
    {
        // 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();
        }
    }
}

Cursor.cs:

using UnityEngine;
public class Cursor : MonoBehaviour
{
    private MeshRenderer meshRenderer;
    // Use this for initialization 初始化时候调用
    void Start()
    {
        // Grab the mesh renderer that's on the same object as this script.
        // 获取 meshRenderer
        meshRenderer = this.gameObject.GetComponentInChildren<MeshRenderer>();
    }
    // Update is called once per frame 每一帧都会自动更新
    void Update()
    {
        // 根据用户头的位置和朝向发射射线;
        // head position and orientation.
        //Var在无法确定变量的类型时使用。且是局部变量
        //念上来说,Gaze是通过用户头部两眼之间发出一条向前方的射线来实现的,射线可以识别它所碰撞的物体.
        //unity,使用Main Camera来表示用户头部的位置和朝向。准确的说,是指UnityEngine.Camera.main.transform.forward 和 UnityEngine.Camera.main.transform.position
        //用Physics.RayCast 发出射线后可以得到RaycastHit结果,该结果包含了碰撞点的3D位置参数和碰撞对象;
        var headPosition = Camera.main.transform.position;//头部位置是相机的位置;
        var gazeDirection = Camera.main.transform.forward;//头部朝向;
        RaycastHit hitInfo;//用于存储射线碰撞到的第一个对象信息。需要提前创建这个对象。
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))//Physics.Raycast此静态函数用于在场景中发射一条可以喝碰撞器碰撞的射线
        {
            // 如果射线碰到了hologram,显示光标;
            meshRenderer.enabled = true;// Move the cursor to the point where the raycast hit.把光标移到射线碰撞的地方;
            this.transform.position = hitInfo.point;//将光标移到射线碰撞位置;将光标与hologram的表面平行;
            this.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
        }
        else
        {
            //如果没有碰到物体隐藏光标;
            meshRenderer.enabled = false;
        }
    }
}

执行脚本(CubeScript):

using System.Collections;

using UnityEngine;


public class CubeScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
//	private void OnTap()
//	{
//
//		gameObject.GetComponents <MeshRenderer> ().material.color = Color.blue;
//	}
//	private void OnDoubleTap()
//	{
//
//		gameObject.GetComponents <MeshRenderer> ().material.color = Color.green;
//	}
	public void OnSelect()

	{
		//随机变换物体颜色

		gameObject.GetComponent<MeshRenderer>().material.color = new Color(Random.Range(0, 255) / 255f, Random.Range(0, 255) / 255f, Random.Range(0, 255) / 255f);

	}
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值