Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Glurth · Feb 19, 2015 at 06:07 PM · inputfield

Draw your own InputField, cursor/selection location

I would like to draw my own InputField control, using 3d objects.

I’ve got the mouse-input working, so I can click on parts of my text to highlight it, and move the cursor around.

Rather than re-invent how the Input field handles keystrokes, given the cursor position, and highlighted text, I would like to use the functionality that is already built into the InputField.

I started by simply hiding the input field, and sending it the keystrokes. Then lookup the resultant text, and display that. This works fine, but has limitations I can’t seem to get around.

The problem I’m hitting is drawing the cursor. Where in the InputField is (Waldo.. no..) the cursor? The issue goes in the other direction too, if the user clicks with the mouse, how do I place the cursor at the given character position in the (hidden) InputField, so the next keystoke will go in the right place.

Unable to find the cursor information, I TRIED to create by own input control code. This worked fairly-well, but has a ton of minutiae still left to handle/implement. Then I tried to handle marked-up text (RichText): which quickly became a nightmare of additional minutiae to handle. Surely there is something out there I can use, rather than having to re-invent the wheel.

I’m not MARRIED to using unity input field’s functionality, but whatever code I use should be able to handle the unity Rich text markup.

(p.s. I realize editing markedup-text fields can yield some odd behavior, but I’m ok with that.)

Research: I DID find this answer: http://answers.unity3d.com/questions/275973/find-cursor-position-in-a-textarea.html

But it doesn't seem to work with an inputfield, I guess it’s only for the old GUI? Maybe I’m doing it wrong? I added a script with this function to an inputfield.

 void OnGUI () {
         TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
         Debug.Log(editor.selectPos.ToString());  //output is ALWAYS zero
     }

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Mar 04, 2015 at 04:23 PM 0
Share

no responses, guess I'll be reinventing the wheel.

avatar image Mmmpies · Mar 04, 2015 at 04:40 PM 0
Share

How are you wanting it to work? If all you want is a custom image at the end of the text input that easily "fudgable" but if you want to move the cursor to also appear between text it'd not work with the method I'm thinking of.

PS also not getting mail notifications.

avatar image Glurth · Mar 04, 2015 at 05:03 PM 0
Share

I'd like to be able to draw the cursor over any selected text: be it a single character, or multiple characters. If no characters are selected, it WOULD indeed be drawn between characters (or at the beginning or end of the string), based on the cursor position.

Again, I have the cursor drawing part down, I just need that cursor/selection position/range.

Thanks for heads up about the notifications! :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by banniandoko · Nov 17, 2016 at 02:23 PM

Maybe you could try this code, I used this code to select/ highlight text in the content/ textField

  GUI.FocusControl("MyTextField");                 
                     editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
                     //editor.MoveUp();
                     editor.OnFocus();
                     editor.cursorIndex = 25; // cursor start position
                     editor.selectIndex = 56; // cursor end select position






Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Nov 17, 2016 at 04:22 PM 0
Share

Any idea how to make that work on an Scene's GameObject's InputField? I can make that work on fields I create in code like

    GUI.SetNextControlName("$$anonymous$$yTextField");
    username = GUI.TextField(new Rect(10, 10, 100, 20), username);

But I'm not sure how to use GUI.FocusControl to focus on a GameObject's Component (be it an InputField, or Text).

avatar image banniandoko Glurth · Nov 18, 2016 at 02:32 AM 0
Share

I'm using this script to focus to "inputfield"

 void Update()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab))
         {
             Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
 
             if (next != null)
             {
 
                 InputField inputfield = next.GetComponent<InputField>();
                 if (inputfield != null)
                     inputfield.OnPointerClick(new PointerEventData(system));  //if it's an input field, also set the text caret
 
                 system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
             }
             //else Debug.Log("next nagivation element not found");
 
         }
     }



avatar image Glurth banniandoko · Nov 18, 2016 at 04:40 AM 0
Share

I used the UnityEngine.EventSystem.SetSelectedGameObject function as you suggested. Alas, the results of this debug are always "0,0"- did I miss somthing? (where objectToFocusOn is the InputField itself- also tried the child with the text component- same result)

 eventSystem.SetSelectedGameObject(objectToFocusOn, new BaseEventData(eventSystem));
 
  TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
  editor.OnFocus();
  Debug.Log("This cursur pos: " + editor.cursorIndex + "," + editor.selectIndex);


Show more comments
avatar image
0

Answer by Glurth · Nov 18, 2016 at 06:05 PM

Been so long since I've looked at this: looks like selectionFocusPosition and selectionAnchorPosition are working ok for this. Guess they are new? https://docs.unity3d.com/ScriptReference/UI.InputField-selectionAnchorPosition.html

I'll do more tests and post back,

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

uGUI inputfield text truncates? 2 Answers

Saving only Integers in an inputfield via code 1 Answer

Problem with UI input field in android 1 Answer

How to disable TouchScreenKeyboard for InputField? 0 Answers

Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges