public void ClosePanel(bool isSubmit)
    {
        if (isSubmit)
        {
            if (_disconnectedGamepad)
            {
                whenJoystickIsReconnected.Invoke();
                _disconnectedGamepad = false;
            }

            _currentManagerState = GamepadManagerState.WaitForDisconnection;

            _currentPanel.gameObject.SetActive(false);
            Invoke("DelayPanelDesactivate", 0.2f);
        }
        else
        {
            _assignedGamepads    = new GamepadInfo[1];
            _assignedGamepads[0] = _mainGamepad;

            _currentManagerState = GamepadManagerState.DoNothing;

            _currentPanel.gameObject.SetActive(false);
            Invoke("DelayPanelDesactivate", 0.2f);
        }
    }
    public void CheckForFourPlayer(bool isObligatory)
    {
        if (isObligatory)
        {
            _assignedGamepads = new GamepadInfo[4];
            for (int i = 0; i < _detectedGamepads.Length; i++)
            {
                _detectedGamepads[i].Selected = false;
            }

            _currentPanel = _panelCheckFor[3];
            _currentPanel.gameObject.SetActive(true);
            panelIsActive = true;

            _eventSystem.SetSelectedGameObject(_currentPanel.gameObject);

            _currentManagerState = GamepadManagerState.CheckForFourJoystick;
        }
        else if (!GamepadAreConfigured(4))
        {
            if (_assignedGamepads == null || _assignedGamepads.Length != 4)
            {
                _assignedGamepads = new GamepadInfo[4];
                for (int i = 0; i < _detectedGamepads.Length; i++)
                {
                    _detectedGamepads[i].Selected = false;
                }
            }

            _currentPanel = _panelCheckFor[3];
            _currentPanel.gameObject.SetActive(true);
            panelIsActive = true;

            _eventSystem.SetSelectedGameObject(_currentPanel.gameObject);

            _currentManagerState = GamepadManagerState.CheckForFourJoystick;
        }
        else
        {
            _currentManagerState = GamepadManagerState.WaitForDisconnection;
        }
    }
 public void DoNothing()
 {
     _currentManagerState = GamepadManagerState.DoNothing;
 }
    public void WaitForFirstJoystick()
    {
        _assignedGamepads = new GamepadInfo[1];

        _currentManagerState = GamepadManagerState.WaitForFirstJoystick;
    }
    private void UpdateManagerState()
    {
        switch (_currentManagerState)
        {
        case GamepadManagerState.WaitForFirstJoystick:
            UpdateJoystickState();

            for (int i = 0; i < _detectedGamepads.Length; i++)
            {
                if (_detectedGamepads[i].Type != Gamepad.None && _detectedGamepads[i].Type != Gamepad.Unknown)
                {
                    bool pressL = GamepadInput.GetButton("Fire5", _detectedGamepads[i].Type, i + 1);
                    bool pressR = GamepadInput.GetButton("Fire6", _detectedGamepads[i].Type, i + 1);

                    string submit = GamepadInput.GetButtonForUI("Submit", _detectedGamepads[i].Type, i + 1);

                    if ((pressL && pressR) || Input.GetButtonDown(submit))
                    {
                        int emptySlot = GetNextJoystickEmptySlot();

                        if (emptySlot != -1)
                        {
                            _mainGamepad.Player = _assignedGamepads[emptySlot].Player = Player.One;
                            _mainGamepad.Id     = _assignedGamepads[emptySlot].Id = i + 1;
                            _mainGamepad.Type   = _assignedGamepads[emptySlot].Type = _detectedGamepads[i].Type;

                            _detectedGamepads[i].Selected = true;

                            SetStandaloneInputModule(_mainGamepad);

                            //Debug.Log("Joystick #" + _assignedGamepeds[0].Id);

                            whenWaitForFirstJoystickSuccess.Invoke();

                            _currentManagerState = GamepadManagerState.WaitForDisconnection;
                        }
                    }
                }
            }
            break;

        case GamepadManagerState.CheckForOneJoystick:
        case GamepadManagerState.CheckForTwoJoystick:
        case GamepadManagerState.CheckForThreeJoystick:
        case GamepadManagerState.CheckForFourJoystick:
            UpdateJoystickState();

            if (!GamepadAreConfigured((int)_currentManagerState - 1))
            {
                _currentPanel.SetOnSubmitActive(false);

                for (int i = 0; i < _detectedGamepads.Length; i++)
                {
                    if (_detectedGamepads[i].Type != Gamepad.None && _detectedGamepads[i].Type != Gamepad.Unknown && !_detectedGamepads[i].Selected)
                    {
                        bool pressL = GamepadInput.GetButton("Fire5", _detectedGamepads[i].Type, i + 1);
                        bool pressR = GamepadInput.GetButton("Fire6", _detectedGamepads[i].Type, i + 1);

                        if (pressL && pressR)
                        {
                            int emptySlotIndex = GetNextJoystickEmptySlot();

                            if (emptySlotIndex != -1)
                            {
                                _assignedGamepads[emptySlotIndex].Player = (Player)(emptySlotIndex + 1);
                                _assignedGamepads[emptySlotIndex].Id     = i + 1;
                                _assignedGamepads[emptySlotIndex].Type   = _detectedGamepads[i].Type;

                                _detectedGamepads[i].Selected = true;

                                if (emptySlotIndex == 0)
                                {
                                    _mainGamepad = _assignedGamepads[emptySlotIndex];
                                    SetStandaloneInputModule(_mainGamepad);
                                }

                                //Debug.Log("Joystick #" + _assignedGamepeds[emptySlotIndex].Id);

                                _currentPanel.AddJoystickSlot(emptySlotIndex, _assignedGamepads[emptySlotIndex]);
                            }
                        }
                    }
                }
            }
            else
            {
                _currentPanel.SetOnSubmitActive(true);
            }
            break;

        case GamepadManagerState.WaitForDisconnection:
            UpdateJoystickState();
            //TODO
            break;
        }
    }