Пример #1
0
    internal static void ReplaceUnityInputField(InputField unityInputField)
    {
        if (unityInputField == null)
        {
            return;
        }

        Graphic    _targetGraphic  = unityInputField.targetGraphic;
        ColorBlock _colors         = unityInputField.colors;
        Color      _selectionColor = unityInputField.selectionColor;

        TMP_InputField.ContentType _contentType = (TMP_InputField.ContentType)unityInputField.contentType;
        TMP_InputField.LineType    _lineType    = (TMP_InputField.LineType)unityInputField.lineType;
        int _characterLimit = unityInputField.characterLimit;


        GameObject obj = unityInputField.gameObject;

        Undo.DestroyObjectImmediate(unityInputField);

        TMP_InputField tmproInputField = Undo.AddComponent <TMP_InputField>(obj);

        tmproInputField.targetGraphic  = _targetGraphic;
        tmproInputField.colors         = _colors;
        tmproInputField.selectionColor = _selectionColor;
        tmproInputField.contentType    = _contentType;
        tmproInputField.lineType       = _lineType;
        tmproInputField.characterLimit = _characterLimit;

        tmproInputField.targetGraphic.GetComponent <Image>();
    }
Пример #2
0
        private void TMPInputFromInput(InputHandler handler)
        {
            if (handler == null)
            {
                return;
            }

            InputField input = handler.GetComponent <InputField>();

            if (input == null)
            {
                return;
            }

            int limit = input.characterLimit;

            TMP_InputField.ContentType content = GetTMPContentType(input.contentType);
            TMP_InputField.LineType    line    = TMPProUtil.LineType(input.lineType);
            float      caretBlinkRate          = input.caretBlinkRate;
            int        caretWidth     = input.caretWidth;
            Color      selectionColor = input.selectionColor;
            GameObject obj            = input.gameObject;

            RectTransform viewport      = handler.GetComponentInChildren <RectMask2D>().rectTransform;
            CWTextMeshPro placholder    = handler.GetComponentsInChildren <CWTextMeshPro>()[0];
            CWTextMeshPro textComponent = handler.GetComponentsInChildren <CWTextMeshPro>()[1];

            if (viewport == null || placholder == null || textComponent == null)
            {
                return;
            }

            MonoBehaviour.DestroyImmediate(input);

            CWTextMeshProInput tmp = obj.AddComponent <CWTextMeshProInput>();

            tmp.textViewport  = viewport;
            tmp.placeholder   = placholder;
            tmp.textComponent = textComponent;

            tmp.characterLimit = limit;
            tmp.contentType    = content;
            tmp.lineType       = line;
            tmp.caretBlinkRate = caretBlinkRate;
            tmp.caretWidth     = caretWidth;
            tmp.selectionColor = selectionColor;

            tmp.readOnly = false;
            tmp.shouldHideMobileInput = false;

            tmp.fontAsset = UISkinManager.TMPFont;
        }
Пример #3
0
 public UIInputField LineType(TMP_InputField.LineType type)
 {
     inputField.lineType = type;
     return(this);
 }
        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);
        }