コード例 #1
0
        /// <summary>
        /// Handles the event when a controller connects. If the connected controller
        /// is valid, we add it to the _allowedConnectedDevices list.
        /// </summary>
        /// <param name="controllerId">The id of the controller.</param>
        private void HandleOnControllerConnected(byte controllerId)
        {
            MLInputController newController = MLInput.GetController(controllerId);

            if (IsDeviceAllowed(newController))
            {
                if (_allowedConnectedDevices.Exists((device) => device.Id == controllerId))
                {
                    Debug.LogWarning(string.Format("Connected controller with id {0} already connected.", controllerId));
                    return;
                }

                _allowedConnectedDevices.Add(newController);

                // Notify Listeners
                if (OnControllerConnected != null)
                {
                    OnControllerConnected.Invoke(controllerId);
                }
            }
        }
コード例 #2
0
            private void ComputeControllerTypes()
            {
                //Remove empty string to ignore disconnected sticks
                string[]      unityNames   = Input.GetJoystickNames();
                List <string> tweakedNames = new List <string>(unityNames);

                for (int i = tweakedNames.Count - 1; i >= 0; i--)
                {
                    if (tweakedNames[i] == null || tweakedNames[i] == string.Empty)
                    {
                        tweakedNames.RemoveAt(i);

                        //Check to trigger disconnected event
                        if (_disconnectedControllers.Contains(i + 1) == false)
                        {
                            _disconnectedControllers.Add(i + 1);
                            //i + 1 will be the controller ID
                            OnControllerDisconnected.Invoke(i + 1);
                        }
                    }
                    //Check to trigger (re)connected event
                    else if (_disconnectedControllers.Contains(i + 1) == true)
                    {
                        _disconnectedControllers.Remove(i + 1);
                        //i + 1 will be the controller ID
                        OnControllerConnected.Invoke(i + 1);
                    }
                }

                //Check to trigger new controller connected event
                if (unityNames.Length > _unityControllerNames.Length)
                {
                    for (int i = _unityControllerNames.Length; i < unityNames.Length; i++)
                    {
                        OnControllerConnected.Invoke(i + 1);
                    }
                }

                //Only update if new controllers found
                if (tweakedNames.Count != _controllerNames.Length)
                {
                    _unityControllerNames = unityNames;
                    _controllerNames      = tweakedNames.ToArray();
                    _controllerTypes      = new ControllerType[_controllerNames.Length];

                    //Reset inputs aswell
                    _lastAxesInputs = new InputDirection[_controllerNames.Length, AXES_COUNT];
                    _axesInputs     = new InputDirection[_controllerNames.Length, AXES_COUNT];

                    //Then check name length to know weither its a PS4 or XBOX controller
                    for (int i = 0; i < _controllerNames.Length; i++)
                    {
                        switch (_controllerNames[i].Length)
                        {
                        case 19:
                            _controllerTypes[i] = ControllerType.PS4;
                            continue;

                        case 33:
                            _controllerTypes[i] = ControllerType.xBox;
                            continue;
                        }

                        _controllerTypes[i] = ControllerType.xBox;
                    }

                    //Lastly triggger count changed event
                    OnControllerCountChange.Invoke(_controllerNames.Length);
                }
            }