DeltaVR/Assets/Scripts/UnityXR/HandPresence.cs
2020-12-29 20:15:49 +02:00

91 lines
2.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
namespace UnityXR
{
public class HandPresence : MonoBehaviour
{
public bool showController = false;
public InputDeviceCharacteristics controllerCharacteristics;
public List<GameObject> controllerPrefabs;
public GameObject handModelPrefab;
private InputDevice _targetDevice;
private GameObject _spawnedController;
private GameObject _spawnedHandModel;
private Animator _handAnimator;
// Start is called before the first frame update
void Start()
{
}
void TryInitialize()
{
List<InputDevice> devices = new List<InputDevice>();
InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);
if (devices.Count > 0)
{
_targetDevice = devices[0];
GameObject prefab = controllerPrefabs.Find(controller => controller.name == _targetDevice.name);
if (prefab)
{
_spawnedController = Instantiate(prefab, transform);
}
else
{
_spawnedController = Instantiate(controllerPrefabs[0], transform);
}
_spawnedHandModel = Instantiate(handModelPrefab, transform);
_handAnimator = _spawnedHandModel.GetComponent<Animator>();
}
}
void UpdateHandAnimation()
{
if (_targetDevice.TryGetFeatureValue(CommonUsages.trigger, out float triggerValue))
{
_handAnimator.SetFloat("Trigger", triggerValue);
}
else
{
_handAnimator.SetFloat("Trigger", 0);
}
if (_targetDevice.TryGetFeatureValue(CommonUsages.grip, out float gripValue))
{
_handAnimator.SetFloat("Grip", gripValue);
}
else
{
_handAnimator.SetFloat("Grip", 0);
}
}
// Update is called once per frame
void Update()
{
if (_targetDevice.isValid)
{
TryInitialize();
}
else
{
if (showController)
{
_spawnedHandModel.SetActive(false);
_spawnedController.SetActive(true);
}
else
{
_spawnedHandModel.SetActive(true);
_spawnedController.SetActive(false);
UpdateHandAnimation();
}
}
}
}
}