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 tedbox39 · Dec 06, 2015 at 02:54 PM · scripting problemuitextui image

How to match UI Image size to UI Text size when using ContentSizeFitter on UI Text?

I have a prefab that has a UI text object and a separate UI image object grouped together. The aim of this is to have text on screen with a semi-transparent background behind it.

I am using a ContentSizeFitter component to make the UI text's RectTransform fit the size of the text string applied to it in a script and I want to be able to set the size of the UI image in the script to match this. However, when I access the size of the UI text's RectTransform.sizeDelta it returns (0, 0). I have included a code snippet below to show what I am doing.

 // Instantiate a panel, which contains the text and background objects
 GameObject panel = Instantiate (starpanel, pos, Quaternion.identity) as GameObject;
 
 // Get the text and background objects
 GameObject title = panel.transform.Find("StarTitle").gameObject;
 GameObject background = panel.transform.Find("Background").gameObject;
 
 // Add name to title and make text white
 title.name = planet.name;
 title.GetComponent<Text>().text = planet.name;
 title.GetComponent<Text>().color = new Color(255, 255, 255, 255);
 
 // Set the background to the same size/location as the text
 background.transform.position = title.transform.position;
 Debug.Log ("title size = " + title.GetComponent<RectTransform>().sizeDelta);                        // RETURNS (0, 0)

In the Editor at runtime, the UI text RectTransform has a note saying "Some values driven by ContentSizeFitter" and the width and height deltas are greyed out, although they do have values in them. I have looked at the ContentSizeFitter documentation and can't see any way to access the width and height that that component is generating.

Does anyone know of a way to get the size generated by the ContentSizeFitter component? I have had a trawl of Unity answers, but can't find anything that specifically addresses this question.

Or, thinking laterally, is there another way to tie to the size of my UI image to the size of the UI text object? I have tried adding the UI image component directly to the same object as the UI text (worth a shot...), but that throws an error saying "Can't add 'Image' to {object name} because a 'Text' is already added to the game object!"

Comment
Add comment · Show 1
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 tedbox39 · Dec 06, 2015 at 09:45 AM 0
Share

After a bit more testing and exploration, I have realised that you can get the text component's preferred width and height even if you use a ContentSizeFitter on the text object or on a parent layout group. These values are the same as the inaccessible RectTransform width and height values.

See here: http://docs.unity3d.com/ScriptReference/UI.Text-preferredWidth.html

1 Reply

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

Answer by tedbox39 · Dec 05, 2015 at 04:42 PM

I got this to work using a Panel with a Horizontal Layout Group component and a ContentSizeFitter component and then making the UI Text object a child of this.

The paragraph titled "Fit to size of UI element with child Text" on this page was very helpful: http://docs.unity3d.com/Manual/HOWTO-UIFitContentSize.html

I had a slight problem with this approach in that my text object wasn't appearing in the game when I instantiated it and made it the child of another UI element. I discovered that you need to use a different method for setting the transform's parent so that it's coordinates aren't set relative to the parent (and therefore a long way away!)

See here for an answer to this: http://answers.unity3d.com/questions/907585/46-newly-instantiated-ui-prefabs-are-not-visible.html ... and documentation here: http://docs.unity3d.com/ScriptReference/Transform.SetParent.html

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 stratos_kakalis · Oct 16, 2016 at 08:01 AM 0
Share

Great answer, thanks a lot for clarifying how you did it, it helped me a lot :)!

avatar image barmhartsam · Mar 24, 2017 at 08:06 PM 0
Share

Hi, I know this is old but since you cracked it I thought I would ask. I have a problem where my text is going over the screen border. $$anonymous$$y panel is anchored to the middle and my text is a child of the panel. How do I stop it from overflowing?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers

Display Emojis using its HTML code in a text 1 Answer

Display the same string in multiple text boxes 1 Answer

OnPointerClick in child's script is not triggered when there exists a OnPointerDown in parent's script 2 Answers

add and remove matches 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