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 playfulbacon · Apr 29, 2018 at 07:38 PM · uitextcanvasinstantiation

Instantiated text not positioning properly

I'm instantiating a GameObject for each letter in a line of poetry so that I can create dynamic effects. The preferred width of the text is used to position the letters.

 void SetupText()
     {
         // instantiate letters
         for (int i = 0, max = text.Length; i < max; ++i)
         {
             string letter = text[i].ToString();
             TrickleLetterUI l = Instantiate(letterPrefab).GetComponent<TrickleLetterUI>();
             l.gameObject.transform.SetParent(lettersParent, false);
             l.gameObject.name = "Letter: " + letter;
             l.Setup(letter);
             letters.Add(l);
         }
         letterPrefab.SetActive(false);
 
 
         // position letters
         for (int i = 0, max = text.Length; i < max; ++i)
         {
             TrickleLetterUI l = letters[i];
             l.letterWidth = l.text.preferredWidth;
 
             Vector3 letterPosition = lettersParent.position;
             if (i > 0)
             {
                 TrickleLetterUI prevLetter = letters[i - 1];
                 letterPosition = prevLetter.transform.position + (prevLetter.transform.right * ((prevLetter.letterWidth / 2) + l.letterWidth / 2));
             }
 
             l.transform.rotation = transform.rotation;
             l.gameObject.transform.position = letterPosition;
 
             l.Init(i, 0, this);
             wordWidth += l.letterWidth;
         }
     }

The GameObjects each have Text components and are children of a Canvas with a Canvas Scaler (UI Scale Mode set to Scale with Screen Size). When my Game window is set to the reference resolution (800x600), the text is positioned correctly: alt text

When my Game window is set to Maximize On Play, or sized differently than the reference resolution, the text isn't positioned correctly: alt text

textappearreferenceresolution.png (32.2 kB)
textmaximizeonplay.png (57.0 kB)
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 playfulbacon · May 05, 2018 at 04:07 PM

Thanks for your help @notpresident35. I ended up finding the answer here.

I just had to use localPosition instead of position when calculating and setting the distance between letters. Because localPosition comes from the RectTransform, I guess it is in screen units instead of world units. So simple!

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
1

Answer by DawdleDev · Apr 29, 2018 at 08:39 PM

Here's the issue. When you're creating the text, you are using world units for the position of the text. However, world units do not translate directly to screen units. Your letter size does seem to be affected by the screen resolution, but your positions are not. Thus, your UI elements get shoved into each other. Here's some potential fixes: 1. Decrease the size of your text on larger resolutions (if you are currently using a resizing script somewhere, decrease the amount that you are resizing it by) 2. Add a reference to the size of the screen into the part that moves each letter to its position 3. Use a horizontal layout group to control the distance between each letter One thing that would make it easier for you is to get or design a monospace font (each letter has the same width) and use that. Then, you wouldn't have to worry about the width of each letter when placing it on the screen.

Hope this helps!

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

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

Related Questions

My Text (Script) Disables at play time. How do I enable that again during run time? 1 Answer

How do I keep child gameobject of UI text 1 Answer

Unity 5 - Manually zooming/scaling in and out of an image with a text layer while still retaining text sharpness when zoomed? 0 Answers

View individual Rect boxes when multiple UI elements are selected? 0 Answers

Text in UI disappears when clicked on 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