Example #1
0
        private void Update()
        {
            // =====================================
            // left
            // =====================================
            var stateL = XRInput.ControllerState(XRController.Left);

            // touch
            PrintButton(stateL.touchTrigger, "Left - TouchTrigger");
            PrintButton(stateL.touchJoystick, "Left - TouchJoystick");
            PrintButton(stateL.touchJoystick2, "Left - TouchJoystick2");
            PrintButton(stateL.touchGrip, "Left - TouchGrip");
            PrintButton(stateL.touchMenu, "Left - TouchMenu");
            PrintButton(stateL.touch1, "Left - Touch1");
            PrintButton(stateL.touch2, "Left - Touch2");
            PrintButton(stateL.touch3, "Left - Touch3");
            PrintButton(stateL.touch4, "Left - Touch4");

            // buttons
            PrintButton(stateL.buttonTrigger, "Left - ButtonTrigger");
            PrintButton(stateL.buttonJoystick, "Left - ButtonJoystick");
            PrintButton(stateL.buttonJoystick2, "Left - ButtonJoystick2");
            PrintButton(stateL.buttonGrip, "Left - ButtonGrip");
            PrintButton(stateL.buttonMenu, "Left - ButtonMenu");
            PrintButton(stateL.button1, "Left - Button1");
            PrintButton(stateL.button2, "Left - Button2");
            PrintButton(stateL.button3, "Left - Button3");
            PrintButton(stateL.button4, "Left - Button4");

            // triggers
            PrintAnalog(stateL.trigger, "Left - Trigger");

            // grips
            PrintAnalog(stateL.grip, "Left - Grip");

            // joysticks
            PrintJoystick(stateL.joystick, "Left - Joystick");
            PrintJoystick(stateL.joystick2, "Left - Joystick2");

            // rumble
            if (stateL.trigger.value != 0)
            {
                XRInput.SetRumble(XRControllerRumbleSide.Left, stateL.trigger.value);
            }

            // =====================================
            // right
            // =====================================
            var stateR = XRInput.ControllerState(XRController.Right);

            // touch
            PrintButton(stateR.touchTrigger, "Right - TouchTrigger");
            PrintButton(stateR.touchJoystick, "Right - TouchJoystick");
            PrintButton(stateR.touchJoystick2, "Right - TouchJoystick2");
            PrintButton(stateR.touchGrip, "Right - TouchGrip");
            PrintButton(stateR.touchMenu, "Right - TouchMenu");
            PrintButton(stateR.touch1, "Right - Touch1");
            PrintButton(stateR.touch2, "Right - Touch2");
            PrintButton(stateR.touch3, "Right - Touch3");
            PrintButton(stateR.touch4, "Right - Touch4");

            // buttons
            PrintButton(stateR.buttonTrigger, "Right - ButtonTrigger");
            PrintButton(stateR.buttonJoystick, "Right - ButtonJoystick");
            PrintButton(stateR.buttonJoystick2, "Right - ButtonJoystick2");
            PrintButton(stateR.buttonGrip, "Right - ButtonGrip");
            PrintButton(stateR.buttonMenu, "Right - ButtonMenu");
            PrintButton(stateR.button1, "Right - Button1");
            PrintButton(stateR.button2, "Right - Button2");
            PrintButton(stateR.button3, "Right - Button3");
            PrintButton(stateR.button4, "Right - Button4");

            // triggers
            PrintAnalog(stateR.trigger, "Right - Trigger");

            // grips
            PrintAnalog(stateR.grip, "Right - Grip");

            // joysticks
            PrintJoystick(stateR.joystick, "Right - Joystick");
            PrintJoystick(stateR.joystick2, "Right - Joystick2");

            // rumble
            if (stateR.trigger.value != 0)
            {
                XRInput.SetRumble(XRControllerRumbleSide.Right, stateR.trigger.value);
            }
        }
Example #2
0
        private IEnumerator Start()
        {
            // only one can exist in scene at a time
            if (singleton != null)
            {
                disposeAPI = false;// don't dispose apis if we're not the owner of possible native instances
                Destroy(gameObject);
                yield break;
            }
            DontDestroyOnLoad(gameObject);
            singleton = this;

            // print version
            Debug.Log("XRInput version: 1.0.11");

            // wait for XR loader
            while (loader == null || !XRSettings.enabled)
            {
                try
                {
                    if (loaderOverride != null)
                    {
                        loader = loaderOverride;
                    }
                    else
                    {
                        loader = XRGeneralSettings.Instance.Manager.activeLoader;
                    }
                }
                catch { }
                yield return(new WaitForSeconds(1));
            }

            // give XR some time to start
            var wait = new WaitForEndOfFrame();

            yield return(wait);

            yield return(wait);

            yield return(wait);

            yield return(wait);

            yield return(wait);

            yield return(new WaitForSeconds(1));

            try
            {
                // init controller instance IDs
                for (int i = 0; i != state_controllers.Length; ++i)
                {
                    state_controllers[i].id = Guid.NewGuid();
                }

                // get loader type
                var    loaderType     = loader.GetType();
                string loaderTypeName = loaderType.Name;
                Debug.Log($"XR-Loader: '{loader.name}' TYPE:{loaderType}");

                // auto set rumble channel
                if (autoSetRumbleChannel)
                {
                    rumbleChannel = 0;
                }

                // auto detect
                if (apiType == XRInputAPIType.AutoDetect)
                {
                    #if UNITY_STANDALONE
                    if (loaderTypeName == "OpenVRLoader")
                    {
                        apiType = XRInputAPIType.OpenVR;
                    }
                    else
                    {
                        apiType = XRInputAPIType.UnityEngine_XR;
                    }
                    #else
                    apiType = XRInputAPIType.UnityEngine_XR;
                    #endif
                }

                // init api
                disposeAPI = true;
                switch (apiType)
                {
                case XRInputAPIType.UnityEngine_XR: api = new UnityEngine_XR(); break;

                case XRInputAPIType.OpenVR: api = new OpenVR_New(); break;

                case XRInputAPIType.OpenVR_Legacy: api = new OpenVR_Legacy(); break;

                default: throw new NotImplementedException();
                }

                api.Init();

                // ensure we dispose before stopping to avoid editor race-condition bugs
                #if UNITY_EDITOR
                UnityEditor.EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged;
                #endif

                apiInit = true;
            }
            catch (Exception e)
            {
                if (InitializedCallback != null)
                {
                    InitializedCallback(false);
                }
                throw e;
            }

            if (InitializedCallback != null)
            {
                InitializedCallback(true);
            }
        }