Extensions for converting all the VRTK events to UniRx stream
Scripts are here.
For Release Notes and unitypackage, see RxVRTK/releases.
- VRTK v3.2.1
GitHub, Asset Store - UniRx
GitHub, Asset Store
RxVRTK mediates VRTK (VR Toolkit) and UniRx.
RxVRTK consists of two components: Event extensions and RxVRTK components.
Event extensions provide extension methods to convert an exposed VRTK event such as VRTK_ControllerEvents.TriggerPressed
to an Rx stream.
You can observe {EVENT NAME}
by using {EVENT NAME}AsObservable()
method.
// Add using directive
// using RxVRTK;
VRTK_ControllerEvents controllerEvents = GetComponent<VRTK_ControllerEvents>();
controllerEvents.TriggerPressedAsObservable()
.Subscribe(_ =>
{
Debug.Log("Trigger Pressed!");
});
Event extensions have a prefix "RxVRTK_" and a suffix "Extension".
RxVRTK components provide an event system that registers a callback by its method name and the registered callbacks are automatically called by the event system of VRTK.
An RxVRTK component inherits from VRTK classes and overrides the aforementioned methods so that it converts a VRTK event to Rx stream. When you use these components, you must not override such methods and must only use observable methods instead.
Let's say you want to register a callback for "StartUsing". In this case, VRTK calls "StartUsing" of an VRTK_InteractableObject instance if it implements a method that matches the following signature:
void StartUsing(VRTK_InteractUse currentUsingObject);
As these methods are already overridden in RxVRTK_InteractableObject, which inherits VRTK_InteractableObject to fire UniRx events and RxVRTK utilizes this technique to register an event callback, ensure that you do not use neither "StartUsing" nor new
to hide this method.
The base class of RxVRTK_InteractableObject is VRTK_InteractableObject. This class provides interactive events: StartUsing, StartTouching, Grabbed, etc.
using RxVRTK;
using UniRx;
using UnityEngine;
public class SomeObject : RxVRTK_InteractableObject
{
protected void Start()
{
this.StartUsingAsObservable()
.Subscribe(user =>
{
Debug.Log(user.name + " is using this object!");
});
}
}
RxVRTK components have a prefix "RxVRTK_" but no suffix.
VRTK_UnityEvents<T> uses UnityEvent<T>. UniRx has UnityEventExtensions which convert UnityEvent to Observable, so RxVRTK doesn't support UnityEvent. Please use UniRx.UnityEventExtensions.
AsObservable() is an extension method to convert UnityEvent to Observable.
// Add using directive
// using UniRx;
var controlEvents = GetComponent<VRTK_Control_UnityEvents>();
controlEvents.OnValueChanged.AsObservable()
.Subscribe(e =>
{
Debug.Log("Changed: "+ e.Item2.value.ToString());
});
This library is released under the MIT License.