Search Unity

Events in steamvr 2.0 ?

Discussion in 'VR' started by Roggi_, Dec 28, 2018.

  1. Roggi_

    Roggi_

    Joined:
    May 14, 2014
    Posts:
    84
    Hi, before steamvr 2.0 came out, I could do input events in custom scripts like this:

    Code (CSharp):
    1.  
    2.    public SteamVR_TrackedController cr1;
    3.  
    4.     void Start () {
    5.  
    6.         cr1.TriggerClicked += HandleTrigger;
    7.     }
    8.  
    9.     void HandleTrigger(object sender,ClickedEventArgs e){
    10.  
    11.         //DoStuff();
    12.  
    13.     }


    After the update however, with the new binding system, I have no idea how to achieve the same thing. How can I add a method to an input that will be called automaticly like before? I can't find an example of that anywhere.

    Thanks for any help.
     
  2. hard_code

    hard_code

    Joined:
    Aug 29, 2013
    Posts:
    238
  3. Roggi_

    Roggi_

    Joined:
    May 14, 2014
    Posts:
    84
    Thanks, that's not what Iam asking though. I know I can check that manually but that means the update loop will have 40 ifs, each asking for a different input and state.

    Having the events call methods automaticaly like in the previous verions saves a lot of code and makes it far more readable. How can I do that with the new system? Did they just straight up remove the feature?
     
  4. hard_code

    hard_code

    Joined:
    Aug 29, 2013
    Posts:
    238
    Each action type has an OnChangeListener but it's slightly different for each action type I think.

    I would look in the editor input components they supply such as SteamVR_Behaviour_Single, SteamVR_Behaviour_Boolean etc that watch for changes in state and then call a method like you want to do.


    Here is the code for a SteamVR_Behaviour_Single.

    Code (CSharp):
    1. private void OnEnable()
    2.         {
    3.             singleAction.AddOnChangeListener(ActionChanged, inputSource);
    4.         }
    5.  
    6.         private void OnDisable()
    7.         {
    8.             singleAction.RemoveOnChangeListener(ActionChanged, inputSource);
    9.         }
    10.  
    11.         private void ActionChanged(SteamVR_Action_In action)
    12.         {
    13.             if (onChange != null)
    14.             {
    15.                 onChange.Invoke((SteamVR_Action_Single)action);
    16.             }
    17.         }
     
    Last edited: Jan 2, 2019