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 /
avatar image
0
Question by matazematratze · Nov 16, 2016 at 03:45 PM · c#text3d text

How to split up a text into substrings of single chars at the exact same position

Hi ppl,

so I'm basically trying to build a highlighting mechanic for texts as it would work on any OS.
In pseudo code something like

 If (user holds down MouseButton(0))
     hightlight every char the mouse hovers over until MouseButton(0)Up

or

 OnMouseButton(0)Down {
 store MousePosition
 }
 OnMouseButton(0)Up {
 highlight every char between stored position and this MousePosition
 }

Now as I see things I need either one of these:
- every character is a text object of its own with its own collider
- a method that translates the positions of the mouse into positions in the text object, creates 3 substrings (of the stuff before the highlighted part, the highlighted part itself and the stuff after that) and displays them in the same positioning as before

The second one seems pretty complicated so I tackled the first one. For usability I want to be able to enter text in the text component of my text object (I'm in Unity Editor right now) and when I hit start I want my method to cut the text into substrings of exactly 1 char.
While this is no problem, I don't know how to position the generated chars so they match their previous positions exactly no matter what font size I chose.

What I need is an algorithm that gives me an offset for the position depending on font size and width of that particular character in the given font.

Any ideas on that?

What I currently have works to create those 1 char text objects with the same font, size etc. as the given text. I use 3D Text instead of UI Text but for no particular reason.

 Transform textParent; //this is simply an empty GameObject holding all my text objects
 
     void Start () {
         textParent = GameObject.Find("Texts").transform;
 
         for (int i = 0; i < textParent.childCount; i++)
         {
             CreateObjectForEveryChar(textParent.GetChild(i));
         }
     }
 
 void CreateObjectForEveryChar(Transform textVessel)
     {
         TextMesh textMesh = textVessel.GetComponent<TextMesh>();
         char[] text;
         float offset = 0f;
 
         if (textVessel.GetComponent<TextMesh>() == null)
         {
             Debug.Log("Error - no TextMesh found on " + textVessel.ToString());
         }
         else
         {
             text = textMesh.text.ToCharArray();
             for(int i = 0; i < text.Length; i++)
             {
                 GameObject character = (GameObject) Instantiate(textVessel.gameObject, textVessel.localPosition + Vector3.left * offset, textVessel.localRotation);
                 //character.transform.SetParent(textVessel); //this actually makes unity freeze everytime I hit play so I'm skipping that for now
                 character.GetComponent<TextMesh>().text = text[i].ToString();
                 //offset += ??;
             }
         }
     }









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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is it possible to script a 3D Text? 1 Answer

Best Practice for Multiple Languages? 6 Answers

Maintaining Text Size Ratio With Screen 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