Skip to content

Commit

Permalink
fix(Interaction): ensure unity event helper doesn't crash
Browse files Browse the repository at this point in the history
The Unity Event helper for the Interactable Object was crashing due
to the Interactable Object not being set at runtime.

This has now been fixed and the default event args are now used rather
than using a generic object.
  • Loading branch information
thestonefox committed Sep 3, 2016
1 parent 4eba75a commit 79ff3dc
Showing 1 changed file with 74 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,88 @@
using UnityEngine.Events;
using VRTK;

[RequireComponent (typeof(VRTK_InteractableObject))]
public class VRTK_InteractableObject_UnityEvents : MonoBehaviour
[RequireComponent(typeof(VRTK_InteractableObject))]
public class VRTK_InteractableObject_UnityEvents : MonoBehaviour
{

private VRTK_InteractableObject io;
private VRTK_InteractableObject io;

[System.Serializable]
public class UnityObjectEvent: UnityEvent<GameObject> {};
public UnityObjectEvent OnTouch;
public UnityObjectEvent OnUntouch;
public UnityObjectEvent OnGrab;
public UnityObjectEvent OnUngrab;
public UnityObjectEvent OnUse;
public UnityObjectEvent OnUnuse;
[System.Serializable]
public class UnityObjectEvent : UnityEvent<InteractableObjectEventArgs> { };
public UnityObjectEvent OnTouch;
public UnityObjectEvent OnUntouch;
public UnityObjectEvent OnGrab;
public UnityObjectEvent OnUngrab;
public UnityObjectEvent OnUse;
public UnityObjectEvent OnUnuse;

private void OnEnable ()
{
io.InteractableObjectTouched += Touch;
io.InteractableObjectUntouched += UnTouch;
io.InteractableObjectGrabbed += Grab;
io.InteractableObjectUngrabbed += UnGrab;
io.InteractableObjectUsed += Use;
io.InteractableObjectUnused += Unuse;
}
private void SetInteractableObject()
{
if (io == null)
{
io = GetComponent<VRTK_InteractableObject>();
}
}

private void Touch(object o, InteractableObjectEventArgs e)
{
OnTouch.Invoke(e.interactingObject);
}
private void OnEnable()
{
SetInteractableObject();
if (io == null)
{
Debug.LogError("The VRTK_InteractableObject_UnityEvents script requires to be attached to a GameObject that contains a VRTK_InteractableObject script");
return;
}

private void UnTouch(object o, InteractableObjectEventArgs e)
{
OnUntouch.Invoke(e.interactingObject);
}
io.InteractableObjectTouched += Touch;
io.InteractableObjectUntouched += UnTouch;
io.InteractableObjectGrabbed += Grab;
io.InteractableObjectUngrabbed += UnGrab;
io.InteractableObjectUsed += Use;
io.InteractableObjectUnused += Unuse;
}

private void Grab(object o, InteractableObjectEventArgs e)
{
OnGrab.Invoke(e.interactingObject);
}
private void Touch(object o, InteractableObjectEventArgs e)
{
OnTouch.Invoke(e);
}

private void UnGrab(object o, InteractableObjectEventArgs e)
{
OnUngrab.Invoke(e.interactingObject);
}
private void UnTouch(object o, InteractableObjectEventArgs e)
{
OnUntouch.Invoke(e);
}

private void Use(object o, InteractableObjectEventArgs e)
{
OnUse.Invoke(e.interactingObject);
}
private void Grab(object o, InteractableObjectEventArgs e)
{
OnGrab.Invoke(e);
}

private void Unuse(object o, InteractableObjectEventArgs e)
{
OnUnuse.Invoke(e.interactingObject);
}
private void UnGrab(object o, InteractableObjectEventArgs e)
{
OnUngrab.Invoke(e);
}

private void OnDisable ()
{
io.InteractableObjectTouched -= Touch;
io.InteractableObjectUntouched -= UnTouch;
io.InteractableObjectGrabbed -= Grab;
io.InteractableObjectUngrabbed -= UnGrab;
io.InteractableObjectUsed -= Use;
io.InteractableObjectUnused -= Unuse;
}
}
private void Use(object o, InteractableObjectEventArgs e)
{
OnUse.Invoke(e);
}

private void Unuse(object o, InteractableObjectEventArgs e)
{
OnUnuse.Invoke(e);
}

private void OnDisable()
{
if (io == null)
{
return;
}

io.InteractableObjectTouched -= Touch;
io.InteractableObjectUntouched -= UnTouch;
io.InteractableObjectGrabbed -= Grab;
io.InteractableObjectUngrabbed -= UnGrab;
io.InteractableObjectUsed -= Use;
io.InteractableObjectUnused -= Unuse;
}
}

0 comments on commit 79ff3dc

Please sign in to comment.