Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Input Field selection (caret position)

Discussion in 'UGUI & TextMesh Pro' started by centralpixel, Apr 8, 2015.

  1. centralpixel

    centralpixel

    Joined:
    Apr 8, 2015
    Posts:
    7
    Is there any possibility to get current selected text inside Input Field or current caret position?
    Something like selectionBegin, selectionEnd.
    I need it to change text value from on-screen buttons (keyboard)
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    I don't think so, but you can check through the InputField source for how it manages the Caret position.
     
  3. Nerevar

    Nerevar

    Joined:
    Mar 15, 2013
    Posts:
    11
    And where do you get that source code?
     
  4. Macrum

    Macrum

    Joined:
    Jul 12, 2015
    Posts:
    2
    Here is the link to the scripting API for "InputField":

    http://docs.unity3d.com/ScriptReference/UI.InputField.html

    Specific to your Needs:

    http://docs.unity3d.com/ScriptReference/UI.InputField-caretPosition.html
    http://docs.unity3d.com/ScriptReference/UI.InputField-selectionAnchorPosition.html

    This should have any answers you need for the basic stuff.

    Code (CSharp):
    1.  
    2.                 InputField temp = gameObject.GetComponent<InputField> ();
    3.  
    4.                 temp.text = "1";
    5.        
    6.                 string stuff = temp.text;
    Everything you mentioned wanting to do is explained in the API. The Code sample above is a basic method to 'get' or 'set' the text for a 'InputField'
     
    Last edited: Jul 13, 2015