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#text3dtext

How to split up a Text Object into Text Objects of size 1 char 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

2 Replies

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

Answer by Glurth · Nov 16, 2016 at 06:45 PM

You will need to look at the font's specific characters in order to make the appropriate computations.

You can get the font from the TestMesh class, via https://docs.unity3d.com/ScriptReference/TextMesh-font.html

Once you have the font you can get each character's alignment and layout data, in that font, using https://docs.unity3d.com/ScriptReference/Font.GetCharacterInfo.html

The results are provided in the form of a https://docs.unity3d.com/ScriptReference/CharacterInfo.html structure, which should have all the information you need to compute positions.

Don't forget about other things that might affect positions, such as the alightment and anchor options, in the TextMesh. Particularly important for this, will be fontSize (watch out for zero values)!

Edit: oh yeah, just incase that isn't complex enough: Text Meshes can also use markup! e.g. Bold Text< /B >

Comment
Add comment · Show 1 · 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 matazematratze · Nov 17, 2016 at 03:12 PM 0
Share

That's awesome, thanks! Looks like that is everything I need. I'll come back to this thread otherwise.

avatar image
0

Answer by Varaughe · Apr 05, 2020 at 04:34 PM

Did you check https://assetstore.unity.com/packages/tools/gui/fully-flexible-ui-text-148050? You can separate each UI Text in as many UI Texts as the number of characters. It even take care of rich text feature.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to display text on a cube and how to change it dynamically? 2 Answers

How to read the format "Key: Value" 1 Answer

What's your equivalent of old GUIStyle ? 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