Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Is it possible to retrieve InputField selected text

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by AnonDreamer, Feb 2, 2015.

  1. AnonDreamer

    AnonDreamer

    Joined:
    Oct 28, 2014
    Posts:
    30
    Hi there,

    I've been looking through the documentation but didn't find a way to do what i want to :s.
    I'm trying to retrieve the selected text in an inputfield, like when you select some part of a text using the mouse.

    The idea is, i want to create a "bold / italic / color button", then apply / delete the style of the selected text.
    Are there some type of events like "OnSelectText" ?

    Also, is there a way to override the behaviour of the inputfield, i'd want to keep a text selected unless i click on the inputfield area. (so i can modify the selected text part via the bold / color / italic buttons)

    Sorry for my poor english and thanks for reading :)
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,634
    Sadly no, I don't think so.
    Unity does keep a record of what text is highlighted on the screen in the InputField and as far as I know, it is the platform that does the selection, not unity. So not sure if that would be even possible.
     
  3. AnonDreamer

    AnonDreamer

    Joined:
    Oct 28, 2014
    Posts:
    30
    Ok thanks :s

    Any idea if they plan on implementing it soon like with the 5.0 version ?
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,146
    we do expose the "selection" for when we control the highlighting (selection text on mobile keyboard is not supported). You need to look at the caretPostition and caretSelectPos on the input field.

    These will give you the position in the .value string of what's being highlighted.

    Note these are not guaranteed to be in any order (caretPos could be a higher or lower value than caretSelectPos).
     
  5. AnonDreamer

    AnonDreamer

    Joined:
    Oct 28, 2014
    Posts:
    30
    Thank you very much, i'll check this out ^^

    edit: that's weird, i get an error of accessibility:
    "error CS0122: `UnityEngine.UI.InputField.caretSelectPos' is inaccessible due to its protection level"
    With the following code:
    Code (CSharp):
    1. public InputField inputfield;
    2. public void pointerUp()
    3. {
    4.         Debug.Log("POINTER UP ===========================================");
    5.         Debug.Log("inputfield.text.Length: " + inputfield.text.Length);
    6.  
    7.         Debug.Log("Caret position: " + inputfield.caretSelectPos);
    8. }
    I did some research and indeed these 2 properties are protected.

    I'm currently using Unity version 4.6.1f1
     
    Last edited: Feb 4, 2015
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,146
    ugh yes you are right :( my brain read that as public yesterday when i mentioned it to you.. :( I'll make the getter public for you :)
     
    ina, BMayne and mdrotar like this.
  7. AnonDreamer

    AnonDreamer

    Joined:
    Oct 28, 2014
    Posts:
    30
    nice :) thanks again !
     
  8. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,634
    Ask (@phil-Unity ) and thou shall receive :D. Best to also log it as a request/issue on the UI repo, for clarity
     
  9. Rational-MA

    Rational-MA

    Joined:
    Mar 6, 2015
    Posts:
    59
    Thanks for exposing this. Any idea when it will be reflected in Unity 5? I don't see these members exposed yet with the official 5.0.0 release.
     
  10. shoffing

    shoffing

    Joined:
    Oct 31, 2013
    Posts:
    8
    Version 5.0.1f1, and these are still protected members. Is there any way to do this? I'm trying to implement an auto-indent feature for a multiline input, and it's not possible because there's no way to set the position of the input caret.
     
  11. shoffing

    shoffing

    Joined:
    Oct 31, 2013
    Posts:
    8
    Just figured out a really hacky way to set the caret position using MoveTextEnd():

    Code (CSharp):
    1. // Moves an input field's caret to the given position.
    2. void SetCaretPosition(InputField inputField, int caretPos) {
    3.     string tempText = inputField.text;
    4.     inputField.text = inputField.text.Substring(0, caretPos);
    5.     inputField.MoveTextEnd(false);
    6.     inputField.text = tempText;
    7. }
    It's obviously not as good as an official solution, but for now it works and I was able to do my auto-indentation with it: http://gfycat.com/MeatyDirtyCopperbutterfly
     
  12. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,212
    This is still a protected property in 5.0.2.
     
  13. KingRecycle

    KingRecycle

    Joined:
    Jul 20, 2013
    Posts:
    18
    Though the bitbucket says they are public now it still hasn't been pushed to release. :(
     
  14. Nerevar

    Nerevar

    Joined:
    Mar 15, 2013
    Posts:
    11
    Yep still can't access the carret position in unity 4.6, I would really need that feature :(
     
  15. multix

    multix

    Joined:
    Aug 5, 2013
    Posts:
    16
  16. MaLlorente

    MaLlorente

    Joined:
    May 27, 2013
    Posts:
    5
  17. joshskelton

    joshskelton

    Joined:
    Aug 30, 2013
    Posts:
    16
    This is still private. Can it be made public?

    "private string GetSelectedString()"