namespace UIWidgets { using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; /// /// InputField adapter to work with both Unity text and TMPro text. /// [AddComponentMenu("UI/New UI Widgets/Adapters/Input Field Adapter")] public class InputFieldAdapter : MonoBehaviour, IInputFieldProxy { IInputFieldProxy proxy; /// /// Proxy. /// protected IInputFieldProxy Proxy { get { if (proxy == null) { proxy = GetProxy(); } return proxy; } } /// /// Proxy gameobject. /// public GameObject GameObject { get { return Proxy.gameObject; } } /// /// Proxy value. /// public string Value { get { return Proxy.text; } set { Proxy.text = (value == null) ? string.Empty : value; } } /// /// Proxy value. /// Alias for Value. /// public string text { get { return Proxy.text; } set { Proxy.text = value; } } /// /// The Unity Event to call when edit. /// /// The OnValueChange. public UnityEvent onValueChanged { get { return Proxy.onValueChanged; } } /// /// The Unity Event to call when editing has ended. /// /// The OnEndEdit. public UnityEvent onEndEdit { get { return Proxy.onEndEdit; } } /// /// Current InputField caret position (also selection tail). /// /// The caret position. public int caretPosition { get { return Proxy.caretPosition; } set { Proxy.caretPosition = value; } } /// /// Is the InputField eligible for interaction (excludes canvas groups). /// /// true if interactable; otherwise, false. public bool interactable { get { return Proxy.interactable; } set { Proxy.interactable = value; } } /// /// Text component. /// public Graphic textComponent { get { return Proxy.textComponent; } set { Proxy.textComponent = value; } } /// /// Placeholder. /// public Graphic placeholder { get { return Proxy.placeholder; } set { Proxy.placeholder = value; } } /// /// If the proxy was canceled and will revert back to the original text. /// public bool wasCanceled { get { return Proxy.wasCanceled; } } /// /// Get text proxy. /// /// Proxy instance. protected virtual IInputFieldProxy GetProxy() { var input_unity = GetComponent(); if (input_unity != null) { return new InputFieldProxy(input_unity); } #if UIWIDGETS_TMPRO_SUPPORT && (UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER) var input_tmpro = GetComponent(); if (input_tmpro != null) { return new InputFieldTMProProxy(input_tmpro); } #endif Debug.LogWarning("Not found any InputField component.", this); return new InputFieldNull(); } /// /// Determines whether InputField instance is null. /// /// true if InputField instance is null; otherwise, false. public bool IsNull() { return Proxy.IsNull(); } /// /// Determines whether this lineType is LineType.MultiLineNewline. /// /// true if lineType is LineType.MultiLineNewline; otherwise, false. public bool IsMultiLineNewline() { return Proxy.IsMultiLineNewline(); } /// /// Function to activate the InputField to begin processing Events. /// public void ActivateInputField() { Proxy.ActivateInputField(); } #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 /// /// Move caret to end. /// public void MoveToEnd() { Proxy.MoveToEnd(); } #endif /// /// Set focus to InputField. /// public void Focus() { Proxy.Focus(); } /// /// Set focus to InputField. /// public void Select() { Proxy.Select(); } #if UNITY_EDITOR /// /// Update layout when parameters changed. /// protected virtual void OnValidate() { GetProxy(); } #endif } }