Skip to content

Instantly share code, notes, and snippets.

@shop-0761
Last active January 26, 2019 00:49
Show Gist options
  • Save shop-0761/a8596fda871de0c999053acb915efb8c to your computer and use it in GitHub Desktop.
Save shop-0761/a8596fda871de0c999053acb915efb8c to your computer and use it in GitHub Desktop.
SteamVR Plugin 2.0 で入力を取るサンプル 要UniRx
using UnityEngine;
using Valve.VR;
using UniRx;
public class SteamVRNotifierBase
{
public string buttonName;
public bool isLeft;
}
public class SteamVRInputBoolNotifier : SteamVRNotifierBase { public bool value; }
public class SteamVRInputSingleNotifier : SteamVRNotifierBase { public float value; }
public class SteamVRInputVector2Notifier : SteamVRNotifierBase { public Vector2 value; }
public class SteamVRInputManager : MonoBehaviour
{
SteamVRInputBoolNotifier boolNotifier = new SteamVRInputBoolNotifier();
SteamVRInputSingleNotifier singleNotifier = new SteamVRInputSingleNotifier();
SteamVRInputVector2Notifier vector2Notifier = new SteamVRInputVector2Notifier();
SteamVR_Input_Sources[] sources;
SteamVR_ActionSet[] actionSets;
void Start()
{
actionSets = SteamVR_Input.actionSets;
if (actionSets == null) { actionSets = SteamVR_Input_References.instance.actionSetObjects; }
//サンプル
// MessageBroker.Default.Receive<SteamVRInputBoolNotifier>().Subscribe(x => Debug.Log("receive: " + x.buttonName + " value: " + x.value + " isLeft: " + x.isLeft));
// MessageBroker.Default.Receive<SteamVRInputSingleNotifier>().Subscribe(x => Debug.Log("receive: " + x.buttonName + " value: " + x.value + " isLeft: " + x.isLeft));
// MessageBroker.Default.Receive<SteamVRInputVector2Notifier>().Subscribe(x => Debug.Log("receive: " + x.buttonName + " value: " + x.value + " isLeft: " + x.isLeft));
}
void Update()
{
sources = SteamVR_Input_Source.GetUpdateSources();
//sourceindex : leftHand = 1
// : rightHand = 2
// : any = 3
//anyはいらなかったので、 sources.Length - 1 に
for (int sourceIndex = 0; sourceIndex < sources.Length - 1; sourceIndex++)
{
var source = sources[sourceIndex];
for (int actionSetIndex = 0; actionSetIndex < actionSets.Length; actionSetIndex++)
{
var actionSet = actionSets[actionSetIndex];
for (int actionIndex = 0; actionIndex < actionSet.allActions.Length; actionIndex++)
{
var action = actionSet.allActions[actionIndex];
if (action.actionSet == null || action.actionSet.IsActive() == false) { continue; }
//ボタン系
if (action is SteamVR_Action_Boolean)
{
var actionBoolean = (SteamVR_Action_Boolean)action;
boolNotifier.buttonName = action.GetShortName();
boolNotifier.value = actionBoolean.GetState(source);
boolNotifier.isLeft = sourceIndex == 0;
MessageBroker.Default.Publish(boolNotifier);
}
//float系 Triggerとか
else if (action is SteamVR_Action_Single)
{
var actionSingle = (SteamVR_Action_Single)action;
singleNotifier.buttonName = action.GetShortName();
singleNotifier.value = actionSingle.GetAxis(source);
singleNotifier.isLeft = sourceIndex == 0;
MessageBroker.Default.Publish(singleNotifier);
}
//facePadとか
else if (action is SteamVR_Action_Vector2)
{
var actionVector2 = (SteamVR_Action_Vector2)action;
vector2Notifier.buttonName = action.GetShortName();
vector2Notifier.value = actionVector2.GetAxis(source);
vector2Notifier.isLeft = sourceIndex == 0;
MessageBroker.Default.Publish(vector2Notifier);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment