Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Dunkelheit · Dec 14, 2013 at 02:21 PM · camera2dgui

Problem to position a GUIText centered at an object

Hi,

I'm trying to position a GUI text at an object. The text must be centered and to achieve this I have to get the text bounds as the target object bounds. I'm creating the GUIText on the fly (it is not a prefab). The picture below illustrates what suppose to be:

alt text

Rendered text is out of the object bounds, it's rendering aside the object not in its middle. I have tried to get the GUIText position using guiText.rendered.bounds.size.x but didn't work, and now trying with guiText.GetScreenRect().width and also not work at all.

Please, check out the part of the code:

 //create a GUIText
 go = new GameObject("FloatingText");
 guiText = go.AddComponent(typeof(GUIText)) as GUIText;

 //text properties
 guiText.color = color;
 guiText.fontSize = 16;
 guiText.text = "100";
 guiText.gameObject.AddComponent<DamageFloatingText>();

 //position the GUIText at object's center
 Vector3 objectPos = transform.position;
 objectPos.y += upSize;

 Debug.Log(transform.position.x);

 float xPos = (transform.position.x - (guiText.GetScreenRect().width / 2));
 objectPos.x = xPos;

 Debug.Log("Width " + guiText.GetScreenRect().width);

 Vector3 relativePosition = Camera.main.transform.InverseTransformPoint(objectPos);
 relativePosition.z = Mathf.Max(relativePosition.z, 1.0f);

 guiText.transform.position = 
     Camera.main.WorldToViewportPoint(Camera.main.transform.TransformPoint(relativePosition + Vector3.up));
     


xPost suppose to be the correct position to render the text.

Any clue or suggestion will be very helpful. Thanks in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Dec 14, 2013 at 02:52 PM

Not tested, but can't you just set GUIText.anchor to TextAnchor.MiddleCenter:

 guiText.anchor = TextAnchor.MiddleCenter;


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 Dunkelheit · Dec 14, 2013 at 04:26 PM 0
Share

Thanks man, you're a genius. I wondering I have to do it only by my own.

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

16 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

Related Questions

Is there a way to change the order of the "Layers" Tab? 0 Answers

Cameras and GUIs 1 Answer

Clamp To The Screen Edge 0 Answers

Apply Render Texture To GUI + Transparency? 2 Answers

How to stop view from scrolling when edge of view collides with a game object? 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