Last active
September 14, 2017 15:34
-
-
Save demonixis/a7bab9162a1c776151cdd16e2ae3eecb to your computer and use it in GitHub Desktop.
The service I use in GunSpinningVR to detect controllers availability. `GunController` is the object responsible to controll guns and `UnityVRController` is the object responsible to update position/rotation/button states.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
#if UNITY_WSA | |
using UnityEngine.XR.WSA.Input; | |
#endif | |
namespace Demonixis.GunSpinningVR.Controllers | |
{ | |
public sealed class WindowsMixedRealityService : MonoBehaviour | |
{ | |
#if UNITY_WSA | |
private void Start() | |
{ | |
InteractionManager.InteractionSourceDetected += InteractionManager_InteractionSourceDetected; | |
InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated; | |
} | |
private void InteractionManager_InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj) | |
{ | |
TryActiveControllers(obj.state); | |
} | |
private void InteractionManager_InteractionSourceDetected(InteractionSourceDetectedEventArgs obj) | |
{ | |
TryActiveControllers(obj.state); | |
} | |
private void TryActiveControllers(InteractionSourceState state) | |
{ | |
if (state.source.kind == InteractionSourceKind.Controller) | |
{ | |
InteractionManager.InteractionSourceDetected -= InteractionManager_InteractionSourceDetected; | |
InteractionManager.InteractionSourceUpdated -= InteractionManager_InteractionSourceUpdated; | |
var manager = GetComponent<GunController>(); | |
var controller = GetComponent<UnityVRController>(); | |
manager.SetInputDevice(controller); | |
Destroy(this); | |
} | |
} | |
#else | |
private void Awake() | |
{ | |
Destroy(this); | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment