Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Laaevin · Jul 13, 2017 at 04:49 PM · javascripttextcanvasrecttransformbest practices

Best practice for creating Canvas text in Javascript?

I'm new to using Canvas and currently trying to convert a game that heavily uses OnGUI to using Canvas. This game has a LOT of textboxes and buttons, and they come and go in many sizes and locations with text that is constantly changing. So I would like to create and edit them via a script rather than in the editor.

In OnGUI I could do this with one line of code:

 var foo = "Changeable text";
 var x = 0;
 var y = 0;
 var w = Screen.width;
 var h = Screen.height/8;
 
 function OnGUI () {
     GUI.Box(Rect(x,y,w,h), foo); //Note that GUI.Box has a nice, semi-transparent background
 }

However, if I want to have the same result with Canvas (including the semi-transparent background) I need a LOT more code:

 var fooBox : GameObject;
 var fooPrefab : GameObject;
 var foo = "Changeable text";
 var x = 0;
 var y = 0;
 var w = Screen.width;
 var h = Screen.height/8;
 
 function Update () {
             fooBox = Instantiate(fooPrefab);
             fooBox.GetComponentInChildren(UI.Text).text = foo;
             fooBox.GetComponentInChildren(UI.Image).rectTransform.sizeDelta = new Vector2(w,h); // I need this in order to have a semi-transparent background
             fooBox.GetComponentInChildren(UI.Text).rectTransform.sizeDelta = new Vector2(w,h);
             fooBox.GetComponentInChildren(UI.Image).rectTransform.position = new Vector2(x,Screen.height-y);
             fooBox.GetComponentInChildren(UI.Text).rectTransform.position = new Vector2(x,Screen.height-y);
 }

This code works just fine. That is not the issue. But there's just so much code. There must be a better way. Any pointers?

Note: With fooPrefab the Text and Image are both children of the same game object, but even if I try to make one a child of the other, I still have to resize and move each one independently.

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

0 Replies

· Add your reply
  • Sort: 

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

116 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

Related Questions

Snap UI Text inside canvas 1 Answer

Change Text Scale on a Canvas Child from a script. 1 Answer

How to align pixel art with UI system ? 0 Answers

How do i position this text to go where i want? 1 Answer

Text smaller on higher resolution screen than on lower resolution screen 1 Answer


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