使用HoloToolKit實現導航手勢旋轉3D模型

Hololens官方教程 Holograms 211中的示例是,通過GestureManager.cs和HandsManager.cs來實現手勢事件。但在最近的HTK中,這兩個腳本被刪除了。現在HoloToolKit使用的手勢事件機制是通過接口來實現。
HoloToolKit提供的接口有:
  • IHoldHandler - 實現hold手勢的接口
  • IInputClickHandler - 實現click手勢的接口
  • IInputHandler - 實現pointer-like手勢的接口
  • IManipulationHandler - 實現manipulation手勢的接口
  • INavigationHandler - 實現navigation手勢的接口
  • ISourceStateHandler - 實現檢測輸入源狀態的接口,如手勢檢測或者丟失
  • ISpeechHandler - 實現語音識別的接口
通過接口的方式來定義對應手勢的事件,要比原有的GestureManager.cs和HandsManager.cs方便許多。
以下是實現手勢旋轉的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity.InputModule;

public class GestureController : MonoBehaviour, INavigationHandler
{
    public float rotationSensitivity = 10.0f;

    private Vector3 manipulationPreviousPostion;
    private float rotationFactor;

    public void OnNavigationStarted(NavigationEventData eventData)
    {

    }
    //手勢旋轉物體。
    public void OnNavigationUpdated(NavigationEventData eventData)
    {
        //Calculate rotationFactor based on eventData.CumulativeDelta.x and multiply by rotationSensitivity.
        rotationFactor = eventData.CumulativeDelta.x * rotationSensitivity;

        //transform.Rotate along the Y axis using rotationFactor.
        transform.Rotate(new Vector3(0, -1 * rotationFactor, 0));
    }

    public void OnNavigationCompleted(NavigationEventData eventData)
    {

    }

    public void OnNavigationCanceled(NavigationEventData eventData)
    {

    }

}
以上代碼通過實現INavigation接口,實現了通過手勢旋轉物體。
如果需要定製其他手勢的事件,只需要實現響應的接口即可。
也可以參考HoloToolKit/Input/Tests/InputManagerTest場景中的示例。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章