예제 #1
0
    void Update()
    {
        if (_currentGamepads != null)
        {
            for (int i = 0; i < _currentGamepads.Length; i++)
            {
                float h = GamepadInput.GetAxis("Horizontal", _currentGamepads[i].Type, _currentGamepads[i].Id);
                float v = GamepadInput.GetAxis("Vertical", _currentGamepads[i].Type, _currentGamepads[i].Id);

                playerPanels[i].h.text  = "H = " + h.ToString("0.00");
                playerPanels[i].h.color = (h == 0f ? Color.black : Color.red);

                playerPanels[i].v.text  = "V = " + v.ToString("0.00");
                playerPanels[i].v.color = (v == 0f ? Color.black : Color.red);

                bool f1 = GamepadInput.GetButton("Fire1", _currentGamepads[i].Type, _currentGamepads[i].Id);
                bool f2 = GamepadInput.GetButton("Fire2", _currentGamepads[i].Type, _currentGamepads[i].Id);
                bool f3 = GamepadInput.GetButton("Fire3", _currentGamepads[i].Type, _currentGamepads[i].Id);
                bool f4 = GamepadInput.GetButton("Fire4", _currentGamepads[i].Type, _currentGamepads[i].Id);
                bool f5 = GamepadInput.GetButton("Fire5", _currentGamepads[i].Type, _currentGamepads[i].Id);
                bool f6 = GamepadInput.GetButton("Fire6", _currentGamepads[i].Type, _currentGamepads[i].Id);

                playerPanels[i].f1.color = (f1 ? Color.red : Color.black);
                playerPanels[i].f2.color = (f2 ? Color.red : Color.black);
                playerPanels[i].f3.color = (f3 ? Color.red : Color.black);
                playerPanels[i].f4.color = (f4 ? Color.red : Color.black);
                playerPanels[i].f5.color = (f5 ? Color.red : Color.black);
                playerPanels[i].f6.color = (f6 ? Color.red : Color.black);

                if (_currentGamepads[i].Player == Player.One)
                {
                    string submit = GamepadInput.GetButtonForUI("Submit", _currentGamepads[i].Type, _currentGamepads[i].Id);
                    string cancel = GamepadInput.GetButtonForUI("Cancel", _currentGamepads[i].Type, _currentGamepads[i].Id);

                    bool sValue = Input.GetButtonDown(submit);
                    bool cValue = Input.GetButtonDown(cancel);

                    playerPanels[i].submit.color = (sValue ? Color.red : Color.black);
                    playerPanels[i].cancel.color = (cValue ? Color.red : Color.black);

                    bool start = GamepadInput.GetButton("Start", _currentGamepads[i].Type, _currentGamepads[i].Id);

                    if (f5 && f6 && start)
                    {
                        gameObject.SetActive(false);
                        prevScreen.SetActive(true);

                        if (_prevButton != null)
                        {
                            EventSystem.current.SetSelectedGameObject(_prevButton);
                        }
                    }
                }
            }
        }
    }
    private void SetStandaloneInputModule(GamepadInfo gi)
    {
        string h = GamepadInput.GetAxisName("Horizontal", gi.Type, gi.Id);

        _standaloneInputModule.horizontalAxis = h.Equals("") ? "Joystick" + gi.Id + "Axis1" : h;
        string v = GamepadInput.GetAxisName("Vertical", gi.Type, gi.Id);

        _standaloneInputModule.verticalAxis = v.Equals("") ? "Joystick" + gi.Id + "Axis2" : v;
        _standaloneInputModule.submitButton = GamepadInput.GetButtonForUI("Submit", gi.Type, gi.Id);
        _standaloneInputModule.cancelButton = GamepadInput.GetButtonForUI("Cancel", gi.Type, gi.Id);
    }
    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;
        }
    }