본문 바로가기
Unity/VR 개발하기

[Steam VR 사용하기] 5. Input Data 기반 Script 작성하기.

by 민트코코넛 2021. 9. 11.
728x90
반응형

▶ Unity C# Script 활용한 Vive controller Input Function 구현하기

> Trigger Button을 사용하여 Grab 기능 구현

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;                                             // Steam VR namespace. Steam VR plugin을 사용할 때는 무조건 사용해야 한다.

public class ViveInputCtrl : MonoBehaviour
{
    // SteamVR_Action : SteamVR Input에서 생성한 Action 호출
    // SteamVR_Action_Booleam : Input에서 생성한 Action의 type이 boolean인 Action을 특정할 때 호출
    public SteamVR_Action_Boolean Grab;

    // Grab 버튼을 누를 때 반응하도록 생성
    void ClickGrab()
    {
        // Action을 기기의 part를 통해서 호출할 때는, Input에서 생성한 순서 반대로 실행한다.
        // binding -> type -> name -> Action 
        // GetState = GetMouse, GetButton 과 동일한 기능을 하는 function
        // GetState : 기기의 어떤 버튼을 지속적으로 누르고 있는 상태
        // GetStatDown : 기기의 어떤 버튼을 한번 누른 상태
        // GetStateUp : 기기의 어떤 버튼을 한번 눌렀다가 땐 상태
        if (Grab.GetStateDown(SteamVR_Input_Sources.Any))
        // SteamVR_Input_Sources : 착용하는 장비 중에서 어떤 장비에서 해당 값을 가져올 것인지 확인하는 것
        // SteamVR_Input_Sources.Any : 어떤 장비여도 상관없이 가져오겠다는 것
        // 특정 장비에서 가져오겠다고 한다면, 해당 장비의 명칭을 호출할 것
        {
            Debug.Log("Grab Button Down");
        }
    }
    private void FixedUpdate()
    {
        ClickGrab();
    }
}

> Script 작성 후, Unity에서 설정해야 하는 사항

>> 1. Empty object를 만들고, 해당 객체 아래에 Camera Rig Prefab 위치하기

>> 2. Empty object의 이름을 기억하기 좋은 이름으로 변경, 저장하기 ( ex. player, user ... )

>> 3. player로 변경한 객체에 작성한 ViveInputCtrl 스크립트 추가하기

>> 4. Custom하게 작성한 Vive Input Ctrl 스크립트 component를 확인했을때, Grab 기능에서 None 으로 설정되어 있는 것을 ▽ 버튼을 누르고, 사용하고자 하는 Action Set의 function으로 변경한다.

>> 5. player 객체를 선택하고, Add Component로 SteamVR_Activate Action Set On Load(Script) 를 추가한다.

>>> SteamVR_Activate Action Set On Load Script는 Unity에서 vive 장비를 인식하고, 활성화될 수 있도록 설정해놓은 script이고, Steam을 사용해서 VR을 개발할때, 필수로 사용해야 한다.

>> 6. Action Set를 현재 사용하는 Action Set로 변경하고, For Source도 변경해준다.

>> 7. Unity Editor 에서 재생 버튼을 누르고, 사용하고 있는 Steam Vive 장비를 착용한 후, Grab 버튼 기능을 연결해놓은 part를 눌러서 작동 여부를 확인한다.

>>> Vive Input Ctrl script에서 작성했었던, Grab 버튼에 대한 Return 값이 제대로 출력된다면, 해당 버튼을 활성화하는 것은 성공한 것이다.

* SteamVR_Activate Action Set On Load(Script) 정보

//======= Copyright (c) Valve Corporation, All rights reserved. ===============

using UnityEngine;
using System.Collections;

namespace Valve.VR
{
    /// <summary>
    /// Automatically activates an action set on Start() and deactivates the set on OnDestroy(). Optionally deactivating all other sets as well.
    /// </summary>
    public class SteamVR_ActivateActionSetOnLoad : MonoBehaviour
    {
        public SteamVR_ActionSet actionSet = SteamVR_Input.GetActionSet("default");

        public SteamVR_Input_Sources forSources = SteamVR_Input_Sources.Any;

        public bool disableAllOtherActionSets = false;

        public bool activateOnStart = true;
        public bool deactivateOnDestroy = true;

        public int initialPriority = 0;

        private void Start()
        {
            if (actionSet != null && activateOnStart)
            {
                //Debug.Log(string.Format("[SteamVR] Activating {0} action set.", actionSet.fullPath));
                actionSet.Activate(forSources, initialPriority, disableAllOtherActionSets);
            }
        }

        private void OnDestroy()
        {
            if (actionSet != null && deactivateOnDestroy)
            {
                //Debug.Log(string.Format("[SteamVR] Deactivating {0} action set.", actionSet.fullPath));
                actionSet.Deactivate(forSources);
            }
        }
    }
}
728x90