(Unity SDK) Hide controller model at runtime

SOLVED
cmdr2
Contributor

(Unity SDK) Hide controller model at runtime

Hi,

 

I'm looking for a way to hide the controller model at runtime, since I need to show the controller only during parts of the game. I'm using the Unity SDK.

 

Presently what works for me (but I'm not sure if this is the correct, future-proof way):

 

1. Store the Controller's GameObject provided by the CONTROLLER_MODEL_LOADED event: WaveVR_Utils.Event.Listen(WaveVR_Utils.Event.CONTROLLER_MODEL_LOADED, onControllerLoadedEvent);

2. Keep setting that controllerGameObject.SetActive to false in an Update(), since it seems to become active a few frames after the event callback above.

 

I verified that the controller's orientation data continues to be reported correctly, even when the controller's gameObject is hidden.

 

I was curious to know if there's a better way to do this.

 

Thanks!

~cmdr2

Vive Staff
Solution

Re: (Unity SDK) Hide controller model at runtime

Hi @cmdr2,

 

Yes, this is a correct way and easiest way.

We also provide alternative way for your reference.

 

Assuming listening to CONTROLLER_MODEL_LOADED :

 

 

    void OnEnable()
    {
        WaveVR_Utils.Event.Listen (WaveVR_Utils.Event.CONTROLLER_MODEL_LOADED, OnControllerLoaded);
    }

 

    // In SDK 2.1.8
    private GameObject loadedController_R = null, loadedController_L;
    void OnControllerLoaded(params object[] args)
    {
        WVR_DeviceType _type = (WVR_DeviceType)args [0];
        if (_type == WVR_DeviceType.WVR_DeviceType_Controller_Right)
            this.loadedController_R = (GameObject)args [1];
        if (_type == WVR_DeviceType.WVR_DeviceType_Controller_Left)
            this.loadedController_L = (GameObject)args [1];
    }

 

 

Then you can call ShowControllerModels (bool) to show / hide controller models.

 

    // True to show, false to hide controller models.
    private void ShowControllerModels(bool show)
    {
        int _children_count =  this.loadedController_R.transform.childCount;
        for (int i = 0; i < _children_count; i++)
        {
            this.loadedController_R.transform.GetChild (i).gameObject.SetActive (show);
        }

        _children_count =  this.loadedController_L.transform.childCount;
        for (int i = 0; i < _children_count; i++)
        {
            this.loadedController_L.transform.GetChild (i).gameObject.SetActive (show);
        }
    }

 Hope this is helpful.

Thanks.

cmdr2
Contributor

Re: (Unity SDK) Hide controller model at runtime

Thanks for your help!