HoloToolKit无法通过父物体集中处理子物体的点击等事件,本文介绍了如何封装Hololens点击事件在父物体上集中处理,大概步骤为:
- 拖入HoloLensCamera预设体
- 拖入InputManager预设体
- 拖入Cursor预设体
- 创建画布
- 创建两个Button
1-3. 拖入三个预设体
4. 创建画布
创建完画布后,把 Render Mode 设为 World Space
添加CanvasCtrl.cs脚本
using UnityEngine;
public class CanvasCtrl : MonoBehaviour
{
private TapEvent m_Button1;
private TapEvent m_Button2;
private void Awake()
{
m_Button1 = transform.FindChild("Button1").GetComponent<TapEvent>();
m_Button2 = transform.FindChild("Button2").GetComponent<TapEvent>();
m_Button1.hololensOnClick.AddListener(OnButton1Click);
m_Button2.hololensOnClick.AddListener(OnButton2Click);
}
private void OnButton1Click()
{
Debug.Log("OnButton1Click");
}
private void OnButton2Click()
{
Debug.Log("OnButton2Click");
}
}
5. 创建两个Button
两个按钮都添加TapEvent.cs脚本
using UnityEngine;
using UnityEngine.Events;
using HoloToolkit.Unity.InputModule;
public class TapEvent : MonoBehaviour,IInputClickHandler
{
[SerializeField]
private UnityEvent HololensOnClick = new UnityEvent();
public UnityEvent hololensOnClick
{
get { return HololensOnClick; }
set { hololensOnClick = value; }
}
public void OnInputClicked(InputEventData eventData)
{
HololensOnClick.Invoke();
}
}
测试结果
如喜欢该简博,请加至收藏夹
作者 @Zajanmy
2017 年 01月 19日