Пример #1
0
        //==============================================================================================
        //                                       Update Functions
        // The following functions are almost identical EXCEPT for the ReadOnlyCollection line.
        // Each function will retrieve control information for the specified player list and create control
        // input buttons for that player. Each player is specified by a playerIndex and the specific player's
        // list can be called with this index 0 (player one) - index 5 (player six).
        //==============================================================================================

        #region Update Active Buttons
        public void UpdateActiveButtons()
        {
            DestroyList();

            float maxNameWidth  = 0;
            float contentHeight = 4;

            //Retrieves and updates the active player's keys
            ReadOnlyCollection <KeyMapping> keys = InputControl.GetActivePlayerKeys();

            foreach (KeyMapping key in keys)
            {
                //========================================================================================
                //                                   Key Text vs Key Buttons
                //Key Text: The labels/text in the first column of the InputManager menu (see Options tab)
                //Key Buttons: The buttons in the second and third column of the Input Manager menu
                //========================================================================================

                //Source: https://github.com/Gris87/InputControl
                #region Key text
                GameObject keyNameText = Instantiate(keyNamePrefab) as GameObject;
                keyNameText.name = key.name;

                RectTransform keyNameTextRectTransform = keyNameText.GetComponent <RectTransform>();

                keyNameTextRectTransform.transform.SetParent(namesTransform);
                keyNameTextRectTransform.anchoredPosition3D = new Vector3(0, 0, 0);
                keyNameTextRectTransform.localScale         = new Vector3(1, 1, 1);

                Text keyText = keyNameText.GetComponentInChildren <Text>();
                keyText.text = key.name;

                float keyNameWidth = keyText.preferredWidth + 8;

                if (keyNameWidth > maxNameWidth)
                {
                    maxNameWidth = keyNameWidth;
                }
                #endregion

                #region Key buttons
                GameObject keyButtons = Instantiate(keyButtonsPrefab) as GameObject;
                keyButtons.name = key.name;

                RectTransform keyButtonsRectTransform = keyButtons.GetComponent <RectTransform>();

                keyButtonsRectTransform.transform.SetParent(keysTransform);
                keyButtonsRectTransform.anchoredPosition3D = new Vector3(0, 0, 0);
                keyButtonsRectTransform.localScale         = new Vector3(1, 1, 1);

                for (int i = 0; i < 2; ++i)
                {
                    KeyButton buttonScript = keyButtons.transform.GetChild(i).GetComponent <KeyButton>();

                    buttonScript.keyMapping = key;
                    buttonScript.keyIndex   = i;

                    buttonScript.UpdateText();
                }
                #endregion
                //=============================================

                contentHeight += 28;
            }

            RectTransform namesRectTransform = namesTransform.GetComponent <RectTransform>();
            RectTransform keysRectTransform  = keysTransform.GetComponent <RectTransform>();
            RectTransform rectTransform      = GetComponent <RectTransform>();

            namesRectTransform.offsetMax = new Vector2(maxNameWidth, 0);
            keysRectTransform.offsetMin  = new Vector2(maxNameWidth, 0);
            rectTransform.sizeDelta      = new Vector2(0, contentHeight);

            GameObject.Find("SettingsMode").GetComponent <SettingsMode>().UpdateButtonStyle();
        }