1
\$\begingroup\$

I'm trying to make a simple chat system for our games lobby, however I am having issues making a text box that allows for text-highlighting. The Text component does not have interactible text, and the Inputfield allows for interaction, but I don't want the text to be editable in any way.

How would I go about doing this?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I haven't tested it, but I would likely implement something where any attempts to change the text simply don't change the text. For example, there's an onValueChange event for the InputField. Attaching to this event and simply setting the text to its initial value will revert any changes made by the user.

This simple script achieves the desired effect:

public class InputFieldLockdown : MonoBehaviour {
    InputField textObject;
    string myText = "This is the text";

    void Start () {
        //Get the inputfield script and set its text value to the initial value
        textObject = this.GetComponentInChildren<InputField>();
        textObject.text = myText;
    }

    public void ValueChanged()
    {
        //When|if the value changes, reset it to its initial value
        textObject.text = myText;
    }
}

Attach this script to your InputField object. Next you'll need to associate the event in the Unity editor:

enter image description here

The only downside to this is pressing the backspace key, still causes the input caret to move backwards. This is a fairly minor downside though.

\$\endgroup\$
5
  • \$\begingroup\$ DeactivateInputField sadly does not allow for highlighting. I have been unable to figure out how to get onValueChange to return the actual value that was changed.. I can get it to call my methods, however I cannot get it to pass any data of what was changed. \$\endgroup\$ May 8, 2015 at 18:50
  • 1
    \$\begingroup\$ I'll see if I can get a working example soon. \$\endgroup\$
    – House
    May 8, 2015 at 22:49
  • \$\begingroup\$ Sure thing, don't bust yourself over it though :) \$\endgroup\$ May 8, 2015 at 22:51
  • \$\begingroup\$ All updated with working code. \$\endgroup\$
    – House
    May 9, 2015 at 1:52
  • \$\begingroup\$ Sweet, I used it and it works. Made some modifications and actually extended the InputField class. I'm a bit disappointed that Unity did not allow methods such as KeyPressed(Event evt) and properties like caretPosition to be overridden. \$\endgroup\$ May 9, 2015 at 2:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .