Exemple #1
0
        public void Initialize(string valueTypeName, TMP_InputField.CharacterValidation characterValidation)
        {
            SecondFieldLabelText.text      = $"New {valueTypeName}";
            ValueField.characterValidation = characterValidation;

            CancelButton.onClick.AddListener(() => gameObject.SetActive(false));
        }
        private TMP_InputField UpgradeInputField(InputField inputField)
        {
            if (!inputField)
            {
                return(null);
            }

            GameObject go = inputField.gameObject;

            stringBuilder.AppendLine("Upgrading InputField: " + GetPathOfObject(go.transform));

            // Copy fields
            Vector2 sizeDelta = ((RectTransform)inputField.transform).sizeDelta;
            SelectableObjectProperties selectableProperties = new SelectableObjectProperties(inputField);

            char  asteriskChar     = inputField.asteriskChar;
            float caretBlinkRate   = inputField.caretBlinkRate;
            bool  customCaretColor = inputField.customCaretColor;
            Color?caretColor       = null;

            try { caretColor = inputField.caretColor; } catch { }
            float caretWidth     = inputField.caretWidth;
            int   characterLimit = inputField.characterLimit;

            TMP_InputField.CharacterValidation characterValidation = GetTMPCharacterValidation(inputField.characterValidation);
            TMP_InputField.ContentType         contentType         = GetTMPContentType(inputField.contentType);
            bool enabled = inputField.enabled;

            TMP_InputField.InputType inputType    = GetTMPInputType(inputField.inputType);
            TouchScreenKeyboardType  keyboardType = inputField.keyboardType;

            TMP_InputField.LineType lineType = GetTMPLineType(inputField.lineType);
            bool   readOnly              = inputField.readOnly;
            Color  selectionColor        = inputField.selectionColor;
            bool   shouldHideMobileInput = inputField.shouldHideMobileInput;
            string _text = inputField.text;

            // Copy UnityEvents
            object onEndEdit = CopyUnityEvent(inputField.onEndEdit);

#if UNITY_5_3_OR_NEWER
            object onValueChanged = CopyUnityEvent(inputField.onValueChanged);
#else
            object onValueChanged = CopyUnityEvent(inputField.onValueChange);
#endif

            // Upgrade&copy child objects
            TextMeshProUGUI textComponent        = UpgradeText(inputField.textComponent);
            Graphic         placeholderComponent = (inputField.placeholder as Text) ? UpgradeText((Text)inputField.placeholder) : inputField.placeholder;

            // Replace InputField with TMP_InputField
            DestroyImmediate(inputField, true);
            TMP_InputField tmp = go.AddComponent <TMP_InputField>();

            // Paste child objects
            tmp.textComponent = textComponent;
            tmp.placeholder   = placeholderComponent;

            // Paste fields
            selectableProperties.ApplyTo(tmp);

            tmp.asteriskChar     = asteriskChar;
            tmp.caretBlinkRate   = caretBlinkRate;
            tmp.customCaretColor = customCaretColor;
            try { if (caretColor.HasValue)
                  {
                      tmp.caretColor = caretColor.Value;
                  }
            } catch { }
            tmp.caretWidth          = Mathf.RoundToInt(caretWidth);
            tmp.characterLimit      = characterLimit;
            tmp.characterValidation = characterValidation;
            tmp.contentType         = contentType;
            tmp.enabled             = enabled;
            tmp.inputType           = inputType;
            tmp.keyboardType        = keyboardType;
            if (tmp.lineType == lineType)
            {
                tmp.lineType = (TMP_InputField.LineType)(((int)lineType + 1) % 3);                                              // lineType adjusts Text's overflow settings but only if the lineType value is different
            }
            tmp.lineType = lineType;
            if (textComponent)
            {
                textComponent.overflowMode = TextOverflowModes.Overflow;                             // lineType doesn't modify this value, though. If must be set to Overflow for TMP_InputField texts
            }
            tmp.readOnly              = readOnly;
            tmp.selectionColor        = selectionColor;
            tmp.shouldHideMobileInput = shouldHideMobileInput;
            tmp.text = _text;

            ((RectTransform)tmp.transform).sizeDelta = sizeDelta;

            // Paste UnityEvents
            PasteUnityEvent(tmp.onEndEdit, onEndEdit);
            PasteUnityEvent(tmp.onValueChanged, onValueChanged);

            // TMP InputField objects have an extra Viewport (Text Area) child object, create it if necessary
            if (textComponent)
            {
                RectTransform viewport;
                if (textComponent.transform.parent != tmp.transform)
                {
                    viewport = (RectTransform)textComponent.transform.parent;
                }
                else
                {
                    viewport = CreateInputFieldViewport(tmp, textComponent, placeholderComponent);
                }

                if (!viewport.GetComponent <RectMask2D>())
                {
                    viewport.gameObject.AddComponent <RectMask2D>();
                }

                tmp.textViewport = viewport;
            }

            return(tmp);
        }