Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Cerbion_ · Sep 09, 2015 at 01:02 PM · c#selectiontextfieldstringsreplace

Replacing a selected part of string in a GUI.Textfield

Hey there folks,

my problem is quite "simple" (famous last words of every coder), yet I struggle to find any useful documentation/examples in the interwebs.

So basically I have this text editor, and it's supposed to replace a selected part of a string in a TextField/Area into another string, example:

it's supposed to turn "Hello my name is Ed" into "Hello my name is <ED>".

Now I can already get which part of the entered string the user has selected with the TextEditor:

 TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
 string selectedText = editor.SelectedText;

So the part of the text is selected and now need to be replaced, and here's where I have no clue to go about it, because the TextEditor, does have a "ReplaceSelection" method, but it doesn't seem to work the way I want it (it just changes whats exactly selected). I would appreciate any help/hints towards solving this matter! :)

P.S: Just in case it's not clear, I don't want to replace the finished string, that would be fairly easy, but the string that is still being written/editted by the user in the Textfield/Area (imagine a WYSIWYG editor-like behavior).

Comment
Add comment
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

1 Reply

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

Answer by Scribe · Sep 09, 2015 at 01:33 PM

 public string txt = "";
 
 void OnGUI(){
     txt = GUI.TextArea(new UnityEngine.Rect(8, 8, 200, 200), txt);
     TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
 
     GUI.Label(new UnityEngine.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 UnityEngine.Rect(8, 216, 400, 20), "Replace")){
         txt = txt.Remove(Mathf.Min(editor.pos, editor.selectPos), editor.SelectedText.Length);
         string replaceString = "hahaha";
         txt = txt.Insert(Mathf.Min(editor.pos, editor.selectPos), replaceString);
 
         editor.pos = Mathf.Min(editor.pos, editor.selectPos);
         editor.selectPos = editor.pos + replaceString.Length;
     }
 }

This link helped

Comment
Add comment · Show 2 · 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 Cerbion_ · Sep 09, 2015 at 05:11 PM 1
Share

Ha! Not even saying a word, are we? Doesn't matter, it does precisely what I wanted, thanks a bunch! :)

avatar image Scribe Cerbion_ · Sep 11, 2015 at 09:24 AM 0
Share

Apologies, my post was a little rushed, Selected text is useful to know what you are replacing, though as there could be several places where a piece of text is present, it might not be correct to simply find the first instance of the text.

editor.pos is where the cursor was first clicked, editor.selectedPos was where the cursor was dragged to, hence the $$anonymous$$imum of the two is the first position of the selected text and the $$anonymous$$imum plus the length of the selected text is the end! you could also use the maximum of the two to find the end!

Remove with two integer arguments x and y, removes the text from the string between the positions x and y! Insert... well.. it inserts stuff at the position specified!

It is not very visible due to the new site layout, but I have linked to another question at the bottom of my post, for where I got the information from!

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Specifying custom Bounds for a component, for use with the '(F)rame' shortcut 0 Answers

Input not updating on user input 1 Answer

Plane character selector unity problem 0 Answers

I need help finding the index of an object in a list. 1 Answer

If instantiate prefab is selected how can i change the color? 1 Answer


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