Ejemplo n.º 1
0
    public IEnumerator CheckForControllerDC()
    {
        while (true)
        {
            var devices = Input.GetJoystickNames();

            //This assumes that everything that is detected by Unity, stays seen by GetJoystickNames, either by name or as empty. (Also skip 1 because of keyboard)
            for (int i = 1; i < this.PlayerMappings.Count; i++)
            {
                if (devices.Length >= this.PlayerMappings[i].OriginalIndex)
                {
                    if (string.IsNullOrEmpty(devices[this.PlayerMappings[i].OriginalIndex]))
                    {
                        if (!this.PlayerMappings[i].IsDisconnected)
                        {
                            this.PlayerMappings[i].IsDisconnected = true;
                            if (OnControllerDisconnected != null)
                            {
                                OnControllerDisconnected.Invoke(i);
                            }
                        }
                    }
                    else
                    {
                        this.PlayerMappings[i].IsDisconnected = false;
                    }
                }
            }
            yield return(new WaitForEndOfFrame());
        }
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles the event when a controller disconnects. If the disconnected
        /// controller happens to be in the _allowedConnectedDevices list, we
        /// remove it from the list.
        /// </summary>
        /// <param name="controllerId">The id of the controller.</param>
        private void HandleOnControllerDisconnected(byte controllerId)
        {
            // Remove from the list of allowed devices.
            int devicesRemoved = _allowedConnectedDevices.RemoveAll((device) => device.Id == controllerId);

            // Notify Listeners of the disconnected device.
            if (devicesRemoved > 0)
            {
                if (OnControllerDisconnected != null)
                {
                    OnControllerDisconnected.Invoke(controllerId);
                }
            }
        }
Ejemplo n.º 3
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);
                }
            }