How Do I Get Controller Objects in Runtime

Introduction

VIVE Wave™ uses WaveVR_ControllerLoader to load the controller in runtime. Therefore, controller objects cannot be modified in editor.

In Assets/WaveVR/Extra/GenericModel/Finch/Resources/Controller, the Generic Controller Model prefabs shows the structure of a VIVE Wave™ controller.

_images/qa_getcontroller_01.png

The prefab contains the Model, Beam and Pointer.

Go to Find the Beam and the Pointer in the Controller to learn how to get the Beam and Pointer.

Find the Beam and the Pointer in the Controller

Follow the steps below to find the Beam and Pointer.

  1. Listen to the CONTROLLER_MODEL_LOADED broadcast to receive the controller instance.
  2. Find all the children of the controller.
  3. Find GameObject with beam / pointer in children.
private GameObject dominantController = null, nonDominantController = null;
void OnEnable()
{
    WaveVR_Utils.Event.Listen (WaveVR_Utils.Event.CONTROLLER_MODEL_LOADED, OnControllerLoaded);
}

void OnControllerLoaded(params object[] args)
{
    WaveVR_Controller.EDeviceType _type = (WaveVR_Controller.EDeviceType)args [0];
    if (_type == WaveVR_Controller.EDeviceType.Dominant)
    {
        this.dominantController = (GameObject)args [1];
        listControllerObjects(this.dominantController);
    }
    if (_type == WaveVR_Controller.EDeviceType.NonDominant)
    {
        this.nonDominantController = (GameObject)args [1];
        listControllerObjects(this.nonDominantController);
    }
}

void OnDisable()
{
    WaveVR_Utils.Event.Remove (WaveVR_Utils.Event.CONTROLLER_MODEL_LOADED, OnControllerLoaded);
}

private void listControllerObjects(GameObject ctrlr)
{
    if (ctrlr == null)
        return;

    WaveVR_Beam _beam = null;
    WaveVR_ControllerPointer _pointer = null;

    // Get all children.
    GameObject[] _objects = new GameObject[ctrlr.transform.childCount];
    for (int i = 0; i < ctrlr.transform.childCount; i++)
        _objects[i] = ctrlr.transform.GetChild (i).gameObject;

    // Find beam.
    for (int i = 0; i < _objects.Length; i++)
    {
        _beam = _objects [i].GetComponentInChildren<WaveVR_Beam> ();
        if (_beam != null)
            break;
    }
    if (_beam != null)
        Debug.Log ("Find beam: " + _beam.name);

    // Find pointer.
    for (int i = 0; i < _objects.Length; i++)
    {
        _pointer = _objects [i].GetComponentInChildren<WaveVR_ControllerPointer> ();
        if (_pointer != null)
            break;
    }
    if (_pointer != null)
        Debug.Log ("Find pointer: " + _pointer.name);
}