- Home /
Make a UI text box with selectable text?
I'm trying to make a simple chat system for our games lobby, however I am unable to figure out how to make text in the text box selectable. So that users can highlight it and copy it if they want, like you should be able to with normal text. I'm using the Unity UI for this.
Any suggestions?
Give a bit more detail. What are you using? Are you using Unity GUI or some other 3rd party tool like NGUI?
It would be also beneficial to know what platform are you a$$anonymous$$g for.
I'm using the Unity UI. Not NGUI, not OnGUI.
I'm ai$$anonymous$$g for Windows/$$anonymous$$ac/Linux platforms
Answer by Jott · Feb 15, 2016 at 05:08 AM
Not exactly a timely response but nevertheless,
I've come across the same problem, and put together this little script. Just attach to a gameobject with an inputfield and set the inputfields text to whatever it is you want to be selectable but not editable. Currently doesn't have the ability to change through code, but that would be simple enough to implement I think.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(InputField))]
public class SelectableText : MonoBehaviour {
string text;
InputField field;
// Use this for initialization
void Start () {
field = GetComponent<InputField>();
text = field.text;
field.onValueChanged.AddListener(s => field.text = text);
}
}
Basically you just reset the input field to your initial value whenever the user tries to change it.
Answer by TheRichardGamer · May 24, 2016 at 03:45 PM
Well, there's actually an option on the InputField that says "Read Only" which allows the user to select the field and copy its contents but it does not allow the user to edit the field.
Answer by OctoMan · May 08, 2015 at 04:00 PM
Just use the InputField in the new UI System. It has everything you need. If you need to code something inside dont forget to use the namespace UnityEngine.UI
I was fiddling with the input field, how do I make it so the text is non-editable? I want to be able to select and highlight it, but not edit it in any way.
inputfield.enabled = false;
you may want to set it depending on inputtypes to what ever you need. Like if there is mouseinput set it to enabled. If there is keyboard input set it to false. $$anonymous$$ight work, never tested!
Sadly disabling the input field also disables the ability to highlight, and I'm unable to selectively disable specific input types for the input field only (unless someone knows how).
http://docs.unity3d.com/460/Documentation/ScriptReference/UI.InputField.html
It has alot possibilitys, like select and so on. you might want to take a closer look. When i have time i'll look deeper into it.
Trust me, I've been pouring over it since 8 in the morning (nearly 5 hours ago) trying to figure it out. Pretty sure I have almost every unity answer question and forum post question for the input field open at this point.
I'll keep searching, thanks.
Your answer