[Unity3d] ReadOnly InputField

By | May 15, 2015

Advertisement from Google

unity3dNew UI system was introduced in Unity3d 4.6, it includes InputField control, but this field could not be read only. You can make it non interactable by disabling “Interactable” property in the editor, but then you will not be able to select and copy text from InputField.

To have proper ReadOnly input field you should do small modification to the original control:


Advertisement from Google

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[AddComponentMenu("UI/Read Only Input Field", 32)]
public class ReadOnlyInputField : InputField
{
    private Event m_ProcessingEvent = new Event();

    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (!isFocused)
            return;

        bool consumedEvent = false;
        while (Event.PopEvent(m_ProcessingEvent))
        {
            if (m_ProcessingEvent.rawType == EventType.KeyDown)
            {
                if (!IsAllowedCombination(m_ProcessingEvent))
                {
                    continue;
                }

                consumedEvent = true;

                var shouldContinue = KeyPressed(m_ProcessingEvent);
                if (shouldContinue == EditState.Finish)
                {
                    DeactivateInputField();
                    break;
                }
            }
        }

        if (consumedEvent)
        {
            UpdateLabel();
        }

        eventData.Use();
    }

    private bool IsAllowedCombination(Event evt)
    {
        var currentEventModifiers = evt.modifiers;
        RuntimePlatform rp = Application.platform;
        bool isMac = (rp == RuntimePlatform.OSXEditor || rp == RuntimePlatform.OSXPlayer || rp == RuntimePlatform.OSXWebPlayer);
        bool ctrl = isMac ? (currentEventModifiers & EventModifiers.Command) != 0 : (currentEventModifiers & EventModifiers.Control) != 0;

        switch (evt.keyCode)
        {
            case KeyCode.Home:
            case KeyCode.End:
            case KeyCode.LeftControl:
            case KeyCode.RightControl:
            {
                return true;
            }

            // Select All
            case KeyCode.A:
            {
                if (ctrl)
                {
                    return true;
                }
                break;
            }

            // Copy
            case KeyCode.C:
            {
                if (ctrl)
                {
                    return true;
                }
                break;
            }

            case KeyCode.LeftArrow:
            case KeyCode.RightArrow:
            case KeyCode.UpArrow:
            case KeyCode.DownArrow:
            {
                return true;
            }
        }

        return false;
    }
}

Now you can add this component to your control or replace InputField component with this one on existing game object.

You can buy Pro version of this asset in Unity Asset Store

One thought on “[Unity3d] ReadOnly InputField

Leave a Reply

Your email address will not be published. Required fields are marked *