Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by Preavis · Jun 28, 2012 at 11:11 AM · textareatab

Find cursor position in a TextArea

Hi guys Im having some troubles adding the ability to use tab spacing in my Text Area.

Im trying to catch the tab event and then use string.Insert() to add the tab into the text, this works as intended except that to use insert as you all probably know you must enter an index value for where the insertion will be.

My question is, How can i find the correct index value for the string in my Text Area based off where the cursor is?

Comment
Add comment · Show 1
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 Preavis · Jul 01, 2012 at 01:15 AM 0
Share

Anyone??? What im looking for is Unity's equivalent of the SelectionStart function in a Windows.Forms textbox, or anyway to insert or split a string based on where the cursor is in the text, does it exist or am i forced to use Windows.Forms as a plugin?

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Statement · Jul 01, 2012 at 02:37 AM

Hi, you can get the TextEditor to get the information you need. Unfortunately this documentation has fallen behind a bit, but check out this example:

 using UnityEngine;
 using System.Collections;
 
 public class GUITextAreaEditorExample : MonoBehaviour
 {
     public string txt = "";
     void OnGUI()
     {
         txt = GUI.TextArea(new Rect(8, 8, 200, 200), txt);
         TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
         
         GUI.Label(new Rect(216, 8, 200, 200), string.Format("Selected text: {0}\nPos: {1}\nSelect pos: {2}",
             editor.SelectedText,
             editor.pos,
             editor.selectPos));
 
         if (GUI.Button(new Rect(8, 216, 400, 20), "Insert Tab"))
             txt = txt.Insert(editor.pos, "\t");
     }
 }
Comment
Add comment · Show 4 · 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 Preavis · Jul 01, 2012 at 03:09 AM 0
Share

Thank you so much, I had been playing around with different methods for hours.

This works perfectly and has helped me do so much more then I originally intended.

Just made my day :D

avatar image MountDoomTeam · May 10, 2013 at 08:13 PM 0
Share

is it possible to write it in JS?

avatar image Statement · May 19, 2013 at 02:01 PM 0
Share

@ZoomDomain; yes.

avatar image Jean-Fabre · Dec 12, 2014 at 01:11 PM 0
Share

Warning: using RichText content will make the .pos very messy, most of the time outputing wrong values, especially when content is wrapped or longer than what seems to be a width limit inside Unity GUI system

avatar image
0

Answer by themarkval · May 07, 2014 at 12:19 PM

This is how I do it :

 private int carretPosCode = 0;
     private bool tabCode = false;
     
     TextEditor editor;
 
 
     GUI.SetNextControlName("CodeArea");
     textAreaCode = GUI.TextArea(new Rect(0,0,500,300),textAreaCode);
     
     editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
         
         if(Event.current.type == EventType.keyDown ){
                                 textAreaCode = textAreaCode.Insert(editor.selectPos, "\t");
                                 carretPosCode = editor.selectPos;
         
         
                             }
     
         
         if ( Event.current.keyCode == KeyCode.Tab && Event.current.type == EventType.keyUp  ) {    
                                 GUI.FocusControl("CodeArea");
                                 Event.current.Use();
                                 tabCode = true;
                             }
         if(tabCode && Event.current.type == EventType.Layout){
                                 editor.pos = carretPosCode+2;
                                 editor.selectPos = carretPosCode+2;
                                 tabCode = false;
                             }
 
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
avatar image
0

Answer by john-essy · Feb 06, 2019 at 03:43 PM

How does it know which text to pay attention to? I am busy trying to use this for an editor script so that my client can decide which text to make bold, But for some reason it only works when i have this at the top of my OnGUI method. When i try to use this with Data i.e in a custom class like Name, Description etc it always says the selected text is empty?

Is this global or ?

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

8 People are following this question.

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

Related Questions

Location of a char in a String 1 Answer

Read text from http 2 Answers

How to validate a UI text that has been dragged to another UI text under different panel ?? 0 Answers

Name for list item selection/movement bar? 0 Answers

Is it possible to rename tabs in editor? 2 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