Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

79 lines
2.3 KiB

namespace VRTK.Examples
{
using UnityEngine;
using UnityEventHelper;
using VRTK.Controllables;
public class ControlReactor : MonoBehaviour
{
public TextMesh go;
#pragma warning disable 0618
protected VRTK_Control_UnityEvents controlEvents;
#pragma warning restore 0618
protected VRTK_BaseControllable controllableEvents;
protected virtual void OnEnable()
{
#pragma warning disable 0618
if (GetComponent<VRTK_Control>() != null && GetComponent<VRTK_Control_UnityEvents>() == null)
{
controlEvents = gameObject.AddComponent<VRTK_Control_UnityEvents>();
}
controlEvents = GetComponent<VRTK_Control_UnityEvents>();
#pragma warning restore 0618
controllableEvents = GetComponent<VRTK_BaseControllable>();
ManageListeners(true);
}
protected virtual void OnDisable()
{
ManageListeners(false);
}
protected virtual void ManageListeners(bool state)
{
if (state)
{
if (controlEvents != null)
{
controlEvents.OnValueChanged.AddListener(HandleChange);
}
if (controllableEvents != null)
{
controllableEvents.ValueChanged += ValueChanged;
}
}
else
{
if (controlEvents != null)
{
controlEvents.OnValueChanged.RemoveListener(HandleChange);
}
if (controllableEvents != null)
{
controllableEvents.ValueChanged -= ValueChanged;
}
}
}
protected virtual void ValueChanged(object sender, ControllableEventArgs e)
{
UpdateText(e.value.ToString("F2"), (e.normalizedValue * 100f).ToString("F0"));
}
protected virtual void HandleChange(object sender, Control3DEventArgs e)
{
UpdateText(e.value.ToString("F2"), (e.normalizedValue * 100f).ToString("F0"));
}
protected virtual void UpdateText(string valueText, string normalizedValueText)
{
if (go != null)
{
go.text = valueText;
}
}
}
}