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 Chimooo · May 22, 2019 at 02:11 PM · positiontextinputfieldinsertrichtextbox

How to properly update caret position when modifying TMP input field with Rich Text ?

I am working on a game where players have to type code to interact with the environment. I wanted to implement syntax highlighting as they are typing. However, when I insert new rich text tags into the input filed, the caret position takes one blink in the wrong position before updating to the correct one. This is really annoying for someone typing since letters will go in the wrong place.

I have tried some of the solutions suggested here: https://forum.unity.com/threads/cant-change-caret-position.431105/

But with no success. Here is my code:

  void ChangeColors(IEnumerable<IToken> original_token_list, string input_text)
     {
         string output = input_text;
         //HashSet<IToken> unique_tokens = new HashSet<IToken>(original_token_list);
         List<string> token_text = new List<string>();
         foreach (IToken t in original_token_list)
         {
             if (keywords.Contains(t.Type))
             {
                 token_text.Add("<color=#F67599>");
                 token_text.Add(t.Text);
                 token_text.Add("</color>");
                 input.GetComponent<TMP_InputField>().caretPosition = caretPos + 1;
             }
             else if (parens.Contains(t.Type))
             {
                 token_text.Add("<color=#9EC651>");
                 token_text.Add(t.Text);
                 token_text.Add("</color>");
                 input.GetComponent<TMP_InputField>().caretPosition = caretPos + 1;
             }
             else if (brackets.Contains(t.Type))
             {
                 token_text.Add("<color=#6FE1CD>");
                 token_text.Add(t.Text);
                 token_text.Add("</color>");
                 input.GetComponent<TMP_InputField>().caretPosition = caretPos + 1;
             }
 
             else if (t.Text != "<EOF>")
             {
                 token_text.Add(t.Text);
             }
         }
         output = "";
         foreach (string s in token_text)
         {
             output += s;
         }
 
         //input.GetComponent<InputFieldWithHideableCaret>().HideCaret();
         //CaretHidden = true;
         input.GetComponent<InputFieldWithHideableCaret>().text = output;
         //ChangeCaretPos();
         Invoke("ChangeCaretPos", 0.05f);
     }

and :

  void ChangeCaretPos()
     {
         input.GetComponent<InputFieldWithHideableCaret>().caretPosition = caretPos + 1;
     }

InputFieldWithHideableCaret is the same as TMP_InputField but with extra classes:

 public class InputFieldWithHideableCaret : TMP_InputField
 {
     // using same name conventions as the InputField for consistency
     private float m_CachedCaretBlinkRate;
 
     /// <summary>
     /// Caches BlinkRate, sets it to 0, hides the caret indefinitely.
     /// </summary>
     public void HideCaret()
     {
         m_CachedCaretBlinkRate = caretBlinkRate;
         caretBlinkRate = 0f;
         m_CaretVisible = false;
     }
 
     /// <summary>
     /// Sets the caretBlinkRate back to original value, existing logic will do the rest.
     /// </summary>
     public void UnhideCaret()
     {
         caretBlinkRate = m_CachedCaretBlinkRate;
     }
 }

Did anyone ever find a solution to this?

Thanks!

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How to set the position of these Texts ?? 0 Answers

[Solved] Saving data from Input Field 1 Answer

Input Field Problem 0 Answers

Find screen position of a specific word in a RichText? 1 Answer

how can I save text to input field with button? 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