0
\$\begingroup\$

I am new to Unity and trying to modify the Unity tutorial Interactive 360 Sample. I want to be able to call a function when a user swipes using an Oculus Go Controller. I noticed that the VRInput script is included in the project, which seems to be able to detect swipe gestures. But where do I attach that script, and how do I subscribe to the OnSwipe event.

I am currently trying by making an empty game object, and attaching the InputVR script as well as my own script called "GestureHandler" (I'm not sure whether InputVR is already attached to something else in the sample project, or whether I need to attach it at all). In my GestureHandler script I currently have the following:

using System.Collections;
using System.Collections.Generic;
using Interactive360.Utils;
using UnityEngine;

public class GestureHandler : MonoBehaviour {

    void onEnable(){
        VRInput.OnSwipe += HandleSwipe;
    }

    void onDisable(){
        VRInput.OnSwipe -= HandleSwipe;
    }

    void HandleSwipe(){
        Debug.Log("swipe");
    }
}

and currently have the following errors:

An object reference is required for the non-static field, method, or property 'VRInput.OnSwipe' [Assembly-CSharp]

No overload for 'HandleSwipe' matches delegate 'Action<VRInput.SwipeDirection>' [Assembly-CSharp]

An object reference is required for the non-static field, method, or property 'VRInput.OnSwipe' [Assembly-CSharp]

No overload for 'HandleSwipe' matches delegate 'Action<VRInput.SwipeDirection>' [Assembly-CSharp]
\$\endgroup\$
0
\$\begingroup\$

It is because

  1. OnSwipe is not a static event, so you need a reference to a VRInput.
  2. OnSwipe's signature receives a VRInput.SwipeDirection.

So it goes like this:

using System.Collections;
using UnityEngine;
using VRStandardAssets.Utils;

public class Example : MonoBehaviour
{
    public VRInput input;

    void OnEnable()
    {
        input.OnSwipe += HandleSwipe;
    }

    void OnDisable()
    {
        input.OnSwipe -= HandleSwipe;
    }

    private void HandleSwipe(VRInput.SwipeDirection swipeDirection)
    {
        Debug.Log("Swipe Direction: " + swipeDirection.ToString());
    }
}

Hope it helps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.