Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 obiolg · Mar 19, 2015 at 01:17 PM · guicanvasscriptingbasicsbasicsbasic programming

Position Text inside Canvas via Script

I have a simple clear 2d scene. There is a Main camera with the strength of 5 and a canvas to hold GUI elements.

I have a script attached to canvas which creates a GameObject and adds a Text component to the Canvase.

Now I would like to position Text inside Canvas. Canvas' width is 900 of some_units and it takes the whole Camera width (strength is 10 meters).

How do I do it programmaticaly? Don't quite understand how to convert 900 of some_units (prob pixels) into 10 meters.

EDIT: Added some code

 ...
 RectTransform rectTransform = textGameObject.GetComponent<RectTransform>();
 
 // Put anchor point into left upper corner
 rectTransform.anchorMin = new Vector2(0, 1)
 rectTransform.anchorMax = new Vector2(0, 1)
 
 // Put pivot into left upper corner as well
 rectTransform.pivot = new Vector2(0, 1)
 
 // Position text at (0, 0) coordinate
 textRectTransform.transform.position = new Vector2(0, 0); // doesn't work
 textRectTransform.anchoredPosition = new Vector2(0, 0); // doesn't work
 textRectTransform.localPosition = new Vector2(0, 0); // doesn't work
 ...

How the text can be posotioned in the left upper corner as well?

EDIT2: Ok, anchoredTransform actually worked.. being called during Start() it didn't set posotion correctly.

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
1
Best Answer

Answer by tigertrussell · Mar 19, 2015 at 01:21 PM

Note: all of this only applies to overlay mode only, not Screen Space and not World Space.

Unity's UI is rendered as an overlay onto your camera; there is no conversion to be done, the width of the Canvas is measured in pixels. The Canvas should cover your entire screen - and if you re-size the Player window, you should see your Canvas re-size the same way.

You Instantiate a prefab which contains a UnityEngine.UI.Text object, call SetParent on its Transform to parent it by something which is parented by the Canvas (or the Canvas itself) and you can position it using its RectTransform.

Make sense?

Comment
Add comment · Show 8 · 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 obiolg · Mar 19, 2015 at 01:38 PM 0
Share

Thank you, it makes sense. But when I can't use Vector3 to adjust RectTransform's position, since Vector3 receives values in meters, while I have value (Canvas' width) in pixels. That is the problem.

avatar image obiolg · Mar 19, 2015 at 01:42 PM 0
Share

$$anonymous$$g. I'd like to set Text's position to 450px. The code will look like: textRectTransform.transform.position = new Vector3(????)

avatar image tigertrussell · Mar 19, 2015 at 03:14 PM 0
Share

Why do you say Vector3 is in meters? It's not Vector3 that measures in meters, it's a Transform. A RectTransform would measure in pixels. As long as your UnityEngine.UI.Text object is the child of a Canvas, the RectTransform will take over and handle everything in screen pixels, unless I am mistaken.

Unfortunately I am at work so I can't do any experiments to help you :\

avatar image obiolg · Mar 19, 2015 at 03:26 PM 0
Share

Updated the question. Check it out, please.

avatar image tigertrussell · Mar 19, 2015 at 03:31 PM 0
Share

When you say it "doesn't work," do you see the Text appearing somewhere else on the screen? Or is it possible that the RectTransform may have 0 (or very close to zero) size?

Show more comments
avatar image
0

Answer by Rbrinks · Aug 30, 2020 at 02:29 AM

If this still isn't working for anyone, try changing position to localPosition. Worked for me anyway.

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

22 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

Related Questions

Adding Text to Canvas via script 4 Answers

Using Canvas/Gui with splitscreen(several cameras) 0 Answers

[VR] How to display a canvas with Oculus 5 Answers

Good way to scale 3D GameObjects in a Canvas (Screen Space - Camera)? 1 Answer

placing a text on to an platform, on infinite runner 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